Binary search Tree

Source: Internet
Author: User

Although the heap is appropriate in applications that require a priority queue, it does not apply to an application that removes arbitrary elements, the time overhead for removing arbitrary elements from a heap with n elements is O (n), and the time overhead of querying arbitrary elements is O (n), so when inserting, deleting, and finding operations, The performance of binary search tree is very suitable.
Binary search tree with the following properties:

  • Each element has a keyword, and the keywords are different for any two elements, that is, the keyword is unique.
  • The key value of a non-empty left subtree must be less than the key value of its sub-root node.
  • The key value of a non-empty right subtree must be greater than the key value of its sub-root node.
  • left, right subtree is still a two-fork find tree.
    a fork search tree in line with the above properties,

    Binary lookup Tree Lookup process: If you want to find a keyword key element, then from root to find, if the root is null, then this binary tree does not contain any elements, the lookup failed, otherwise, the key and root keyword comparison, if the value of key is equal to the root keyword value, If the lookup succeeds, if the key value is less than the root keyword value, then there is no element in the right subtree that is equal to key, so you only need to find the left subtree of root, if the key value is greater than the root keyword value, then you need to find the right subtree of root, whose time complexity is O ( l o g 2 N), the lookup process

    Recursive lookup implementations:
//递归二叉查找树查找tree_pointer search(tree_pointer root,int key){   if(!returnNULL;   if(root->data==key)      return root;   if(root->data>key)      return search(root->left_child,key);   else      return search(root->right_child,key);}

Iterative Lookup implementations:

//迭代二叉查找树查找tree_pointer search2(tree_pointer root,int key){   while(root)   {      if(root->data==key)         return root;      if(root->data>key)         root=root->left_child;      else         root=root->right_child;   }   returnNULL;}

Two fork Find tree insert:
To insert a new element with a key value of key, you must first confirm that the keyword is different from the key value of the existing element, to find the two-fork lookup tree, and if the lookup is unsuccessful, insert the element at the end of the query with a time complexity of O ( l o g 2 N).

Code implementation:

//If the tree is empty, or if the key value already exists, returns NULL, otherwise returns a pointer to the last node in the tree encountered during the lookupTree_pointer Modified_search (tree_pointer root,int key) {Tree_pointer Lastroot=NULL; while(Root) {Lastroot=Rootif(Root -Data==Keyreturn NULL;if(Root -Data>Key) Root=Root -Left_child;ElseRoot=Root -Right_child; }returnLastroot;}//insert OperationvoidInsert_node (Tree_pointer*Node,int num) {tree_pointer ptr,temp=Modified_search (*Node,num);if(Temp||!(*node) {PTR=(tree_pointer) malloc (sizeof (node)); Ptr -Data=Num Ptr -Left_child=Ptr -Right_child=NULL;if(*node) {if(PTR -Data<Temp -Data) Temp -Left_child=ptrElseTemp -Right_child=ptr }Else         *Node=ptr }}

Two fork Find tree Delete:
The deletion of binary search tree is also a common operation, its time complexity is O ( l o g 2 N), the following three scenarios and corresponding processing are considered for deleting the target node:

    • If the target node is a leaf node, delete it directly.
    • If the target node is only one child node, after the deletion, the unique child node is replaced with the original position.
    • If the target node node has two sons nodes, when deleted, the largest element in its left subtree or the smallest element of its right subtree is substituted for the node, and the alternate node is removed from its subtree.
      Code implementation:
//Delete element actionvoidDelete_node (tree_pointer node,int num) {if(!Nodereturn;if(num<Node -Data) Delete_node (node -Left_child,num);Else if(num>Node -Data) Delete_node (node -Right_child,num);//If the target has a two-node condition   Else if(node -Left_child!=NULL&&Node -Right_child!=NULL) {Tree_pointer Minnode=Node -Right_child;//Find the smallest node of the right sub-tree       while(Minnode -Left_child) Minnode=Minnode -Left_child; Node -Data=Minnode -Data; Delete_node (Minnode,minnode -Data); }//If the target node has a son node or no son node   Else{Tree_pointer Deletenode=Node Node=(node -Left_child!=NULL)?Node -Left_child:node -Right_child;   Free (deletenode); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Binary search Tree

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.