Binary sort Tree

Source: Internet
Author: User

1 Concepts

Also known as the two-fork search tree , referred to as BST. A two-fork tree with the following properties:

(1) Joz tree is not empty, then all node values on the left subtree are smaller than the value of the root node.

(2) If the right subtree is not empty, then all node values on the right subtree are greater than the value of the root node.

(3) The left and right sub-tree itself is a binary sort tree, respectively.

is also a recursive data structure. is a binary sort tree.

The sequential traversal is performed to obtain an ascending ordered sequence, such as the sequence of 2,4,5,6,7,8,9,10,11,14.

22 Fork Sort Tree Insert

Binary sort tree is a dynamic set, the process of inserting nodes is, the node value is less than the root node value, inserted into the left subtree, if the large root node value, then inserted into the right subtree. The new node inserted must be a leaf node .

/*** Insert a node with a value of info in the binary sort tree node *@paramnode two fork sort tree *@paraminfo to insert node value *@return */ Publicnode Insert (node node, Integer info) {if(node = =NULL) {node=NewNode (); Node.data=info; }    if(Info <node.data) {Node.lchild=Insert (Node.lchild, info); } Else if(Info >node.data) {Node.rchild=Insert (Node.rchild, info); } Else { }    returnnode;}
32 Fork Sorting Tree Lookup

Starting from the root node, if the given value equals the root node value, the lookup succeeds; If the root node value is small, find it in the left subtree, otherwise find it in the right sub-tree. Can be divided into recursive and non-recursive implementation.

3.1 Recursive implementations
/*** Binary sort tree, find, recursive implementation *@paramRoot *@paraminte *@return-1 does not exist 1 exists*/ Public intsearchrecur (Node root, Integer inte) {if(root!=NULL){        if(inte = =root.data) {            return1; } Else if(Inte <root.data) {            returnsearchrecur (Root.lchild, inte); } Else if(Inte >root.data) {            returnsearchrecur (Root.rchild, inte); }     }    return-1;}
3.2 Non-recursive implementations
/*** Lookup, non-recursive implementation *@return-1 cannot exist 1 exists*/ Public intSearch (Node root, Integer inte) { while(Root! =NULL&& inte! =root.data) {        if(Inte <root.data) {Root=Root.lchild; } Else{root=Root.rchild; }    }    returnRoot = =NULL? -1:1;}
construction of a 42-fork sorting Tree

Enter the data element in turn and insert it into the appropriate position, the process is that each read into an element, establish a new node, if the new node value is less than the root node value, then inserted into the left subtree, or inserted into the right sub-tree.

 Public classBST {Node root; intN=1;//Total node points    Private classnode{Integer data;    Node Lchild, Rchild; }     PublicBST () {build ();//Recursive constructionSYSTEM.OUT.PRINTLN ("root node:" +root.data);    Btdepth (); }    /*** Construct a binary sort tree*/     Public voidbuild () {//integer[] ins = new integer[] {8, 4, 9, 2, 7, 5, 6};integer[] ins =NewInteger[] {8, 4, 2, 7, 5, 6, 16, 10, 9, 14, 15, 11, 12}; Root=NewNode (); Root.data= Ins[0];  for(inti = 1; i < ins.length; i++{Insert (root, ins[i]); N++; }    }}
52 Fork Sorting tree removal

When deleting a node, you must ensure that the nature of the binary sort tree is not lost. The implementation of the delete operation is handled in 3 different situations:

(1) If the deletion of the node n is a leaf node, then delete directly, will not break the binary sorting tree nature;

(2) If the deleted node n has only one left subtree or right subtree, the left or right child is substituted;

(3) If the deleted node n is not empty, then the direct successor (or direct precursor) of n is substituted for n, then the direct successor (or precursor) is removed.

/*** Find the node and its parent nodes first, then delete them according to the rules .@paramroot root node *@paramdel Delete node value *@return-1 node does not exist 1 delete succeeded*/ Public intDelete (node root, Integer del) {node pre=NULL;//parent node of the node to be deleted    /** First find the node.*/     while(Root! =NULL&& del! =root.data) {Pre=Root; if(Del <root.data) {Root=Root.lchild; } Else{root=Root.rchild; }    }    if(Root = =NULL){//If there is no direct return        return-1;    } Delete (root, pre); return1;}/*** Delete Nodes in three cases * <ul> * <li> If the deleted node n is a leaf node, then delete it directly; * <li> if the deleted node n has only one left subtree or right subtree, the left or right child is replaced; * <li& Gt If the deleted node n is not empty, then the direct successor (or direct precursor) of n is substituted for N, and then the direct successor (or precursor) is removed (or predecessor) * <p> here the direct precursor and successor are for the order of the sequence traversal * <ul/> *@paramnode to delete node *@parampre its parent node*/Private voidDelete (node node, node Pre) {node tmp=NULL; if(Node.lchild = =NULL&& Node.rchild = =NULL){//leaf knot PointTMP =NULL; } Else if(Node.lchild = =NULL) {//left child empty, directly with right child replacementTMP =Node.rchild; } Else if(Node.rchild = =NULL) {//right child is empty, directly with left child substitutionTMP =Node.lchild; } Else{//The left and right subtree are not empty, and the direct successor substitution using the middle sequence traversalNode Prenode =NULL//parent node of the first node of the sequence traversal in the right sub-tree, Lnode = Node.rchild;//finds the first node of the sequence traversal in its right subtree, which has no left child         while(Lnode.lchild! =NULL) {Prenode=Lnode; Lnode=Lnode.lchild; } node.data= Lnode.data;//Exchange ValuePrenode.lchild = Lnode.rchild;//Delete directlyTMP =node; }        if(Pre.lchild = =node) {Pre.lchild=tmp; } Else{pre.rchild=tmp; }}
analysis of searching efficiency of 62-fork sorting Tree

For a two-fork sort tree with a height of H , the insertion and deletion run time is O (h). But in the worst case, the input sequence that constructs a two-fork sort tree is ordered, and a slanted single-branch tree is formed, at which point the performance of the binary sorting tree is significantly worse, and the height of the tree is increased to the number of elements N.

Look at the concept of average lookup length before analyzing it.

To determine where the record is in the lookup table, the number of keywords that are compared to the given value is called the average lookup length (ASL)When the lookup algorithm is found to be successful . For a lookup table with n elements, the average lookup length for the lookup success is:

where pi is the probability of finding the element I in the table, CI is the number of times that I have been compared to find the first element.

The unknown origin element is not found in the lookup table, but finding the average number of lookups where the unknown origin element should exist in the table is known as the average lookup length for unsuccessful lookups .

The number next to the element is the number of times the comparison occurs when the lookup succeeds.

In the case of equal probabilities , Figure 2 (a) finds a successful average lookup length of ( probability multiplied by the number of layers of the keyword ):

ASLa = (1+2+2+3+3+3+3+4+4+4)/10 = 2.9

The average lookup length for a successful lookup of Figure 2 (b) is

aslb= (1+2+3+4+5+6+7+8+9+10)/10=5.5

From the above, the average search length of binary sorting tree search algorithm depends mainly on the height of the tree, which is related to the morphology of the two-fork tree. If the binary sort tree is a single-branch tree (like an ordered single-linked list) with only right (left) children, the average lookup length is the same as the single-linked list, O (n), and if the height difference of the left and the lower subtree is not more than 1, it is a balanced binary tree with an average search length of O (log2n

Binary sort tree is similar to binary search , in terms of average performance, it is almost the same, but the binary sorting tree is unique, and the binary sort trees are not unique, different input order, may get different two-fork sort tree. Such as.

For maintaining the order of the table, the two-fork sorting tree does not need to move the nodes, just modify the pointer to complete the INSERT and delete, the average execution time is O (log2n). The binary Lookup object is an ordered sequential table, and the insertion and deletion costs are O (n). Therefore, if the static lookup table , it is advisable to use the sequential table storage structure, binary search; If the table is dynamically found , the binary sort tree is chosen as its logical structure.

Binary sort 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.