二叉排序树的一些基本操作

二叉排序树的基本操作

在做数据结构课程设计时遇到的问题,记录之,以待日后查阅。

【选做内容】
(1) 创建一棵二叉排序树;
(2) 查找二叉排序树中一个指定结点;
(3) 输出查找指定结点过程中所需比较的次数。

导包及一些声明

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define TRUE 1
#define OVERFLOW -2
#define FALSE 0
#define ERROR 0
#define MAXSIZE 100

typedef bool Status;
typedef int KeyType;
typedef struct{
    KeyType key; // 用于存放编码(即数字)
    // 还可以添加别的数据类型,可根据需要自行定义
}ElemType;
typedef struct BiTNode{
    ElemType data; // 结点的数据域
    struct BiTNode *lchild, *rchild; // 左右孩子指针
}BiTNode, *BiTree;

节点查找算法

BiTree SearchBST(BiTree T, KeyType key){
    if ((!T)||(key==T->data.key)){
        printf("查找成功!\n");
        return T;// ⚠️注意:此处返回的是?树
    }
    else if (key<T->data.key)
        return SearchBST(T->lchild, key);
    else
        return SearchBST(T->rchild, key);
}

二叉排序树的创建

Status RSearchBST(BiTree T, KeyType key, BiTree f, BiTree &p){
    // 返回插入位置的二叉排序树查找算法
    if (!T) {p = f; return FALSE;} // 查找不成功
    else if (key == T->data.key){ p=T; return TRUE;} // 查找成功
    else if (key < T->data.key) return RSearchBST(T->lchild, key, T, p); // 在左子树中继续查找
    else return RSearchBST(T->rchild, key, T, p); // 在右子树中继续查找
}

Status InsertBST(BiTree &T, ElemType e){
    // 在二叉排序树中插入关键字值为key, 记录为e的新结点
    // 若成功,则返回TRUE;否则返回FAISE
    BiTree p,s;
    if (!RSearchBST(T, e.key, NULL, p)) {
        s=(BiTree)malloc(sizeof(BiTNode));
        s->data=e;
        s->lchild=s->rchild=NULL;
        if (!p) T=s;
        else if (e.key<p->data.key) p->lchild=s;
        else p->rchild=s;
        return TRUE;
    }
    else
        return FALSE;
}

Status CreateBST(BiTree &T, ElemType items[], int n){
    for (int i=0; i<n; i++) {
        InsertBST(T, items[i]); // 调用二叉排序树的插入算法
    }
    return OK;
}

输出(遍历)

void PreRootTraverse(BiTree T){
    // 先序遍历
    if (T!=NULL) {
        printf("%d ",T->data.key);
        PreRootTraverse(T->lchild);
        PreRootTraverse(T->rchild);
    }
}

void InRootTraverse(BiTree T){
    // 中序遍历
    if (T!=NULL) {
        InRootTraverse(T->lchild);
        printf("%d ", T->data.key);
        InRootTraverse(T->rchild);
    }
}

结点层级查找

int levelOfNode(BiTree T, KeyType key){
    int n = 1;
    BiTree q;
    q=T;
    if (T==NULL)
        return 0; // 空树
    while (q) {
        if (key==q->data.key) {
            return n;
        }else{
            if (key<q->data.key)
                q=q->lchild;
            else
                q=q->rchild;
            n++;
        }
    }
    return -1; // 不存在,未查询到
}

主函数(例子)

int main() {
    BiTree T;
    int count, n;
    printf("请输入元素个数:");
    scanf("%d", &count);
    printf("输入元素:\n");
    ElemType items[count];
    for (int i=0; i<count; i++) {
        scanf("%d",&n);
        items[i].key=n;
    }
//  ElemType items[7]={2,4,1,3,6,7,5};这是例子
    CreateBST(T, items, count);
    printf("中序遍历:");
    InRootTraverse(T);
    printf("\n");
    printf("先序遍历:");
    PreRootTraverse(T);
    printf("\n");
    printf("请输入要查找的节点:");
    int findnum;
    scanf("%d",&findnum);
    PreRootTraverse(SearchBST(T, findnum));
    printf("\n");
    printf("该节点位于第%d层。\n",levelOfNode(T, findnum));
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值