Binary Lookup Tree _ binary Sort tree _ binary search tree _c++

Source: Internet
Author: User

First, data structure background + code variable introduction

  Binary search tree, also known as binary sorting tree, also known as two-fork tree

It satisfies the following definitions:

1, the subtree of any node is a binary search tree, and each node of the left subtree is smaller than that node, each node of the right subtree is larger than that node.

2, by 1 can be launched, any node of the left child is less than the node, the right child is greater than the node

The above discussion is about the presence of the left (right) child (subtree)

Its middle order traversal is an ascending sort

In the reference code, we define:

In the main program, K represents the value of the node being inserted or deleted or found

Root, root node position; a[i], Value of node I, Cl[i], position of left child of node I, Cr[i], position of right child of node I, Fa[i], Father node position;

Second, middle sequence traversal

The method of the middle sequence traversal uses recursion, recursion its left child first, then prints the current node, and finally recursion its right child (recursive when left or right child exists)

  

If the middle order traversal is DBEAFC

Time complexity O (n)

 1  void  Mid ( int   X)  2  3  if  (Time[cl[x]]!=0  ) mid (cl[x]);  4  cout<<a[x]<<  " ;  5  if  (Time[cr[x]]!=0  ) mid (cr[x]);  6   7  

Third, insert the node

We adopt the method of encountering the same node, making the node counter +1

Start with the root node:

1, if the current node is less than the number of insertions, then recursion into their left son

If the left son counter is 0, i.e. there is no left son, the number is inserted directly into the left son

2, if the current node is greater than the number of insertions, then recursion into their right son

If the right son counter is 0, i.e. there is no right son, the number is inserted directly into the right son.

3. If the current node equals the number of insertions, the node counter +1 is directly

Sample One

For example, insert 6 into the two fork tree

1, compared with the root node, the small root node, into the left son

2, compared with 2, greater than 2, into the right son

3, compared with 5, greater than 5, into the right son, but because the right son does not exist, so 6 as their right son

Example Two

  For example, insert 1 into the two fork tree:

1, compared with the root node, less than 8, into the left son

2, compared with 2, less than 2, into the left son, but because his left son does not exist, so 1 as its left son

Time complexity O (log2n)

1 voidInsintIintx)2 {3     if(root==0)4     {5a[1]=x;6time[1]=root=1;7         return;8     }9     if(x<A[i])Ten     { One         if(time[cl[i]]==0) A         { -cl[i]=top>0?s[top--]:++tail; -a[cl[i]]=x; thefa[cl[i]]=i; -time[cl[i]]++; -cr[cl[i]]=cl[cl[i]]=0; -         } +         Elseins (cl[i],x); -     } +     Else if(x>A[i]) A     { at         if(time[cr[i]]==0) -         { -cr[i]=top>0?s[top--]:++tail; -a[cr[i]]=x; -fa[cr[i]]=i; -time[cr[i]]++; incr[cr[i]]=cl[cr[i]]=0; -         } to         Elseins (cr[i],x); +     } -     Else the     { *time[i]++; $     }Panax Notoginseng } -  theIn the main program: Ser (root,k); Root represents the starting position, and K represents the inserted value

Iv. Finding nodes

Looks similar to insert

Also from the root node, if you encounter an empty node, that is, the counter equals 0 node, directly return 0, indicating no found

If the node equals the value of the lookup, the location of the node is returned

If the node is larger than the lookup value, the left son is recursively looking for

If the node is less than the lookup value, the right son looks for it recursively

It is shown here that if the current node is empty, then the value of the father of the node is less than (greater than) that does not exist, i.e. not found

Looking for a line-like tree, it is a step down to narrow the range until you find the desired value or no result

Sample One

For example, the value to be looked for is 5

1. Find root node, 5<8, go to left son

2, 5>2, into the right son

3, 5=5, find the node, return the location of the node

If you want to find a value of 4 o'clock, in the 3rd step will enter its left son, and because the left son is empty node, then return 0, indicating not found

Time complexity O (log2n)

1 intSeaintIintx)2 {3     if(time[i]==0)return 0;4     if(a[i]==x)returni;5     Else if(a[i]>x)returnser (cl[i],x);6     Else returnser (cr[i],x);7 }8 9In the main program: Sea (ROOT,K); Root represents the starting position of the lookup, and K represents the value of the lookup

V. Deleting nodes

When you need to delete a node, look for the location where the change is located before deleting it

If the node counter is greater than 1, use only the counter-1

If the counter is equal to 1, that is, the node no longer exists after deletion, it is divided into three cases depending on the number of sons:

1. No son: delete the node directly

2. A son: The only son who points directly to the node's father's left (right) son

3, two sons: look for a minimum value in the right subtree of the node, then replace the node with the smallest node, and finally delete the smallest node

Find the Minimum node specific method: First point the current node to the right son, and then look for the left son who constantly points to the current node, until there is no left son so far

Because the smallest node that needs to be removed must not have a left son, simply recursively perform the 1th or 2nd case, the Del (minimum node position, minimum node value), please refer to the code for details.

Sample One

Delete 9 node: Call Find, find to 9 location, because it has no son, can be deleted directly

Example Two

Delete 5 node: Also find the location of the 5 node, found that it has 1 children, 5 nodes of the Father 2 node of the right child point to the 5 node child 6 node, so that the 5 node is automatically deleted

Example Three

To delete a 2 node:

1. Find the location of the 2 node

2. Point the smallest node to its right child

3, constantly looking for the smallest node of the left child, until the end, find the smallest node

4. Assign the minimum node information to the 2 node

5. Delete the minimum node 4 node according to the method of deleting the node

Time complexity O (log2n)

1 voidDelintKintx)2 {3     intct=0;4     if(cl[k]!=0) ct++;5     if(cr[k]!=0) ct++;6time[k]--;7     if(time[k]==0)8     {9         if(ct==1)Ten         { OneS[++top]=k; A             if(k==root) -             { -root=time[cr[k]]==0?Cl[k]:cr[k]; the                 return; -             } -             if(X<a[fa[k]]) cl[fa[k]]=time[cr[k]]==0?Cl[k]:cr[k]; -             Elsecr[fa[k]]=time[cr[k]]==0?Cl[k]:cr[k]; +         } -         Else if(ct==2) +         { A             ints=Cr[k]; at             if(time[cl[s]]!=0) s=Cl[s]; -time[k]=Time[s]; -a[k]=A[s]; - del (S,a[s]); -         } -         ElseS[++top]=k; in     } - } to  +Main program: Del (Sea (ROOT,K), k); First find the location of the node you want to delete, and then delete the K

Six, empty node position stack

Many empty nodes are generated when the node is deleted

In order to save space, we will open a stack to save the location of the empty node, when a node is deleted, the location of its empty position into the stack

When inserting a node, first ask if the top of the stack is greater than 0, if there are elements in the stack, then the position in the stack pops up, with the location of the node

If there is no element in the stack, indicating that there is no space in front of it, a new room is opened to hold the node, and the maximum position is controlled by the variable tail that holds the maximum number of positions.

Vii. Special Circumstances

  For binary search tree will produce the situation as

The time of all operations will often be stuck into linear O (n), and the use of balanced binary trees, stretching trees, red and black trees and other data structures will help us solve this problem

Copyright, reprint please contact the author, offenders must investigate

qq:740929894

Binary Lookup Tree _ binary Sort tree _ binary search tree _c++

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.