NueDS 数据结构&树

一.单选题

任何二叉搜索树中同一层的结点从左到右是有序的(从小到大)。T


二叉搜索树的查找和折半查找的时间复杂度相同。F

需要满足平衡二叉树,二叉树的时间复杂度才为O(logN) 


二叉排序树的后序遍历序列必然是递增的。F

中序遍历才是递增的 


若一搜索树(查找树)是一个有n个结点的完全二叉树,则该树的最大值一定在叶结点上 。F

最大值出现在最右边的右孩子,但不一定是叶节点


N个结点的二叉排序树有多种,其中树高最小的二叉排序树是最佳的。T

二叉排序树的主要用途是链式存储结构的二分查找,查找的最坏次数是树的高度,因此高度最小的二叉排序树是最佳的 


中根遍历二叉查找树所得序列一定是有序序列。T


在二叉排序树中,每个结点的关键字都比左孩子关键字大,比右孩子关键字小。T


二叉排序树的查找效率和二叉排序树的髙度有关。T


二.单选题

1.对二叉搜索树进行什么遍历可以得到从小到大的排序序列?

A.前序遍历

B.后序遍历

C.层次遍历

D.中序遍历


2.若二叉搜索树是有N个结点的完全二叉树,则不正确的说法是:

A.最大值一定在最后一层

B.平均查找效率是O(logN)

C.中位值结点在根结点或根的左子树上

D.最小值一定在叶结点上


3.若一棵二叉树的前序遍历序列是{ 4, 2, 1, 3, 6, 5, 7 },中序遍历序列是{ 1, 2, 3, 4, 5, 6, 7 },则下列哪句是错的?

A.这是一棵二叉搜索树

B.这是一棵完全二叉树

C.所有的奇数都在叶子结点上

D.2是5的父结点

在这里插入图片描述


4.将{ 6, 9, 12, 3, 4, 8 }依次插入初始为空的二叉搜索树。则该树的后序遍历结果是:

A.3, 4, 9, 8, 12, 6

B.4, 3, 8, 12, 9, 6

C.4, 3, 6, 8, 12, 9

D.3, 4, 6, 8, 12, 9


5.将{ 32, 2, 15, 65, 28, 10 }依次插入初始为空的二叉搜索树。则该树的前序遍历结果是:

A.10, 28, 15, 2, 65, 32

B.2, 10, 15, 28, 32, 65

C.32, 2, 15, 10, 28, 65

D.32, 2, 10, 15, 28, 65


6.将{ 32, 2, 15, 65, 28, 10 }依次插入初始为空的二叉排序树,则该树的后序遍历结果是。

A.32, 2, 15, 10, 28, 65

B.32, 2, 10, 15, 28, 65

C.2, 10, 15, 28, 32, 65

D.10, 28, 15, 2, 65, 32


7.已知由(60,30,56,78,12,45)序列构成的二叉排序树,其不成功查找的平均查找长度为( )。

A.28/7

B.21/7

C.15/6

D.21/6

【【自用数据结构】王道7.3.4  树型查找计算题 计算二叉排序树查找成功/失败的平均查找长度+区分大根堆与二叉排序树】 https://www.bilibili.com/video/BV1de4y1i72h/?share_source=copy_web&vd_source=a6401c9820869ff8ca006c68478cde72 


8. 已知一棵由1、2、3、4、5、6、7共7个结点组成的二叉搜索树(查找树),其结构如图所示,问:根结点是什么?

A.5

B.不能确定

C.4

D.1

 左边四个都比根节点小,右边两个都比根节点大,所以根节点为5


三.函数题

R6-2 二叉搜索树的操作集

分数 40

全屏浏览题目

切换布局

作者 陈越

单位 浙江大学

本题要求实现给定二叉搜索树的5种常用操作。

函数接口定义:

BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); Position FindMin( BinTree BST ); Position FindMax( BinTree BST ); 

其中BinTree结构定义如下:

typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; 
  • 函数InsertX插入二叉搜索树BST并返回结果树的根结点指针;
  • 函数DeleteX从二叉搜索树BST中删除,并返回结果树的根结点指针;如果X不在树中,则打印一行Not Found并返回原树的根结点指针;
  • 函数Find在二叉搜索树BST中找到X,返回该结点的指针;如果找不到则返回空指针;
  • 函数FindMin返回二叉搜索树BST中最小元结点的指针;
  • 函数FindMax返回二叉搜索树BST中最大元结点的指针。

裁判测试程序样例:

#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; void PreorderTraversal( BinTree BT ); /* 先序遍历,由裁判实现,细节不表 */ void InorderTraversal( BinTree BT ); /* 中序遍历,由裁判实现,细节不表 */ BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); Position FindMin( BinTree BST ); Position FindMax( BinTree BST ); int main() { BinTree BST, MinP, MaxP, Tmp; ElementType X; int N, i; BST = NULL; scanf("%d", &N); for ( i=0; i<N; i++ ) { scanf("%d", &X); BST = Insert(BST, X); } printf("Preorder:"); PreorderTraversal(BST); printf("\n"); MinP = FindMin(BST); MaxP = FindMax(BST); scanf("%d", &N); for( i=0; i<N; i++ ) { scanf("%d", &X); Tmp = Find(BST, X); if (Tmp == NULL) printf("%d is not found\n", X); else { printf("%d is found\n", Tmp->Data); if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data); if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data); } } scanf("%d", &N); for( i=0; i<N; i++ ) { scanf("%d", &X); BST = Delete(BST, X); } printf("Inorder:"); InorderTraversal(BST); printf("\n"); return 0; } /* 你的代码将被嵌在这里 */ 

输入样例:

10
5 8 6 2 4 1 0 10 9 7
5
6 3 10 0 5
5
5 7 0 10 3

输出样例:

Preorder: 5 2 1 0 4 8 6 7 10 9
6 is found
3 is not found
10 is found
10 is the largest key
0 is found
0 is the smallest key
5 is found
Not Found
Inorder: 1 2 4 6 8 9
Position Find(BinTree p,ElementType x)
{
    while(p)
    {
        if(x>p->Data)
            p=p->Right;
        else if(x<p->Data)
            p=p->Left;
        else break;
    }
    return p;
}

Position FindMax(BinTree p)
{
    if(p)
        while(p->Right)
            p=p->Right;
        
    return p;
}
Position FindMin(BinTree p)
{
    if(p)
        while(p->Left)
            p=p->Left;
    return p;
}

BinTree Insert(BinTree p,ElementType x)
{
    if(!p)
    {
        p=(struct TNode*)malloc(sizeof(struct TNode));
        p->Data=x;
        p->Left=p->Right=NULL;
    }else{
        if(x<p->Data)
            p->Left=Insert(p->Left,x);
        else if(x>p->Data)
            p->Right=Insert(p->Right,x);
    }
    return p;
}

BinTree Delete(BinTree p,ElementType x)
{
    Position temp;
    if(!p)
    {
        printf("Not Found\n");
        
    }else if(x<p->Data)
    {
        p->Left=Delete(p->Left,x);
    }else if(x>p->Data)
    {
        p->Right=Delete(p->Right,x);
        
    }else{
        if(p->Left&&p->Right)
        {
            temp=FindMax(p->Left);
            p->Data=temp->Data;
            p->Left=Delete(p->Left,p->Data);
        }else{
            temp=p;
            if(!p->Left)
            {
                p=p->Right;
            }else if(!p->Right)
            {
                p=p->Left;
            }
            free(temp);
        }
    }
    return p;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H._

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值