热门关键字:  ubuntu  分区  函数  Fedora  linux系统进程

当前位置 :| 主页>Linux教程>编程开发>C++>

二叉查找树的一级ADT接口、实现和使用

来源: 作者: 时间:2007-12-04 Tag: 点击:
接口文件:
[code]
/*-----------------------------------------------------------
        二叉查找树的ADT接口
        作者: QingXiaoming, 12-03, 2007([url]http://www.cublog.com/augustusqing/[/url])
        您可以自由的传播,修改这份代码,转载处请注明原作者
-------------------------------------------------------------*/

#ifndef BIN_SORT_TREE_ADT_H
#define BIN_SORT_TREE_ADT_H
#include "Item.h"
typedef struct BinTreeNode *bstlink;
typedef struct BinTree *BST;
BST initBinTree(void);
int emptyBinTree(BST);
bstlink searchBinTree(BST, Item);
bstlink maxBinTree(BST);
bstlink minBinTree(BST);
bstlink sucBinTree(BST, bstlink);
bstlink preBinTree(BST, bstlink);
void insertBinTree(BST, Item);
bstlink deleteBinTree(BST, Item);
void traverseBinTree(BST);
void destroyBinTree(BST);
Item showBinTreeNode(bstlink);
#endif
[/code]
 
实现文件:
[code]
/*-----------------------------------------------------------
        二叉查找树的ADT实现
        作者: QingXiaoming, 12-03, 2007([url]http://www.cublog.com/augustusqing/[/url])
        您可以自由的传播,修改这份代码,转载处请注明原作者
-------------------------------------------------------------*/
#include <stdlib.h>
#include "include/Bin_Sort_Tree_ADT.h"
struct BinTreeNode {
       Item item;
       bstlink parent;
       bstlink lchild;
       bstlink rchild;
       };
struct BinTree {
       bstlink root;
       };
static bstlink NEW(Item item, bstlink parent)
{
    bstlink x = malloc(sizeof(*x));
    x->item = item;
    x->parent = parent;
    x->lchild = x->rchild = NULL;
    return x;
}
BST initBinTree(void)
{
    BST bst = malloc(sizeof(*bst));
    bst->root = NULL;
    return bst;
}
int emptyBinTree(BST bst)
{
    return bst->root == NULL;
}
bstlink searchBinTree(BST bst, Item item)
{
       bstlink x = bst->root;
       while (x != NULL && x->item != item)
       {
           x = (item < x->item) ? x->lchild : x->rchild;
       }
       return x;
}
bstlink maxBinTree(BST bst)
{
      bstlink x = bst->root;
      while (x != NULL && x->rchild != NULL)
      {
            x = x->rchild;
      }
      return x;
}
bstlink minBinTree(BST bst)
{
     bstlink x = bst->root;
      while (x  && x->lchild)
      {
            x = x->lchild;
      }
      return x;
}
bstlink sucBinTree(BST bst, bstlink link)
{
     bstlink x ;
     if (link->rchild != NULL)
     {
         x = link->rchild;
         while (x && x->lchild) x = x->lchild;
         return x;
     }
     x = link->parent;
     while (x && link == x->rchild)
     {
         link = x;
         x = x->parent;
     }
     return x;
}
bstlink preBinTree(BST bst, bstlink link)
{
     bstlink x;
     if (link->lchild != NULL)
     {
         x = link->lchild;
         while (x && x->rchild) x = x->rchild;
         return x;
     }
     x = link->parent;
     while (x && link == x->lchild)
     {
         link = x;
         x = x->parent;
     }
     return x;
}
void insertBinTree(BST bst, Item item)
{
     bstlink x, p;
     if (!bst->root)
     {
         bst->root = NEW(item, bst->root);
         return ;
     }
     x = bst->root;
     while (x)
     {
         p = x;
         x = (item <= x->item) ? x->lchild : x->rchild;
     }
     if (item <= p->item)
     {
         p->lchild = NEW(item, p);
     }
     else
     {
         p->rchild = NEW(item, p);
     }
}
bstlink deleteBinTree(BST bst, Item item)
{
    bstlink x, y, z = searchBinTree(bst, item);
    if (!z)
    {
        return z;
    }
    if ( !z->lchild || !z->rchild)
    {
         y = z;
    }
    else
    {
        y = sucBinTree(bst, z);
    }
    x = y->lchild ? y->lchild : y->rchild;
    if (x)
    {
        x->parent = y->parent;
    }
    if (!y->parent)
    {
        bst->root = x;
    }
    else
    {
        if ( y == y->parent->lchild)
        {
             y->parent->lchild = x;
        }
        else
        {
            y->parent->rchild = x;
        }
    }
    if (y != z)
    {
        z->item = y->item;
    }
    return y;
}
void traverseBinTree(BST bst)
{
     bstlink x = minBinTree(bst);
     while (x)
     {
         printf("%d \n", x->item);
         x = sucBinTree(bst, x);
     }
}
void destroyBinTree(BST bst)
{
     bstlink y, x = minBinTree(bst);
     while (x)
     {
           y = sucBinTree(bst, x);
           free(x);
     }
}
Item showBinTreeNode(bstlink link)
{
     return link->item;
}
[/code]
 
客户程序:
[code]
/*-----------------------------------------------------------
        二叉查找树的ADT客户程序
        作者: QingXiaoming, 12-03, 2007([url]http://www.cublog.com/augustusqing/[/url])
        您可以自由的传播,修改这份代码,转载处请注明原作者
-------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "include/Bin_Sort_Tree_ADT.h"
#include "include/Stack_ADT.h"
int main(int argc, char *argv[])
{
    int i, N = 10;
    Item t;
    bstlink x;
    BST bst = initBinTree();
    S ps = initStack(N);
    srand((unsigned)clock());
    for (i = 0; i < N; i++)
    {
        t = rand() + i;
        printf("%d \n", t);
        pushStack(ps, t);
        insertBinTree(bst, t);
    }
    printf("\n");
    traverseBinTree(bst);
    printf("\n");
    for (i = 0; i < N; i++)
    {
        t = popStack(ps);
        x = deleteBinTree(bst, t);
        printf("%d \n", showBinTreeNode(x));
    }
    system("pause");
    return 0;
}
[/code]

最新评论共有 4 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表