Build a binary tree, search nodes, and delete nodes C and C ++

Source: Internet
Author: User

The program is to create a binary sorting tree, find the node to return its parent node, return NULL when the failure, delete the node is divided into four situations: 1. Left subtree and right subtree are empty; 2. Left subtree is empty, right subtree is not empty; 3. Left subtree is not empty, right subtree is empty; 4. Left and right subtree are not empty.

C language version (implemented using struct ):

# Include <stdio. h> # include <stdlib. h> typedef int DataType; typedef struct BTree {DataType data; struct BTree * Tleft; struct BTree * Tright;} * BTree; BTree CreateTree (); // create a BTree insert (BTree root, DataType data); // insert the node void InBTree (BTree root); // traverse void PreBTree (BTree root) in the middle order ); // first traverse void PostBTree (BTree root); // then traverse BTree findPostion (BTree root, int deleteNode, int * flags ); // find the appropriate insertion point BTree delNode (B Tree root, BTree parent, int flags); // Delete the Tree node int main () {BTree root = NULL; int flags = 0; int deleteNode = 0; BTree parent = NULL; // char choiceAgain = 'y'; root = CreateTree (); printf ("\ n sequential traversal:"); InBTree (root ); printf ("\ n pre-order traversal:"); PreBTree (root); printf ("\ n post-order traversal:"); PostBTree (root ); printf ("\ n"); do {printf ("node to be deleted:"); scanf ("% d", & deleteNode); parent = findPostion (root, deleteNode, & flags); ro Ot = delNode (root, parent, flags); printf ("result after deletion:"); printf ("\ n sequential traversal:"); InBTree (root ); printf ("\ n pre-order traversal:"); PreBTree (root); printf ("\ n post-order traversal:"); PostBTree (root); choiceAgain = 'n '; printf ("\ nDelete Again (Y) or N?: "); Getchar (); scanf (" % c ", & choiceAgain);} while (choiceAgain = 'y' | choiceAgain = 'y '); printf ("\ nDone! \ N "); return 0;} BTree CreateTree () {BTree root = NULL; DataType temp = 0; printf (" enter a node, ending with 0: \ n "); scanf ("% d", & temp); while (temp! = 0) {root = insert (root, temp); scanf ("% d", & temp);} return root;} BTree insert (BTree root, DataType data) {BTree ptr = root; BTree tempNode; BTree newNode = (BTree) malloc (sizeof (struct BTree); newNode-> data = data; newNode-> Tleft = NULL; newNode-> Tright = NULL; if (ptr = NULL) {return newNode;} else {while (ptr! = NULL) {tempNode = ptr; if (ptr-> data> = data) {ptr = ptr-> Tleft;} else {ptr = ptr-> Tright ;}} if (tempNode-> data> = data) {tempNode-> Tleft = newNode;} else {tempNode-> Tright = newNode;} return root;} BTree findPostion (BTree root, int deleteNode, int * flags) {BTree parentNode = root; BTree temp = root; * flags = 0; while (temp! = NULL) {if (temp-> data = deleteNode) {return parentNode;} else {parentNode = temp; if (temp-> data> deleteNode) {temp = temp-> Tleft; * flags =-1;} else {temp = temp-> Tright; * flags = 1 ;}} return NULL ;} BTree delNode (BTree root, BTree parent, int flags) {BTree deleteNode = NULL; if (parent = NULL) {printf ("deleted node not found! \ N "); return root;} switch (flags) {case-1: deleteNode = parent-> Tleft; break; case 0: deleteNode = parent; break; case 1: deleteNode = parent-> Tright; break; default: printf ("ERROR! \ N "); exit (1);} if (deleteNode-> Tleft = NULL & deleteNode-> Tright = NULL) {if (parent-> Tleft = deleteNode) {parent-> Tleft = NULL;} else if (parent-> Tright = deleteNode) {parent-> Tright = NULL ;} else {parent = NULL;} free (deleteNode);} else if (deleteNode-> Tleft = NULL & deleteNode-> Tright! = NULL) {if (deleteNode-> data = root-> data) {root = deleteNode-> Tright ;} else {if (parent-> Tleft-> data = deleteNode-> data) {parent-> Tleft = deleteNode-> Tright ;} else {parent-> Tright = deleteNode-> Tright;} free (deleteNode);} else if (deleteNode-> Tleft! = NULL & deleteNode-> Tright = NULL) {if (deleteNode-> data = root-> data) {root = deleteNode-> Tleft ;} else {if (parent-> Tleft-> data = deleteNode-> data) {parent-> Tleft = deleteNode-> Tleft ;} else {parent-> Tright = deleteNode-> Tleft ;}} free (deleteNode);} else {BTree temp = deleteNode-> Tleft; BTree tempParent = deleteNode; while (temp-> Tright! = NULL) {tempParent = temp; temp = temp-> Tright;} deleteNode-> data = temp-> data; if (tempParent-> Tleft = temp) {tempParent-> Tleft = temp-> Tleft;} else {tempParent-> Tright = temp-> Tleft;} printf ("temp = % d \ n ", temp-> data); free (temp);} return root;} void InBTree (BTree root) {if (root! = NULL) {InBTree (root-> Tleft); printf ("% d", root-> data); InBTree (root-> Tright );}} void PreBTree (BTree root) {if (root! = NULL) {printf ("% d", root-> data); PreBTree (root-> Tleft); PreBTree (root-> Tright );}} void PostBTree (BTree root) {if (root! = NULL) {PostBTree (root-> Tleft); PostBTree (root-> Tright); printf ("% d", root-> data );}}

C ++ version (implemented by class)

# Include <iostream. h ># include <cstring> typedef int keytype; # define num 11 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); void bstreeinsert (binstreenode * & BT, keytype K ); int bstreedelete (binstreenode * & BT, keytype K); void bstreepreorder (binstreenode * BT); bool isempty () {return root = NULL ;}}; /*** Binary Tree sorting search algorithm * searches for the node of element K in the binary tree with the root pointer Bt. if the search is successful, returns the pointer to this node * parameter P points to the found node; otherwise, a null pointer is returned. parameter P points to the parent node that K should insert */binstreenode * bi Nstree: bstreesearch (binstreenode * BT, keytype K, binstreenode * & P) {binstreenode * q = NULL; q = Bt; while (q) {P = Q; if (Q-> key = k) {return (p);} If (Q-> key> K) q = Q-> lchild; else q = Q-> rchild;} return NULL;}/*** insert node algorithm of the binary tree * BT points to the root node of the binary tree, insert the node of element K */void binstree: bstreeinsert (binstreenode * & BT, keytype K) {binstreenode * P = NULL, * q; q = Bt; if (bstreesearch (Q, K, P) = NULL) {Binstreenode * r = new binstreenode; r-> key = K; r-> lchild = r-> rchild = NULL; If (q = NULL) {bt = R; // The inserted node is the root node of the tree.} If (P & K <p-> key) P-> lchild = r; else if (P) p-> rchild = r ;}}/*** sequential traversal */void binstree: bstreepreorder (binstreenode * BT) {If (BT! = NULL) {cout <Bt-> key <"; bstreepreorder (BT-> lchild); bstreepreorder (BT-> rchild );}} /*** Node Deletion algorithm of the Binary Tree **: delete nodes whose elements are K in the binary tree. * BT points to the root node of the binary tree * 1 is returned after deletion, 0 is returned if no operation is successful. */INT binstree: bstreedelete (binstreenode * & BT, keytype K) {binstreenode * F, * P, * q, * s; P = Bt; F = NULL; // search for the node with the keyword K and find out the two sides of the node while (P & P-> key! = K) {f = P; If (p-> key> K) P = p-> lchild; else P = p-> rchild;} If (P = NULL) // return 0 if the node to be deleted cannot be found; If (p-> lchild = NULL) // The left subtree of the node to be deleted is null {If (F = NULL) // The node to be deleted is the root node bt = p-> rchild; else if (F-> lchild = P) // The node to be deleted is the left node of the parent node F-> lchild = p-> rchild; else F-> rchild = p-> rchild; // The node to be deleted is the right node Delete P of the parent node ;} else // The node to be deleted has the left subtree {q = P; S = p-> lchild; while (S-> rchild) // find the rightmost node {q = s; S = s-> rchild;} If (q = P) in the left subtree of the node to be deleted) q-> lchild = s-> lchild; else Q-> rchild = s-> lchild; P-> key = s-> key; Delete s;} return 1 ;} int main (void) {int A [num] = {34, 18, 76, 13, 52, 82, 16, 67, 58, 73, 72}; int I; binstree BST; binstreenode * PBT = NULL, * P = NULL, * PT = NULL; for (I = 0; I <num; I ++) {BST. bstreeinsert (PBT, a [I]); // create a binary sorting tree} Pt = BST. bstreesearch (PBT, 52, p); // search for the sort binary tree BST. bstreepreorder (PBT); cout <Endl; BST. bstreedelete (PBT, 13); // Delete BST without left children. bstreepreorder (PBT); cout <Endl; BST. bstreedelete (PBT, 76); // Delete the BST when the left child exists. bstreepreorder (PBT); cout <Endl; return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.