[Data structure] binary sort tree

Source: Internet
Author: User

On the problem of finding the data, if the data set to be found is an ordered linear table, and is stored sequentially, the lookup can be implemented using binary, interpolation, Fibonacci and other lookup algorithms, but, unfortunately, it takes a lot of time in order to insert and delete operations.

Then is there a way to make the insertion and deletion of the efficiency is good, but also can be more efficient to implement the search algorithm? In other words, there is no one algorithm that can be used with dynamic lookups.

Dynamic lookup: A lookup table that is inserted or deleted when it is found is called a dynamic lookup table.

So we introduced a two-fork sort tree, which is such a binary tree:

    • The value of all nodes on the left dial hand tree is less than the value of its root node;
    • The value of all nodes on the right subtree is greater than the value of its root node;
    • Its Saozi right sub-tree is also a binary sort tree.

As you can tell by the nature of the above, the sequential traversal of the binary sort tree gets an ascending sequence. But the purpose of constructing a binary sort tree is not to sort, but to improve the speed of finding and inserting deletes .

Code implementation

Tree structure node Definition:

typedef struct BITNODE{INT data;struct bitnode *lchild, *rchild;} Bitnode, *bitree;

Find implementation: Lookup succeeds, returns a pointer to the node found, lookup unsuccessful, returns the last node on the lookup path.

F is the parent node of the current root node T, and returns the parent node bool Searchbst (Bitree t, int key, Bitree F, Bitree *p) {if (t = = NULL)//recursive lookup unsuccessful when lookup fails {*p = F;return f Alse;} if (T->data = = key)//Find success {*p = T;return true;} else if (Key < T->data) {return Searchbst (T->lchild, Key, T, p);//Zuozi Recursive lookup}else{return searchbst (T->rchild, Key, T, p);//Right Sub-tree recursive Lookup}}

Why do you want to go back to the last node on the lookup path: Because you need to use a lookup when inserting an operation, the point is that the last node on the lookup path is the child who will insert the node as the last node.

To insert a new node, look for it, find the position that should be inserted bool Insertbst (bitree *t, int key) {Bitree P, s;if (! Searchbst (*t, Key, NULL, &p))//lookup unsuccessful, can be inserted {s = (bitnode*) malloc (sizeof (Bitnode)); s->data = Key;s->lchild = s-& Gt;rchild = null;if (p = = NULL) {*t = s;} else if (P->data > key) {p->lchild = s;} Else{p->rchild = s;} return true;} else//Lookup succeeded, the element to be inserted already exists, insert failed {return false;}}

  

[Data 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.