//Nodetypedef struct bsnode{int data; struct Bstree *lchild; struct Bstree *rchild;} Bsnode,*bstree; ------------------------------------------------------------------------------------
To create a two-fork sort tree:
1 Create a two-fork tree:void Createbstree (Bstree &bt) {//Call Insert function to insert node one by one int key; scanf ("%d", &key); if (key!= ") {Insertnode (Bt,key); scanf ("%d", &key); }}
2. Insert the node: bstree Insertbsnode (Bstree Bt,char key) { bstree p=bt; while (p) { //Find the location to insert, when P is empty at the end, Pre for the parent to insert the node pre=p; if (P->data==key) return Bt; else if (keydata) p=p-> Lchild; else p=p->rchild; } q= (bstree) maliic (sizeof (Bsnode)); q->data=key; q->lchild=0; q->rchild=0; if (!BT) Bt=q; else{ if (Keydata) pre->lchild=q; else pre->rchild=q; } return BT;}
3. Delete tree (call delete function to delete node individually)void DeleteTree (Bitree T) {if (! T) return OK; else{if (t->lchild) DeleteTree (t->firstchild); if (t->lchild) DeleteTree (t->nextsibling); Free (T); T=null; }}
4. Delete the node: bstree Deletebsnode (Bstree bt) { bstree p=bt; while (p) { pre=p; //marks its parent node to remove if (key==p->data) break; //Find the node to delete else if (keydata) p=p->lchild; else p =p->rchild; } if (!bt| |! p) return bt; //tree is empty or no if found ( p->lchild==null| | P->rchild==null) //non-empty but the node to be deleted has no left/right subtree/No Child if (Pre==NULL) { //the node to be deleted is the root node if (!p->child) bt= p->rchild; else bt=p->lchild; } else{ //the node to be removed is not the root node if (!p->lchild) { if (P==pre->lchild) pre->lchild=p->rchild; else pre- >rchild=p->rchild; } else{ if (P==pre->lchild) pre->lchild=p-> lchild; else pre->rchild=p->lchild; } } else{ //The number of words left and right for the node to be deleted exists. At this point, find the maximum value of the left subtree, and the right child/root node of the left subtree, copy it to the node you want to delete, and then delete the child pre=p; //hold the P pointer to the node to be deleted, pointing to the precursor with two pointers and subsequent q=pre->lchild; while (q->rchild) { / /Find the right child of the left subtree pre=q; q=pre->rchild; } p->data=q->data; //assigns the value of the right child or left child root node of the left subtree to the node to be deleted if (pre==p) //Zuozi No right child pre->lchild=q->lchild; else //found the right child, At this time, the right child must be the right child of his predecessor pre->rchild=q-> lchild; }} ----------------------------------------------------------------------- ------------
traversal of a two-fork sort tree (as with a two-tree traversal algorithm)
1. Recursive traversalFirst-order traversal of void Preordertraverse (Bstree &t) {if (T) {visit (t->data); Preordertraverse (T->lchild); Preordertraverse (T->rchild); } return OK; Middle order traversal void Inordertraverse (Bstree &t) {if (T) {inordertraverse (t->lchild); Visit (T->data); Inordertraverse (T->lchild); } return OK; Post-post traversal of void Postordertraverse (Bstree &t) {if (T) {postordertraverse (t->lchild); Postordertraverse (T->rchild); Visit (T->data); }}
2. Non-recursive traversal, with stack assist. Algorithm thought: The root node and all the children are pressed into the stack, then take out the right child pressed into the stack, and then explore their children //first sequence traversal void Preordertraver (Bstree &t) { stack S; iniystack (S); bstree p=T; while (p| |! Emptystack (S)) { if (P) { visit (P->data); push (s,p); p=p->lchild; } else{ pop (s,p); p=p->rchild; } } return OK;} //in sequence traversal void Inordertraverse (Bstree &t) { stcak s; initstack (S); BsTree p= T; while (p| |! Emptystqck (S)) { if (P) { push (s,p); p=p->lchild; } else{ pop (S,P); visit (P->data); p=p->rchild; } } return OK;} //post-traversal void Postordertraver (Bstree &t) { stack s; initstack (S); bstree P=t; while (p| |! Emptystack (S)) { if (p) { push ( S,P); p=p->lchild; } else{ if (P->rchild==NULL || Pre==p->rchild) { visit (T->data); pre=p; p=NULL; } else{ p=p->rchild; } } } return OK;} //hierarchy traverse binary tree with queue void Levelordertreaverse (Bstree &t) { queue q; initqueue (q); bstree P=t; enqueu (Q,p); while (!emptyQueue (q)) { dequeue (q,p); if (P) { visit (P->data); enqueue (Q,p-> Lchild); enqueue (Q,p->rchild);Nbsp; } } return OK;} ---------------------------------------------------------------------------------
Find a node xFinds whether a message is in a binary tree (ordinal recursive traversal) void searchch (Bstree t,char ch) {Bstree p=t; if (p) {if (p->data==ch) return p; else{searchch (T->LCHILD,CH); Searchch (T->RCHILD,CH); } }} ---------------------------------------------------------------------------------
inserting nodes, deleting nodes, swapping left and right subtrees
1. Inserting nodesFinds its parent node and then inserts it on the basis of the traversal.
2. Delete a nodeFind the node and delete it on the basis of the traversal.
3. Exchanging left and right subtreesRecursive method void Exchange (Bstree T) {Bstree item; if (T) {Exchange (T->lchild); Exchange (T->rchild); item=t->lchild; t->lchild=t->rchild; t->rchild=item; }}//non-recursive recursive method using non-recursive traversal switching------------------------------------------------------------------------------------
find the number of nodes/number of leaf nodes, the depth of the tree
1. Number of nodes in a treerecursively solves void Nodenum (Bstree T) {if (! T) return 0; else return Nodenum (t->lchild) +nodenum (T->rchild) +1;} Non-recursive solution using non-recursive traversal method to solve---------------------------------
2. Find the number of leaf nodes:Recursive solution void Leavenum (Bstree t) {if (t) {if (t->lchild==null&&t->rchild==null) return 1; else return Leavenum (t-lchild) +leavenum (t->rchild); }}//non-recursive solution using non-recursive traversal solver-----------------------------------
3. Find the depth of the binary treevoid Depth (Bstree T) {Bstree p=t; int lh=0,rh=0; if (!p) return 0; else{lh=depth (T->lchild); Rh=depth (T->rchild); Return (LH>RH?LH:RH) +1; }} ---------------------------------------------------------------------------------------------
Two fork sort tree basic operations