[cpp]
#include <iostream>
#include <vector>
using namespace std;
/*二叉查找樹結構*/
typedef struct BSTree
{
int node_value;
struct BSTree * left;
struct BSTree * right;
struct BSTree * parent;
}Tree;
Tree * root = NULL;
/*****構造二叉查找樹**********************************************/
void CreateBSTree(Tree * root,int node_value);
Tree * CreateBSTree(int * array_list,int array_length);
void Print(Tree* root);
/***************************************************************/
int Minimum(Tree * p);
Tree * TreeMinimum(Tree * root);
int Maximum(Tree * p);
Tree * TreeMaximum(Tree * root);
Tree * FindNode(Tree * root,int node_value);
Tree * Successor(Tree * root);
Tree * PredeSuccessor(Tree * p);
bool DeleteTreeNode(Tree * root,int node_value);
bool DeleteTreeNode(Tree * n);
/***************************************************************/
int main(int argc,char * argv[])
{
//int list[]={5,3,4,9,1,7,11};
int list[]={15,5,16,3,12,20,10,13,18,23,6,7};
root = CreateBSTree(list,12);
std::cout<<"Cearte BSTree."<<std::endl;
//Print(root);
//std::cout<<Successor(FindNode(root,4))->node_value;
//Print(root);
//std::cout<<std::endl;
//DeleteTreeNode(root,15);
//Print(root);
Tree * t = FindNode(root,18);
std::cout<<PredeSuccessor(t)->node_value;
getchar();
return 0;
}
/*找出樹中的最小節點
p數的根節點
*/
int Minimum(Tree * p)
{
Tree * t = TreeMinimum(p);
if(t != NULL)
{
return t->node_value ;
}
else
{
return -1;
}
}
Tree * TreeMinimum(Tree * p)
{
if(p->left == NULL)
{
return p;
}
TreeMinimum(p->left);
}
/*找出樹中的最大節點*/
int Maximum(Tree * p)
{
Tree * t = TreeMaximum(root);
if(t != NULL)
{
return t->node_value ;
}
else
{
return -1;
}
}
Tree * TreeMaximum(Tree * p)
{
if(p->right == NULL)
{
return p;
}
TreeMaximum(p->right);
}
/*假定所有的節點值都不相同,找到一個節點的指針
p樹的根節點,
node_value要查找的節點的值
*/
Tree * FindNode(Tree * p,int node_value)
{
if(p == NULL)
{
return NULL;/*遞歸返回標志*/
}
if(p->node_value == node_value)
{
return p;
}
if(p->node_value < node_value)
{
FindNode(p->right, node_value);
}
else
{
FindNode(p->left, node_value);
}
}
/*找出一個節點的後繼結點*/
Tree * Successor(Tree * p)
{
if(p == NULL)
{
return NULL;
}
if(p->right != NULL)
{
return TreeMinimum(p->right);
}
Tree * t = p->parent ;
while((t != NULL) && (p == t->right))
{
p = t;
t = t->parent ;
}
return t;
}
/*找到一個節點的前驅節點
p為節點的指針
*/
Tree * PredeSuccessor(Tree * p)
{
if(p == NULL)
{
return NULL;
}
else if(p->left != NULL)
{/*如果左子樹不為空,則前驅為其左子樹的最大值節點*/
return TreeMaximum(p->left);
}
else
{
Tree * t = p->parent ;
while((t != NULL) && (p == t->left))
{/*注意節點t的方向,這個與尋找後繼節點相反*/
p = t;
t = t->parent;
}
return t;
}
}
/*刪除一個節點
p為樹根節點指針
node_value要刪除的節點的值
*/
bool DeleteTreeNode(Tree * p,int node_value)
{
Tree * t = FindNode(p,node_value);
if(t == NULL)
{
return false;
}
if((t->left == NULL)&&(t->right == NULL))
{/*沒有子節點*/
Tree* tmp = t;
if(tmp->parent->left == tmp)
{
tmp->parent->left = NULL;
}
else
{
tmp->parent->right = NULL;
}
delete tmp;
tmp = NULL;
}
else if((t->left == NULL)||(t->right == NULL))
{/*一個子節點*/
Tree* tmp = t;
if(tmp->parent->left == tmp)
{
tmp->parent->left = (tmp->left ==NULL)?tmp->right :tmp->left;
}
else
{
tmp->parent->right = (tmp->left ==NULL)?tmp->right :tmp->left;
}
delete tmp;
tmp = NULL;
}
else
{/*兩個子節點*/
Tree* s = Successor(t);
if(s == NULL)
{
return false;
}
t->node_value = s->node_value ;
DeleteTreeNode(s);
}
}
/*刪除一個節點
p為樹根節點指針
*/
bool DeleteTreeNode(Tree * n)
{
if(n == NULL)
{
return NULL;
}
else if((n->left == NULL)&&(n->right == NULL))
{/*沒有子節點*/
Tree* tmp = n;
if(tmp->parent->left == tmp)
{
tmp->parent->left = NULL;
}
else
{
tmp->parent->right = NULL;
}
delete tmp;
tmp = NULL;
}
else if((n->left == NULL)||(n->right == NULL))
{/*一個子節點*/
Tree* tmp = n;
if(tmp->parent->left == tmp)
{
tmp->parent->left = (tmp->left ==NULL)?tmp->right :tmp->left;
}
else
{
tmp->parent->right = (tmp->left ==NULL)?tmp->right :tmp->left;
}
delete tmp;
tmp = NULL;
}
else
{/*兩個子節點*/
Tree* s = Successor(n);
if(s == NULL)
{
return false;
}
n->node_value = s->node_value ;
DeleteTreeNode(s);
}
}
/*生成二叉查找樹*/
Tree * CreateBSTree(int * array_list,int array_length)
{
if(array_length <= 0)
{
return false;
}
Tree * root = NULL;
root = new BSTree();
root->left = NULL;
root->right = NULL;
root->parent = NULL;
root->node_value = array_list[0];
for(int i=1;i<array_length;i++)
{
CreateBSTree(root,array_list[i]);
}
return root;
}
void CreateBSTree(Tree * root,int node_value)
{
if(root == NULL)
{
return ;
}
if(root->node_value > node_value)
{
if(root->left == NULL)
{
Tree * node = new Tree();
node->left = NULL;
node->right = NULL;
node->node_value = node_value;
node->parent = root;
root->left = node;
}
else
{
CreateBSTree(root->left,node_value);
}
}
else
{
if(root->right == NULL)
{
Tree * node = new Tree();
node->left = NULL;
node->right = NULL;
node->node_value = node_value;
node->parent = root;
root->right = node;
}
else
{
CreateBSTree(root->right,node_value);
}
}
}
/*中序排序輸出二叉查找樹*/
void Print(Tree* root)
{
if(root == NULL)
{
return ;
}
Print(root->left);
std::cout<<root->node_value<<"\t";
Print(root->right);
}