Explanation of Binary Search Tree (Introduction to algorithms)

Source: Internet
Author: User
Document directory
  • Tree search
  • Maximum and minimum keyword Elements
  • Node precursor and successor
  • Insert
What is a binary search tree?

If y is a node in the left subtree of X, then Y. key <X. key. if y is a node in the right subtree of X, then Y. key> X. key

Binary Tree Data Structure

typedef int data_t;typedef struct BST{data_t  data;struct BST *left;struct BST *right;struct BST *parent;}BST;

Binary Search tree traversal

We can use a simple recursive algorithm to output all the keywords in the binary search tree in order. This algorithm is called in-order traversal. Similarly, first, traverse the output root keywords in sequence between the left and right subtree keywords, and then traverse the output root values after the left and right subtree values

The following are the recursive and non-Recursive Algorithms for sequential traversal of Binary Trees.

void bst_nonrecur_inorder(BST *root)//use stack to store tree's node {stack<BST *> s_bst;while(s_bst.size() || root!=NULL){if(root!=NULL){s_bst.push(root);root = root->left;}else{root = s_bst.top();s_bst.pop();cout << root->data << " ";root = root->right;}}}void bst_inorder(BST *root){if(root==NULL)return ;else{bst_inorder(root->left);cout << root->data << " ";bst_inorder(root->right);}}

NOTE: If X is the root of N node tree, it takes O (n) Time to traverse the tree.

Binary Tree Query

We often need to find a keyword stored in the binary search tree. In addition to search operations, query operations such as minnum, maxinum, successor, and predecessor are also supported.

Tree search input pointer to the root node and a keyword K. If this checkpoint exists, bst_search returns a pointer to the key K node, otherwise, the return value of null is the Recursive Implementation and iterative Implementation of tree search.
//search a node in the binary-search-treeBST *bst_search(BST *root, data_t data){if(root==NULL || root->data== data)return root;if(data<root->data)return bst_search(root->left, data);elsereturn bst_search(root->right, data);}BST *bst_iterative_search(BST *root, data_t data){if(root==NULL || root->data == data)return root;while(root!=NULL && data!=root->data){if(data<root->data)root = root->left;else if(data>root->data)root = root->right;}return root;}

The maximum keyword element and the minimum keyword element start from the root of the tree along the left child pointer until the left pointer of a node is null, then the value of this node is the minimum keyword of the Binary Search Tree. Similarly, from the root of the tree along the right child pointer until the right pointer of a node is null, the value of the node is the maximum keyword of the binary search tree. The following is the implementation code.

//return the minnest node of treeBST *bst_mininum(BST *root){if(root == NULL)return NULL;while(root->left!=NULL)root = root->left;return root;}//return the maxest node of treeBST *bst_maxinum(BST *root){if(root == NULL)return NULL;while(root->right!=NULL)root = root->right;return root;}

Note: the time complexity of the preceding two algorithms is O (logn) (N is the number of lost nodes)

Given a node of a binary search tree, the forward and successor of a node sometimes need to search for its successor in the order of central traversal. If all the keywords are different, the successor of node X is the node with the smallest keyword greater than the key. The search process consists of two parts: 1. if the right subtree of X is not empty, the successor of X is the leftmost node of the right subtree of X; 2. if the right subtree of X is empty and has a successor node y, Y is the bottom-layer ancestor and Its left child is also an ancestor. To find y, you only need to start from X along the tree and know the front of a node with its parent and child left to search for the node, first determine whether the left subtree of the x node is empty, if it is not empty, the frontend is the rightmost node of the Left subtree. If it is null, it goes from X along the tree until it encounters a node with both its parents having right children, this node is the implementation code below the X precursor node.
BST *bst_successor(BST *node){if(node == NULL)return NULL;if(node->right!=NULL)//find successor in the leftest of the right subtreereturn bst_mininum(node->right);//else find it a node leftSubTree from his parentBST *y = node->parent;while(y!=NULL && node==y->right){node = y;y = y->parent;}return y;}BST *bst_predecessor(BST *node){if(node == NULL)return NULL;if(node->left!=NULL)return bst_maxinum(node->left);BST *y = node->parent;while(y!=NULL && node==y->left){node = y;y = y->parent;}return y;}
Tree insertion and Deletion

The Tree insertion and deletion operations will change the dynamic set represented by the binary search tree. You must modify the data structure to reflect this change. However, the modification must ensure that the binary tree search tree is of a valid nature.

To insert a new value to a binary search tree T, you must call bst_insert. This function uses the keyword V as the input. In the function, construct the node and insert node-> left = NULL, node-> right = NULL to start from the root of the tree, the pointer node records a simple downward track and finds the null value of the insert statement to replace the New node. This traversal keeps TMO as the parent node.
//insert a node into a binary-search-treeBST *bst_insert(BST *root, data_t data){BST *insert = NULL, *node = NULL, *tmp = NULL;insert = bst_newnode(data);  //make a node to insert  node = root;while(node)    //find pos to insert{tmp = node;if(data < node->data)node = node->left;elsenode = node->right;}insert->parent = tmp;if(tmp==NULL)    //tree root is emptyroot = insert;else {if(data < tmp->data) tmp->left = insert;elsetmp->right = insert;}return root;}

Tree deletion removes a node Z from a binary search tree T. This process depends on the process of pointing to the root node and the node to be deleted. The following are four situations of this process. if the node does not have a left child, its right child will replace the node. The right child can be null or not 2. if a node has only one node and is a left child node, use its left child to replace Node 3. otherwise, the node has both a left child and a right child. We need to find the node's successor y, which is located in the right subtree of the node and has no left child, now you need to remove y from the original position and splice it with node 3.1 in the tree. if y is the right child of node Z, replace node 3.2 with Y. Otherwise, Y is located in the right subtree of Z but not the right child of Z. In this case, replace y with the right child of Y and Y with node (the last step is the same as 3.1). To move the subtree in the binary search tree, define a function bst_transplant, it replaces a word with a cooly subtree and becomes a child node of both parents. The following is to replace a subtree with a V root instead of a U root. The dual-parent of node u becomes the dual-parent of node v, and v becomes the parent child of U.

//replace subtree of u with subtree of vBST *bst_transplant(BST *root, BST *u, BST *v){if(u->parent==NULL)   //u is root root = v;else if(u->parent->right == u)  // u is his parent's right subtreeu->parent->right = v;elseu->parent->left = v;   //u is his parent's left subtreeif(v!=NULL)   //set v's parentv->parent = u->parent;return root;}
The tree deletion code is as follows:
BST *bst_delete(BST *root, data_t data){BST *node = NULL, *y = NULL;node = bst_search(root, data);  //find the deleted nodeif(node==NULL)return NULL;if(node->left == NULL) //case 1root = bst_transplant(root, node, node->right);else if(node->right == NULL)//case 2root = bst_transplant(root, node, node->left);else {y = bst_mininum(node->right);  //find node's successorif(y->parent!=node)         //case 3  follow  make y's parent to node{root = bst_transplant(root, y, y->right);y->right = node->right;node->right->parent = y;}                            //case 3 y->parent == noderoot = bst_transplant(root, node, y);y->left = node->left;y->left->parent = y;}return root;}

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.