Binary Sort Tree(BST)

来源:互联网 发布:linux mint cinnamon 编辑:程序博客网 时间:2024/05/17 03:44

Basic structure

typedef struct BstNode{    key_type key;    struct node *lchild;    struct node *rchild;    struct node *parent;}Node

Structure Feature

  1. If left_child is not NULL, left child is smaller than parant node,
  2. If right_child is not NULL, right child is bigger than parant node,
  3. Its left child tree is binary sort tree, so is right child tree,
  4. It is easier to search and insert keys than ordinary binary tree.

Basic Operation

a. keyword search and location.

Start with root node x, if k is less than x.key, go left, else go right , if k is equal x.key, stop and location. It takes time about O(h).

b. maximum/minimum/predecessor/successor keyword search.

Keyword’s predecessor and successor is all about inorder tree walk algorithm definition.

c. keyword delete/insert

we need focus on delete operation,there are four situations,delete z ,to analyze as follows.

a. z is leaf node,no left or right child

Delete directly, plus, you have to pay attention to whether it is root node.

b. z has only left subtree,no right subtree or child.

If z is left child of z’s parent , connect z’s left node with z’s parent ,delete z.

If z is right child of z’s parent , connect z’s left node with z’s parent ,delete z.

c.z has only right subtree,no left subtree or child.

If z is left child of z’s parent , connect z’s right node with z’s parent ,delete z.

If z is right child of z’s parent , connect z’s right node with z’s parent ,delete z.

d.z has both right and left subtree.

the key point is to find z’s successor y,and analyze it.if its successor y has no left child ,replace z with y directly.if y has parent ,and it is z’s right child, right now, y must has no left child, maybe has right child,y replace z, y’s right child replace y,its parent change to z’s right child.

Code

//Binary Search Tree in C programming language#include<stdio.h>#include<stdlib.h>typedef int key_type;typedef struct node{    key_type key;    struct node *lchild;    struct node *rchild;    struct node *parent;}node,*node_ptr;void insert(node_ptr *root,key_type key){    node_ptr p = (node_ptr)malloc(sizeof(node));    p->key = key;    p->lchild = p->rchild = p->parent = NULL;    if((*root) == NULL)    {        *root = p;        return;    }    if((*root)->lchild == NULL&&(*root)->key > key)    {        p->parent = (*root);        (*root)->lchild = p;        return;    }    if((*root)->rchild == NULL&&(*root)->key < key)    {        p->parent = (*root);        (*root)->rchild = p;        return;    }    if(key< (*root)->key)        insert(&(*root)->lchild,key);    else if(key> (*root)->key)        insert(&(*root)->rchild,key);    else        return;}node_ptr search(node_ptr pnode, key_type key){    if(pnode == NULL)        return NULL;    if(key > pnode->key)        return search(pnode->rchild,key);    else if(key < pnode->key)        return search(pnode->lchild,key);    else        return pnode;}void create(node_ptr *root,key_type a[],int n){    int i;    for(i = 0;i<n;++i)        insert(root, a[i]);}node_ptr Searchmin(node_ptr pnode){    if(pnode==NULL)        return NULL;    if(pnode->lchild==NULL)        return pnode;    else        return Searchmin(pnode->lchild);}node_ptr Searchmax(node_ptr pnode){    if(!pnode)        return NULL;    if(pnode->rchild==NULL)        return pnode;    else        return Searchmax(pnode->rchild);}node_ptr SearchPredecessor(node_ptr pnode){    //inorder traverse     if(pnode == NULL)        return pnode;    if(pnode->lchild)        return Searchmax(pnode->lchild);    else{        if(pnode->parent == NULL)            return NULL;        while(pnode){            if(pnode->parent->rchild==pnode)                break;            pnode=pnode->parent;        }        return pnode->parent;    }}node_ptr SearchSuccessor(node_ptr pnode){    if(!pnode)        return pnode;    if(pnode->rchild)        return Searchmin(pnode->rchild);    else{        if(pnode->parent == NULL)            return NULL;        while(pnode){            if(pnode->parent->lchild == pnode)                break;            pnode=pnode->parent;        }        return pnode->parent;    }}int deletenode(node_ptr *root,key_type key){    node_ptr q;    node_ptr p=search(*root,key);    key_type temp;    if(!p)        return 0;    //1.the deleted one is leaf    if(p->lchild==NULL && p->rchild == NULL)    {        //root        if(p->parent == NULL)        {            free(p);            (*root)=NULL;        }        //NOT root        else        {            if(p->parent->lchild==p)                p->parent->lchild = NULL;            else                p->parent->rchild = NULL;            free(p);        }    }    //2.the deleted one has only left subtree,no right subtree    else if(p->lchild && !(p->rchild))    {        p->lchild->parent=p->parent;        if(p->parent == NULL)        {            (*root)=p->lchild;        }        else if(p->parent->lchild == p)//p is lchild of parent node        {            p->parent->lchild=p->lchild;        }        else        {            p->parent->rchild=p->lchild;        }        free(p);    }    //3.the deleted one has only right subtree    else if(p->rchild && !(p->lchild))    {        p->rchild->parent=p->parent;        if(p->parent == NULL)        {            (*root)=p->rchild;        }        else if(p->parent->lchild == p)//p is lchild of parent node        {            p->parent->lchild=p->rchild;        }        else        {            p->parent->rchild=p->rchild;        }        free(p);    }    //4.the deleted one has both left and right subtree    else    {        q = SearchSuccessor(p);        temp = q->key;        deletenode(root,q->key);        p->key=temp;    }    return 1;}int main(int argc, char ** argv){    node_ptr pnode = NULL;    node_ptr root  = NULL;    node_ptr max,min;    key_type a[]   = {11,1,5,3,4,8,13};    create(&root,a,7);    pnode=search(root,5);    printf("%d\n", pnode->key);    max = Searchmax(root);    min = Searchmin(root);    printf("maximum ,minimum is %d, %d\n",max->key,min->key);    deletenode(&root,1);    printf("root is %d\n",root->key);    printf("root predecessor is %d\n",SearchPredecessor(root)->key);    printf("root successor is %d\n",SearchSuccessor(root)->key);    printf("now minimum is %d\n",Searchmin(root)->key);    return 0;}

Result & Disscusion

  1. Processing result

这里写图片描述

  1. Disscusion of result

这里写图片描述

From the result we can find that the procesure success to find element 5, maximum value 13 and minimum value 1, right now the root is 11, after delete 1, to keep inorder search mode, so 11’s processor 8, successor is 13, right now minimum value become 3.

0 0