二叉查找树(BST)

《二叉查找树(BST)》
《二叉查找树(BST)》
BST的定义

1 //Definition of a Binary Search Tree
2 typedef struct BST
3 {
4 int value;
5 struct BST* left;
6 struct BST* right;
7 }BST;
8 BST* root=new BST;

 

《二叉查找树(BST)》
《二叉查找树(BST)》
初始化

 1 /*Initialization of a BST, based on values in a vector.
2 First element of the vector fills the root, then others will be in the proper position.*/
3 void Init_BST(vector<int>& vec)
4 {
5 if(vec.empty())
6 return;
7 vector<int>::iterator iter=vec.begin();
8 root->value=*iter;
9 root->left=NULL;
10 root->right=NULL;
11 BST* node,*pre;
12 iter++;
13 for(;iter!=vec.end();++iter)
14 {
15 node=root;
16 while(node!=NULL)
17 {
18 pre=node;
19 if(*iter>node->value)
20 node=node->right;
21 else
22 node=node->left;
23 }
24 BST* inode=new BST;
25 inode->value=*iter;
26 inode->left=NULL;
27 inode->right=NULL;
28 if(*iter>pre->value)
29 pre->right=inode;
30 else
31 pre->left=inode;
32 }
33 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
查找

 1 //Search the key downward the tree
2 bool BST_Search(BST* root,int key)
3 {
4 while(root!=NULL)
5 {
6 if(key==root->value)
7 return true;
8 else if(key>root->value)
9 root=root->right;
10 else
11 root=root->left;
12 }
13 return false;
14 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
最小值和最大值

 1 //The min node is in the left of the tree
2 int BST_Minimum(BST* root)
3 {
4 while(root->left!=NULL)
5 {
6 root=root->left;
7 }
8 return root->value;
9 }
10
11 //The max node is in the right of the tree
12 int BST_Maximum(BST* root)
13 {
14 while(root->right!=NULL)
15 root=root->right;
16 return root->value;
17 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
后继

 1 /*
2 If the key node has right child, the successor is the min node of the right subtree;
3 else, upward the tree from current node, until there is a left-child relationship,
4 or this node has no successor.
5 */
6 int BST_Successor(BST* root,int key)
7 {
8 if(!BST_Search(root,key))
9 return -1;
10 stack<BST*> s;
11 while(root!=NULL)
12 {
13 if(root->value==key)
14 break;
15 else if(root->value>key)
16 {
17 s.push(root);
18 root=root->left;
19 }
20 else
21 {
22 s.push(root);
23 root=root->right;
24 }
25 }
26 if(root->right!=NULL)
27 return BST_Minimum(root->right);
28 while(!s.empty())
29 {
30 BST* p=s.top();
31 s.pop();
32 if(p->right==root)
33 {
34 root=p;
35 }
36 else
37 return p->value;
38 }
39 return -1;
40 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
前驱

 1 /*
2 If the key node has left child, the successor is the max node of the left subtree;
3 else, upward the tree from current node, until there is a right-child relationship,
4 or this node has no predecessor.
5 */
6 int BST_Predecessor(BST* root,int key)
7 {
8 if(!BST_Search(root,key))
9 return -1;
10 stack<BST*> s;
11 while(root!=NULL)
12 {
13 if(root->value==key)
14 break;
15 else if(root->value>key)
16 {
17 s.push(root);
18 root=root->left;
19 }
20 else
21 {
22 s.push(root);
23 root=root->right;
24 }
25 }
26 if(root->left!=NULL)
27 return BST_Maximum(root->left);
28 while(!s.empty())
29 {
30 BST* p=s.top();
31 s.pop();
32 if(p->left==root)
33 root=p;
34 else
35 return p->value;
36 }
37 return -1;
38 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
插入

 1 //Insert in the proper position
2 void BST_Insert(BST* root,int key)
3 {
4 BST* node=new BST;
5 node->value=key;
6 node->left=NULL;
7 node->right=NULL;
8 if(root==NULL)
9 root=node;
10 else
11 {
12 BST* pre;
13 while(root!=NULL)
14 {
15 if(root->value>key)
16 {
17 pre=root;
18 root=root->left;
19 }
20 else
21 {
22 pre=root;
23 root=root->right;
24 }
25 }
26 if(pre->value>key)
27 pre->left=node;
28 else
29 pre->right=node;
30 }
31 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
删除

 1 /*
2 If the key node has no children,the change the parent's corresponding pointer to null;
3 else if the key node has either child, change the parent's corresponding pointer to node's child;
4 else, find the successor of the node, substitute it with the successor, and handle the successor's parent in proper way.
5 */
6 void BST_Delete(BST* root,int key)
7 {
8 if(!BST_Search(root,key))
9 return;
10 BST* pre;
11 while(root!=NULL)
12 {
13 if(root->value==key)
14 break;
15 else if(root->value>key)
16 {
17 pre=root;
18 root=root->left;
19 }
20 else
21 {
22 pre=root;
23 root=root->right;
24 }
25 }
26 if(root->left==NULL||root->right==NULL)
27 {
28 if(root->left!=NULL)
29 {
30 if(pre->left==root)
31 pre->left=root->left;
32 else
33 pre->right=root->left;
34 }
35 else
36 {
37 if(pre->left==root)
38 pre->left=root->right;
39 else
40 pre->right=root->right;
41 }
42 }
43 else
44 {
45 pre=root;
46 BST* post=root->right;
47 while(post->left!=NULL)
48 {
49 pre=post;
50 post=post->left;
51 }
52 root->value=post->value;
53 if(pre->left==post)
54 pre->left=post->right;
55 else
56 pre->right=post->right;
57 }
58 }

 

《二叉查找树(BST)》
《二叉查找树(BST)》
测试程序

 1 int main()
2 {
3 vector<int> v;
4 int s;
5 while(cin >>s)
6 v.push_back(s);
7
8 Init_BST(v);
9 In_Order(root);
10 if(BST_Search(root,5))
11 cout <<"OK"<<endl;
12 cout <<endl<<BST_Minimum(root)<<" "<<BST_Maximum(root)<<endl;
13 cout <<BST_Successor(root,5)<<" "<<BST_Predecessor(root,5)<<endl;
14 BST_Insert(root,3);
15 In_Order(root);
16 cout <<endl;
17 BST_Delete(root,5);
18 In_Order(root);
19 return 0;
20 }

 

    原文作者:Cavia
    原文地址: https://www.cnblogs.com/daniagger/archive/2012/01/31/2333884.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞