#include <iostream> #include <string> #include <queue> using namespace std;
typedef int KEYTYPE;
#define NUM class Binstree;
Class Binstreenode {Public:keytype key;
Binstreenode *lchild;
Binstreenode *rchild;
Binstreenode () {lchild=null;
Rchild=null;
}
};
Class Binstree {Public:binstreenode *root;
Binstree () {root=null;
} ~binstree () {//delete root; } binstreenode *bstreesearch (/*binstreenode *bt,*/keytype k,binstreenode *&p);/lookup failure Returns NULL,P record the parent node void to be inserted
Bstreeinsert (KeyType k);//bt with references, inserting nodes as root nodes, you need to modify root int bstreedelete (KeyType k);
void Bstreepreorder (Binstreenode *bt);
void Bstreeinorder (Binstreenode *bt);
void Bstreeaforder (Binstreenode *bt);
void Bstreeleveltraverse (Binstreenode *bt);/hierarchy traverse bool IsEmpty () {return root==null;
}
}; * * Binary sort Tree Lookup algorithm * Find the node of element K in the two-fork sort tree with the root pointer of BT. Returns a pointer to the node if the lookup succeeds the parameter P points to the found node; When the lookup fails, the parameter P points to the parent node to which K should be inserted. * Binstreenode * Binstree::bstreesearch (/*binstreenode *bt,*/keytype k,binstreenode *&p) {
Binstreenode *q=null;
Q=root;
while (q) {p=q;
if (q->key==k) return p;
else if (q->key>k) q=q->lchild;
else q=q->rchild;
return NULL;
}//insert operation void Binstree::bstreeinsert (KeyType k) {Binstreenode *p=null,*q;
Q=root;
Insert {Binstreenode *r=new binstreenode if Bstreesearch (k,p) ==null)/lookup fails;
r->key=k;
if (Q==null)//root node is empty {root=r;
return;
} if (P&&k<p->key) p->lchild=r;
else if (P&&k>p->key) p->rchild=r;
}///first-order traversal void Binstree::bstreepreorder (Binstreenode *bt) {if (BT) {cout<<bt->key<< "";
Bstreepreorder (Bt->lchild);
Bstreepreorder (Bt->rchild);
//in-order traversal void Binstree::bstreeinorder (Binstreenode *bt)//Time complexity O (N);
{if (BT) {Bstreeinorder (bt->lchild);
cout<<bt->key<< "";
Bstreeinorder (Bt->rchild);
}//Subsequent traversal of void Binstree::bstreeaforder (Binstreenode *bt) {if (BT) {Bstreeaforder (bt->lchild); Bstreeaforder (BT->rchild);
cout<<bt->key<< "";
}//Hierarchy traversal void Binstree::bstreeleveltraverse (Binstreenode *bt) {queue<binstreenode*> q;
if (BT) Q.push (BT); while (!q.empty ()) {Cout<<q.front ()->key<< "";//Access Team header node if (Q.front ()->lchild) Q.push (Q.front ()-&G
T;lchild);
if (Q.front ()->rchild) Q.push (Q.front ()->rchild);
Q.pop ();
} cout<<endl;
///Two fork sort tree delete operation, delete successful return 1, failure return 0.
int Binstree::bstreedelete (KeyType k) {Binstreenode *f,*p,*q,*s;
P=root;
F=null;
Find the node with the keyword K and find the parents of the node while (p&&p->key!=k) {f=p;//f Record parent node if (p->key>k) p=p->lchild;
else p=p->rchild;
if (!p)//Cannot find the node to be deleted return 0;
The left subtree of the IF (p->lchild==null)//To delete node is empty {if (f==null)//The node to be deleted is the root node root=p->rchild;
else if (f->lchild==p)//The node to be deleted is the left nodes of its parent node f->lchild=p->rchild;
else//Pending deletion node is the right node of its parent node f->rchild=p->rchild;
Delete p;
else//Pending deletion node has Zoozi tree {q=p;
s=p->lchild; while (S->rChild)//To find the leftmost node in the left subtree of the node to be deleted, that is, to find the middle order precursor node of the node to be deleted {q=s;
s=s->rchild;
} if (q = = p) Q->lchild = s->lchild;
else Q->rchild = s->lchild; P->key = s->key;
Value substitution deletes the node delete s;
return 1;
int main () {int a[num]={34, 18, 76, 13,12,11, 52, 82, 16, 67, 58, 73, 72};
Binstree BST;
Binstreenode *PBT = null, *p = NULL, *pt = NULL; for (int i=0;i<num;i++) {BST.
Bstreeinsert (A[i]); } PT = BST. Bstreesearch (Wuyi, p); Search sort binary tree BST.
Bstreeleveltraverse (Bst.root);
if (PT) {cout<<pt->key<<endl; } BST.
Bstreepreorder (Bst.root);
cout << Endl; Bst. Bstreedelete (13); Remove the condition of the left child without BST.
Bstreepreorder (Bst.root);
cout << Endl; Bst. Bstreedelete (76); Delete the condition BST with the left child.
Bstreepreorder (Bst.root);
cout << Endl;
System ("pause");
return 0; }
The establishment, insertion, deletion, lookup of binary sort tree and the complete implementation version of C + + 4 traversal methods