Detailed Implementation of Binary Search Tree

Source: Internet
Author: User
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:

/*************************************** * ********************************** This is a binary search tree, the following operations are performed: Insert a node, construct a binary tree, delete a node, search for the maximum value, find the minimum value, and find the precursor and successor of a specified node. The time complexity of all the above operations is O (h), where H is the detailed description of the tree, for more information, see the code ********************************** ***************************************/ # include <stdio. h> # include <stdlib. h> // Tree node description for Binary Search typedef int keytype; typedef struct node {keytype key; // keyword struct node * left; // left child pointer struct node * right; // right child pointer struct node * parent; // pointer to parent node} node, * pnode; // insert a node into the Binary Search Tree, the root node address may be changed, so the second-level pointer void inseart (pnode * root, keytype key ){ // Initialize the insert node pnode P = (pnode) malloc (sizeof (node); P-> key = key; p-> left = p-> right = p-> parent = NULL; // If (* root) = NULL), directly act as the root node) {* root = P; return;} // Insert the left child if (* root) to the current node (* root)-> left = NULL & (* root) -> key) {P-> parent = (* root); (* root)-> left = P; return;} // insert to the current node (* root) right child if (* root)-> right = NULL & (* root)-> key <key) {P-> parent = (* root ); (* root)-> right = P; return;} If (* root)-> key) inseart (& (* root)-> L EFT, key); else if (* root)-> key <key) inseart (& (* root)-> right, key); elsereturn;} // search for elements, locate the node pointer that returns the keyword, and fail to find the return nullpnode search (pnode root, keytype key) {If (root = NULL) return NULL; If (Key> root-> key) // search for the right subtree return search (root-> right, key); else if (Key <root-> key) // search for the left subtree return search (root-> left, key); elsereturn root;} // query the minimum keyword. If the tree is empty, nullpnode searchmin (pnode root) {If (root = NULL) return NULL; if (root-> left = Null) return root; else // search for the left child until the left child node does not return searchmin (root-> left);} // search for the maximum keyword, return nullpnode searchmax (pnode root) {If (root = NULL) return NULL; If (root-> right = NULL) return root; else // keep searching for the right child until the right child node does not return searchmax (root-> right);} // find the pnode searchpredecessor (pnode P) {// empty tree if (P = NULL) return P; // If (p-> left) return searchmax (p-> left); // No left subtree. After finding the right subtree of a node, the else {If (p-> Paren T = NULL) return NULL; // look up the precursor while (p) {If (p-> parent-> right = P) break; P = p-> parent;} return p-> parent;} // find the next pnode searchsuccessor (pnode P) of a node) {// empty tree if (P = NULL) return P; // If (p-> right) with the smallest of the right subtree) return searchmin (p-> right); // No right subtree. After finding the left subtree of a node, the else {If (p-> parent = NULL) returns NULL; // search for the successor while (p) {If (p-> parent-> left = P) break; P = p-> parent ;} return p-> parent ;}}// delete a node based on the keyword. If the node is successfully deleted, 1 is returned; otherwise, 0 is returned. // If Delete the root node and change the address of the root node. Therefore, the second-level pointer int deletenode (pnode * root, keytype key) {pnode Q; // find the node to be deleted pnode P = search (* root, key); keytype temp; // Save the value of the successor node // This keyword is not found if (! P) return 0; // 1. the deleted node is a leaf node and directly deletes if (p-> left = NULL & P-> right = NULL) {// only one element exists, after deletion, it becomes an empty tree if (p-> parent = NULL) {free (p); (* root) = NULL ;} else {// The deleted node is the left child of the parent node if (p-> parent-> left = P) P-> parent-> left = NULL; else // The deleted node is the right child of the parent node p-> parent-> right = NULL; free (p) ;}// 2. the deleted node only has the left subtree else if (p-> left &&! (P-> right) {P-> left-> parent = p-> parent; // If the deletion is a parent node, to change the parent node pointer if (p-> parent = NULL) * root = p-> left; // The deleted node is the parent node's left child else if (p-> parent-> left = P) P-> parent-> left = p-> left; else // The deleted node is the right child of the parent node p-> parent-> right = p-> left; free (p);} // 3. the deleted node only has the right child else if (p-> Right &&! (P-> left) {P-> right-> parent = p-> parent; // If the deletion is a parent node, to change the parent node pointer if (p-> parent = NULL) * root = p-> right; // The deleted node is the parent node's left child else if (p-> parent-> left = P) P-> parent-> left = p-> right; else // The deleted node is the right child of the parent node p-> parent-> right = p-> right; free (p);} // 4. the deleted node has both the left child and the Right child. // The successor node of the node certainly does not have the left child tree (refer to the above to find the successor node function) // Delete the successor node, the value of the successor node replaces this node else {// find the successor q = searchsuccessor (p); temp = Q-> key; // Delete the successor node deletenode (root, q-> key); P-> key = temp;} return 1;} // create a binary search tree void create (pnode * root, keytype * keyarray, int length) {int I; // insert one by one node into the binary tree for (I = 0; I <length; I ++) inseart (root, keyarray [I]);} int main (void) {int I; pnode root = NULL; keytype nodearray [11] = {15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9}; Create (& root, nodearray, 11); for (I = 0; I <2; I ++) deletenode (& root, nodearray [I]); printf ("% d \ n ", searchpredecessor (Root)-> key); printf ("% d \ n", searchsuccessor (Root)-> key); printf ("% d \ n", searchmin (Root) -> key); printf ("% d \ n", searchmax (Root)-> key); printf ("% d \ n", search (root, 13) -> key); Return 0 ;}
4. Appendix

Introduction to Algorithms

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.