Binary Sort Tree(BST)

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.

时间: 2024-11-10 13:56:13

Binary Sort Tree(BST)的相关文章

二叉排序树(Binary Sort Tree)

二叉排序树(Binary Sort Tree)又称二叉查找树,是一种排序和查找都很有用的特殊二叉树.BST 满足 左节点的值<根节点的值<右节点的值.根据这个原理,我们可以推断:BST的中序遍历必定是严格递增的. 如图一棵二叉树,是一棵BST.我们可以明显看到他的中序遍历为09,17,23,45,53,60,70,75,78,88,94.(因为左<中<右) 下面是BST的实现,包括BST的插入,创建,删除,遍历 //BST.h #include<iostream> us

二叉排序树(Binary Sort Tree)

1.定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树,或者是满足如下性质的二叉树: ①  若它的左子树非空,则左子树上所有结点的值均小于根结点的值: ②  若它的右子树非空,则右子树上所有结点的值均大于根结点的值: ③  左.右子树本身又各是一棵二叉排序树. 上述性质简称二叉排序树性质(BST性质),故二叉排序树实际上是满足BST性质的二叉树. 注意: 当用线性表作为表的组织形式时,可以有三种查找法

二叉查找树(Binary Sort Tree)(转)

二叉查找树(Binary Sort Tree) 我们之前所学到的列表,栈等都是一种线性的数据结构,今天我们将学习计算机中经常用到的一种非线性的数据结构--树(Tree),由于其存储的所有元素之间具有明显的层次特性,因此常被用来存储具有层级关系的数据,比如文件系统中的文件:也会被用来存储有序列表等. 在树结构中,每一个结点只有一个前件,称为父结点,没有前件的结点只有一个,称为树的根结点,简称树的根(root).每一个结点可以有多个后件,称为该结点的子结点.没有后件的结点称为叶子结点.一个结点所拥有

Lowest Common Ancestor of a Binary Search Tree (BST)

Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node* LCA(Node* root, Node* p, Node* q) { if (!root || !p || !q) return NULL; if (max(p->data, q->data) < root->data) return LCA(root->left, p,

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

UVA 1264 - Binary Search Tree(BST+计数)

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

Convert Sorted List to Balanced Binary Search Tree (BST)

(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Code: BinaryTree* sortedListToBST(ListNode *& list, int start, int

Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

(http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Code: v