Binary search trees have the following properties:
X is a node in a two-fork lookup tree, and if Y is a node in the X left dial hand tree, then y.key≤x.key; If Y is a node in the X right subtree, then X.key≥y.key.
The time to perform basic operations on a binary tree is proportional to the height of the tree. When this tree is a fully binary tree, the worst case run time for these operations is θ (LGN), and if the tree is a linear chain with n nodes, the worst case scenario for these operations is θ (n). We can construct a two-fork lookup tree by randomly (the desired height: E (h) =o (LGN)), so that the average time for the basic dynaset operation on this tree is θ (LGN).
Basic operation:
Find keywords
Maximum Value & Minimum value
Finding a successor to a key
Insert
Delete
The complete code is as follows:
#include <iostream> #include <cstdlib>using namespace std;typedef struct Bstnode{int key; Key value//int data; Satellite Databstnode *parent; Parent Nodebstnode *left; Left child Nodebstnode *right; Right child node}bstnode;typedef struct Bstree{bstnode *root;} Bstree;void Bst_insert (bstree *t,int value) {Bstnode *x=t->root; Bstnode *y=null; Y is X ' s parent nodebstnode *z=new Bstnode ();//new inserted nodez->key=value;z->parent=null;z->left=null;z- >right=null;//locate Insert Positionwhile (x!=null) {y=x;if (Z->key < X->key) x=x->left;elsex=x-> Right }//link new node to it parent nodez->parent = Y;//link parent node to new Nodeif (Y==null)//treee is EMP Tyt->root=z;else if (Z->key < Y->key) Y->left = Z;elsey->right = Z;} void Bst_inorderwalk (Bstnode *x) {if (x!=null) {bst_inorderwalk (x->left);cout<<x->key<< ""; Bst_inorderwalk (X->right); }}bstnode *bst_search (BstnoDe *x,int skey) {if (X==null | | skey==x->key) return x;if (Skey < X->key) return Bst_search (X->left,skey); Elsereturn Bst_search (X->right,skey);} Bstnode *bst_minimum (Bstnode *x) {while (x->left!=null) X=x->left;return x;} Bstnode *bst_maximum (Bstnode *x) {while (x->right!=null) X=x->right;return x;} Bstnode *bst_successor (Bstnode *x) {if (x->right!=null) return bst_minimum (x->right); Bstnode *y=x->parent;while (y!=null && x==y->right) {x=y;y=y->parent;} return y;} Bstnode *bst_predecessor (Bstnode *x) {if (X->left!=null) return Bst_maximum (X->left); Bstnode *y=x->parent;while (y!=null && x==y->left) {x=y;y=y->parent;} return y;} void Bst_transplant (Bstree *t,bstnode *u,bstnode *v) {if (u->parent==null) t->root=v;else if (u==u->parent- >left) u->parent->left=v;elseu->parent->right=v;if (V!=null) v->parent=u->parent;} void Bst_delete (Bstree *t,bstnode *z) {if (z->left==null)//case 1 bst_transplant (t,z,z->right); else if (z->right==null)//case 2bst_transplant (t,z,z->left); Else//case 3 {bstnode *y=bst_minimum (z->right); if (y->parent!=z) {bst_transplant (t,y,y-& Gt;right); y->right=z->right;y->right->parent=y;} Bst_transplant (t,z,y); y->left=z->left;y->left->parent=y;}} int main () {int a[]={12,5,2,9,18,15,17,19};int n=sizeof (A)/sizeof (int);cout<< "/*----------------------Create BST-------------------------*/"<<endl; Bstree *t=new Bstree (); T->root =null;for (int i=0;i<n;i++)//create Binary Search Treebst_insert (t,a[i]);cout<< "The Tree is (Inord Er-walk-tree): "<<endl; Bst_inorderwalk (t->root);cout<<endl;cout<< "/*-------------------------------------------------- -------*/"<<endl;cout<<"/*-----------------------Search BST------------------------*/"<<endl; int skey; Bstnode *snode=null;while (1) {cout<< "Please input the searching key ( -1 denote exiting):"; cIn>>skey;if (skey==-1) {cout<< "Exiting ..." <<endl;break;} Snode=bst_search (T->root,skey); if (snode!=null) cout<< "The node exists in the tee!" <<endl;elsecout<< "The node doesn ' t exist." <<endl;} cout<< "/*----------------------------------------------------------*/" <<endl;cout<< "/*------ -------------Minimum and Maximum--------------------* * "<<endl; Bstnode *minnode=null,*maxnode=null;minnode=bst_minimum (t->root); Maxnode=bst_maximum (T->root);cout< < "The Minimum of the BST is:" <<minNode->key<<endl;cout<< "the Maximum of the BST are:" << maxnode->key<<endl;cout<< "/*----------------------------------------------------------*/" < <endl;cout<</*-------------------successor and predecessor--------------*/"<<endl;cout<<" Please input the node ' s key: ";cin>>skey; Bstnode *curnode=null;curnode=bst_search (T->root,skey); if (curnode!=null) {Bstnode *SUCNODE=NULL,*PREnode=null;sucnode=bst_successor (Curnode);p renode=bst_predecessor (Curnode);cout<< "The Successor of the Current Node ' <<curNode->key<< ' is: ' << sucnode->key<<endl;cout<< ' the Predecessor of the current Node ' <<curNode->key<< ' is: ' << Prenode->key<<endl;} elsecout<< "The node Doen ' t exist." <<endl;cout<< "/*-----------------------------------------------------------*/" <<endl;cout <</*----------------------Insert-------------------------------*/"<<endl;int ikey;cout<<" Please input the iserting node ' s key: ";cin>>ikey; Bst_insert (T,ikey);cout<< "After inserting, the Tree is:" <<endl; Bst_inorderwalk (t->root);cout<<endl;cout<< "/*-------------------------------------------------- ---------*/"<<endl;cout<<"/*------------------deletion---------------------------------*/"<< Endl;int dkey;cout<< "Please input the deleting node ' s key:";cin>>dkey; Bstnode *dnode=null;dnode=bst_search (T->root,dkey); if (dnode==null) cout<< "The node doesn ' T exist in the treee ." <<endl;else{Bst_delete (T,dnode);cout<< "After deleting, the tree is:" <<endl; Bst_inorderwalk (t->root); Cout<<endl;} cout<< "/*-----------------------------------------------------------*/" <<endl;return 0;}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Algorithms Chapter 12th: Two-fork search tree (binary search Trees)