Binary tree operating system (binary sort tree) header file section: #define INF 0xffffffvoid insert_btree (BTree *root,datatype data) {BTree s;if (*root==null)// Understand the idea of writing code like this? {s= (Btnode *) malloc (sizeof (Btnode)); When to use a level two pointer or reference type in a non-initialized function parameter s->data=data; S->lchild=null;s->rchild=null;*root=s;} else if ((*root)->data>data) Insert_btree (& ((*root)->lchild), data); else if ((*root)->data<data) Insert_btree (& (*root)->rchild), data);} int Search_btree (BTree root,datatype data)//Two fork sort tree find operation {btnode *p;p=root;while (p) {if (p->data==data) return p-> Data;if (P->data>data) P=p->lchild;elsep=p->rchild;} return INF;} void Create_btree (BTree *root) {int i,data,cnt;*root=null;printf ("Please enter the number of binary sort tree nodes: \ n"), scanf ("%d", &cnt);p rintf (" Please enter data for each node, separated by a space between adjacent data: \ n "), for (i=1;i<=cnt;i++) {scanf ("%d ", &data); Insert_btree (Root,data);}} void Delete_btree (BTree root,datatype data) {BTree p,father,sc,q;p=root;father=null; while (p)//Match element data or p==null when jumping out of loop {if (P->data==data)//Match P is the node to be deleted Break;fathEr=p; Father always holds the parent node of P if (p->data>data) P=p->lchild;else if (p->data<data) p=p->rchild;} if (p==null)//Start category Discussion {printf ("element does not exist, delete failed!!! \ n "); return;} if (p->lchild==null)//delete with insert still to conform to the nature of the binary sort tree {if (father->lchild==p) father->lchild=p->rchild;elsefather- >rchild=p->rchild;free (P);} Else//p's left subtree is not empty {//since P left dial hand tree is not empty, I am sure to find Zuozi's maximum node q=p; or the smallest node of the right subtree, but Sc=p->lchild;while (sc->rchild)//This way until SC is a leaf node or only Zuozi node {//Final SC is the most of P Zuozi Large node q=sc;sc=sc->rchild; }if (q==p)//At this point the right subtree of the P node is empty Q->lchild=sc->lchild;else//The right subtree of the left subtree of P is not empty at this time,q->rchild=sc->lchild; Think about why this is so that we can maintain the nature of the binary sort Tree P->data=sc->data;free (SC);}} void In_order_rec_bst (BTree root) {if (root) {In_order_rec_bst (root->lchild);p rintf ("%d", root->data); In_order _rec_bst (Root->rchild);}} void Fuction_btree (BTree root) {int Data,chioce,t;system ("cls");p rintf ("\ t two fork sort tree application \ n"); printf ("\ t 1. Create two Forks\ n "); printf ("\ t 2. Search node \ n "); printf ("\ t 3. Insert node \ n "); printf ("\ t 4. Delete node \ n "); printf ("\ t 0. Exit \ n "); printf ("Please enter the appropriate action: \ n"); scanf ("%d", &CHIOCE); while (!) ( chioce>=0&&chioce<=4)//input serial number does not exist, operation error {printf ("Input problem, please re-enter:"); scanf ("%d", &CHIOCE); } while (chioce!=0) {//build cycle Select different operation switch (CHIOCE) {Case 1://Select Operation 1 is the creation of the binary sort tree printf ("Create two fork tree: \ n"); Create_btree (&root); In_order_rec_bst (root); printf ("\ n"); Break Case 2://Select operation 2 to find printf in the binary sort tree ("Please enter the node storage element information you want to search: \ n"); scanf ("%d", &data); T=search_btree (Root,data); if (t!=inf) {printf ("Find success:"); printf ("%d\n", t); } else printf ("keyword does not exist \ n"); Break;case 3://Select operation 3 to insert in binary sort tree printf ("Please enter the node storage element information you want to insert: \ n"); scanf ("%d", &data); Insert_btree (&root,data); In_order_rec_bst (root); printf ("\ n"); Break Case 4://Select operation 4 is the delete operation in the binary sort tree printf ("Please enter the node storage information you want to delete: \ n"); scanf ("%d", &data); Delete_btree (Root,data); In_order_rec_bst (root); printf ("\ n"); Break } printf ("Please proceed with the appropriate operation:"); scanf ("%d", &CHIOCE); } return; }
Course design of data structure-two tree operating system