/*************************************** * ********************************* Five classic search (5): Use the binary sorting tree -- search (1) binary sorting tree generation (2) the central order traversal of the Binary Tree LDR_BiTreeTraverse (3) insertion of the binary sorting tree (4) *********************************** * **********************************/# include
Typedef struct Tree_Node // defines the binary sorting Tree Structure {int data; struct Tree_Node * plchild; struct Tree_Node * prchild;} BSTreeNode; // insert a binary sorting tree // T is the parent node void InsertBSTree (BSTreeNode * FatherNode, int key, bool & flag) {if (FatherNode = NULL) // determine whether the current parent node is empty return; if (key <FatherNode-> data) // if the key <parent node InsertBSTree (FatherNode-> plchild, key, flag ); // insert to the left subtree else InsertBSTree (FatherNode-> prchild, key, flag); // insert to the right subtree if (! Flag) {// construct a new node and use the Key to initialize BSTreeNode * p = new BSTreeNode; if (NULL = p) return; p-> data = key; p-> plchild = NULL; p-> prchild = NULL; // create a node if (key <FatherNode-> data) using the parent node link // key
<父节点 新建p为左孩子节点fathernode->
Plchild = p; elseFatherNode-> prchild = p; flag = true ;}// create a binary sorting tree void CreateBSTree (BSTreeNode * & RootNode, int a [], int len) {if (RootNode = NULL) // construct the root node {RootNode = new BSTreeNode; if (RootNode = NULL) return; rootNode-> data = a [0]; RootNode-> plchild = NULL; RootNode-> prchild = NULL;} bool flag; for (int I = 1; I
Plchild); printf ("% d", RootNode-> data); LDR_BiTreeTraverse (RootNode-> prchild) ;}// search binary sorting tree BSTreeNode * SearchBST (BSTreeNode * RootNode, int key) {if (! RootNode) // return NULL; if (key = RootNode-> data) return RootNode; else {if (key <RootNode-> data) return SearchBST (RootNode-> plchild, key); else return SearchBST (RootNode-> prchild, key) ;}// Delete the specified node void DeleteNode (BSTreeNode * p) {BSTreeNode * q; // if (p-> plchild = NULL), you can delete a leaf node or connect only the left or right subtree directly) // If the left subtree of the deleted node is empty. {Q = p; p = p-> prchild; // re-link the right subtree delete q;} else {if (p-> prchild = NULL) // if the right subtree of the deleted node is empty. {Q = p; p = p-> plchild; // re-link the left subtree delete q;} else // The left and right subtree do not have a blank lookup precursor pointer {q = p; BSTreeNode * cur = p-> plchild; // cur points to the left subtree of the deleted node while (cur-> prchild) // search for the precursor {q = cur; cur = cur-> prchild;} p-> data = cur-> data that traverses the rightmost node in the left subtree of the deleted node in the middle order; // Delete the data of the specified node if (p! = Q) q-> prchild = cur-> plchild; // re-connect the right subtree else of q // p = q indicates that cur is the leaf node q-> plchild = cur-> plchild; // re-connect the left subtree of q to delete cur ;}}// print the original array void PrintArray (int a [], int len) {for (int I = 0; I