Data Structure and algorithm problems Binary Search Tree, data structure and algorithm binary
1. Order
Describes the operations of the Binary Search Tree in detail: inserting a node, constructing a binary tree, deleting a node, searching for and finding the maximum value, finding the minimum value, and searching for the precursor and successor of a specified node.
2. Introduction to Binary Search Tree
It is either an empty tree or a binary tree of the following nature: (1) if the left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of its root node; (2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node. (3) The left and right subtree are also binary sorting trees.
3. operations on the binary search tree
The code is provided here. The comments are very detailed. For details, refer to the Code:
# Include <iostream> using namespace std; typedef struct Binary_Search_Tree // The node Structure {int data; Binary_Search_Tree * lchild; Binary_Search_Tree * rchild; Binary_Search_Tree * parent;} Binary_Search_Tree; void insert (Binary_Search_Tree * & root, int data) // insert {Binary_Search_Tree * p = new Binary_Search_Tree; p-> data = data; p-> lchild = p-> rchild = p-> parent = NULL; if (root = NULL) // if it is an empty tree, that is, the insert node is the root node {root = p; return;} // insert to the current parent node Right node of the node if (root-> rchild = NULL & root-> data <data) {root-> rchild = p; p-> parent = root; return ;} // if (root-> lchild = NULL & root-> data) {p-> parent = root; root-> lchild = p; return;} if (root-> data) insert (root-> lchild, data); else if (root-> data <data) insert (root-> rchild, data); else return;} void create (Binary_Search_Tree * & root, int a [], int size) {root = NULL; for (int I = 0; I <size; I ++) insert (root, a [I] );} // Find the element and find the node pointer of the keyword returned. if not, return NULLBinary_Search_Tree * search (Binary_Search_Tree * & root, int data) {if (root = NULL) return NULL; if (data <root-> data) return search (root-> lchild, data); else if (data> root-> data) return search (root-> rchild, data); elsereturn root;} // recursively find the smallest element Binary_Search_Tree * searchmin (Binary_Search_Tree * & root) {if (root = NULL) return NULL; if (root-> lchild = NULL) return root; elsereturn searchm In (root-> lchild);} // recursively searches for the largest element Binary_Search_Tree * searchmax (Binary_Search_Tree * & root) {if (root = NULL) return NULL; if (root-> rchild = NULL) return root; elsereturn searchmax (root-> rchild);} // search for a node's precursor Binary_Search_Tree * seachpredecessor (Binary_Search_Tree * & p) {// empty tree if (p = NULL) return p; if (p-> lchild) return searchmax (p-> lchild); // No left subtree, after finding the right word tree of a node, else {if (p-> lchild = NULL) return NULL; // search for the precursor while (p) {I F (p-> parent-> rchild = p) break;} return p-> parent;} // find the successor Binary_Search_Tree * searchsuccessor (Binary_Search_Tree * & p) of an element) {if (p = NULL) return p; if (p-> rchild) return searchmin (p-> rchild); else {if (p-> rchild = NULL) return NULL; // search for the successor while (p) {if (p-> parent-> lchild = p) break;} return p-> parent ;}} // delete a node based on the keyword // if the root node is deleted, the address of the root node needs to be changed. Therefore, pass the second-level pointer void deletetree (Binary_Search_Tree * & root, int data) {Binary_Search_Tr Ee * q; Binary_Search_Tree * p = search (root, data); if (! P) return; // if no left or right subnode exists, delete if (p-> lchild = NULL & p-> rchild = NULL) {if (p-> parent = NULL) {delete p; root = NULL;} else {if (p = p-> parent-> lchild) p-> parent-> lchild = NULL; elsep-> parent-> rchild = NULL; delete p ;}// if there is a left node, no right node if (p-> lchild &&! (P-> rchild) {p-> lchild-> parent = p-> parent; if (p-> parent = NULL) root = p-> lchild; else if (p = p-> parent-> lchild) p-> parent-> lchild = p-> lchild; elsep-> parent-> rchild = p-> lchild; delete p;} // if there is a right node, there is no left node else if (p-> rchild &&! (P-> lchild) {p-> rchild-> parent = p-> parent; if (p-> parent = NULL) root = p-> rchild; else if (p-> parent-> lchild = p) p-> parent-> lchild = p-> rchild; elsep-> parent-> rchild = p-> rchild; delete p;} // if there are both left and right else if (p-> rchild & p-> lchild) {if (p-> parent = NULL) root = p-> rchild; if (p-> parent-> lchild = p) p-> parent-> lchild = p-> rchild; else if (p-> parent-> rchild = p) p-> parent-> rchild = p-> lchild; delete p ;} else {// find the successor q = searchsuccessor (p); int temp = q-> data; // Delete the successor node deletetree (root, q-> data ); p-> data = temp;} int main () {Binary_Search_Tree * root = NULL; int a [12] = {, 9 }; create (root, a, 11); Binary_Search_Tree * x = new Binary_Search_Tree; deletetree (root, 21); // delete node 21x = searchmin (root ); cout <x-> data; cout <endl; x = searchmax (root); cout <x-> data; cout <endl; return 0 ;}
Data Structure Algorithm Design of Binary Trees
Save the reference of the parent node in the node and use the breadth Traversal method to calculate the depth of each node (the child node is the parent node + 1)
At the same time, it is determined that the value is an invalid KEY. If yes, TRUE is returned. Otherwise, FALSE is returned.
Data Structure algorithms for Binary Trees
The number of bt nodes, the depth of the Binary Tree bt, and the number of leaf nodes of the Binary Tree bt are all returned to the caller as the return value of the function. No output is provided in the function, but no output is provided. The change is simple:
Cout <"(6) the number of nodes of the Binary Tree bt is:" <Count_BT (bt );
Cout <endl;
Cout <"(2) the depth of the Binary Tree bt is:" <Depth_BT (bt );
Cout <endl;
Cout <"(2) The number of leaf nodes of the Binary Tree bt is:" <Leafs_BT (bt );
OK.