Binary search tree of data structure and algorithm

Source: Internet
Author: User

Unlike linked lists, a tree is a non-linear data structure. The most commonly used tree is a two-fork tree, the binary tree limits the number of subtrees, that is, the subtree of each node is up to 2, and the two subtrees are sequential. The binary search tree (binary lookup tree, binary sort tree) refers to the root node's keyword is greater than the left subtree, and the small less right operand subtree, and the left and right subtree is also a binary search tree. That is, the middle order traversal of a binary search tree, its output is from small to large sort good.

In addition to the normal two-fork search tree, there is a lot of distortion about it.

Binary balance search tree, that is, a binary balance tree, is also a search tree, the balance tree is any one node the height of the left subtree and the height of the right subtree of the difference between the absolute value of not more than 1.

Red-black tree, also a special two-fork search tree, in addition to the features of the search tree, it has a red and black color element on each node.

Here we mainly discuss the general two-fork search tree.

Binary search tree, in fact, is a special tree, any algorithm applicable to the tree is applicable to the binary search tree. Of course it is also a graph, any algorithm applicable to the graph also applies to the binary search tree. The depth-first search (DFS) in the graph is actually the pre-order traversal in the tree, and the breadth-first search (BFS) is the sequence traversal in the tree. Of course, there are many other algorithms in the diagram that apply to trees as well.

1. Binary Tree data structure

struct binsrctreenode{    int  element;     struct binsrctreenode *pleft;     struct binsrctreenode *pright;} Bstnode;

2. Dynamic allocation of a binary tree node

bstnode* allocnode (int  element) {    *pnode = (bstnode*)malloc(sizeof( Bstnode));     if (NULL! = pnode) {        pnode->element = element;        Pnode->pleft = NULL;        Pnode->pright = NULL;    }     return Pnode;}

3, binary search tree to find

Since the binary search tree is a two-fork tree that has already been sorted, it can be searched by binary lookup. First determine whether the root node is equal, if the keyword is greater than the root node, search the right subtree, the small root node of the keyword, the search for the left subtree.

BOOLSearchinbstree (ConstBstnode *proot,intElement,bstnode * *Pnode) {    if(NULL = =proot)return false; *pnode =Proot; if(Proot->element = =Element) {        return true; }    Else if(Proot->element >Element)returnSearchinbstree (proot->Pleft,element,pnode); Else        returnSearchinbstree (proot->pright,element,pnode);}

4, Binary search tree insertion

First, the binary tree is found whether the element already exists, if it already exists return false, if it does not exist, insert the node is larger than the root node, insert the right subtree, otherwise, insert the left subtree.

BOOLInsertintobstree (Bstnode **proot,intElement) {    if(NULL = =proot)return false; if(NULL = = *proot) {        *proot =Allocnode (Element); returnNull==*proot?false:true; } Bstnode**PSRC =NULL; if(Searchinbstree (PROOT,ELEMENT,PSRC))return false; Bstnode*pnode =Allocnode (Element); if(Element < *psrc->Element)*psrc->pleft =Pnode; Else*psrc->pright =Pnode; return true;}

5, binary search tree deletion

The deletion of binary search tree is divided into three cases:

(1) Delete the node is the leaf node, delete the node directly, and then modify its parent node pointer (note is the root node and not the root nodes).

(2) The deletion node is a single node (i.e., only Zuozi or right subtree).

(3) The Saozi right subtree of the deleted node is not empty.

BOOLDeletefrombstree (Bstnode **proot,intElement) {    if(NULL = = Proot | | NULL = = *proot)return false; Bstnode**PSRC =NULL; Bstnode**pparent =NULL; if(Searchinbstree (*proot,element,pparent,psrc)) {        if(Null = = *psrc->pleft && NULL = = *psrc->pright) {             Free(*PSRC); *PSRC =NULL; return true; }        Else if(NULL = = *psrc->pleft) {            if(*psrc = = *pparent->pleft)*pparent->pleft = *psrc->Pright; Else if(*psrc = = *pparent->pright)*pparent->pright = *psrc->Pright;  Free(*PSRC); return true; }        Else if(NULL = = *psrc->pright) {            if(*psrc = = *pparent->pleft)*pparent->pleft = *psrc->Pleft; Else if(*psrc = = *pparent->pright)*pparent->pright = *psrc->Pleft;  Free(*PSRC); return true; }        Else{Bstnode*pnode = *PSRC; Bstnode*pchild = *psrc->Pleft;  while(pchild->pright) {Pnode=pchild; Pchild= pchild->Pright; }            if(Pnode = = *psrc) Pnode->pleft = pchild->Pleft; ElsePnode->pright = pchild->Pleft; if(*psrc = = *pparent->pleft) *pparent->pleft =pchild; Else*pparent->pright =pchild; Pchild->pleft = *psrc->Pleft; Pchild->pright = *psrc->Pright; return true; }    }        return false;}

6. Binary search tree pre-order sequence traversal

/*preorder to traverse the binarysearchtree.*/voidPreorderbinarysearchtree (ConstBstnode *proot) {    if(NULL = =proot)return ; printf ("%d",proot->element); Preorderbinarysearchtree (Proot-pleft); Preorderbinarysearchtree (Proot-pright);}/*inorder to traverse the Binarysearchtree.*/voidInorderbinarysearchtree (ConstBstnode *proot) {    if(NULL = =proot)return ; Inorderbinarysearchtree (Proot-pleft); printf ("%d",proot->element); Inorderbinarysearchtree (Proot-pright);}/*Posorder to traverse the Binarysearchtree.*/voidPosorderbinarysearchtree (ConstBstnode *proot) {    if(NULL = =proot)return ; Posorderbinarysearchtree (Proot-pleft); Posorderbinarysearchtree (Proot-pright); printf ("%d",proot->element);}

7. Release the node

void freebinarysearchtree (Bstnode *proot) {    if(NULL = = proot        )  return  ;    Freebinarysearchtree (Proot-pleft);    Freebinarysearchtree (Proot-pright);      Free (proot);}

Full code See: Https://github.com/whc2uestc/DataStructure-Algorithm/tree/master/tree

typedef struct binsrctreenode{int element;    struct Binsrctreenode *pleft; struct Binsrctreenode *pright;} Bstnode;

Binary search tree of data structure and algorithm

Related Article

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.