6-4 二叉排序树查找最小值最大值操作 (6 分)
本文最后更新于1782天前,其中的信息可能已经有所发展或是发生改变。

本题要求实现二叉排序树的两个基本操作。

函数接口定义:

BSTree FindMin( BSTree T);
BSTree FindMax( BSTree T);

函数FindMin返回二叉排序树T中最小元素结点的指针;

函数FindMax返回二叉排序树T中最大元素结点的指针。

其中BSTree结构定义如下:

typedef int ElemType;
typedef struct BSTNode
{
	ElemType data;
	struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct BSTNode
{
	ElemType data;
	struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
BSTree CreateBST(); /* 二叉排序树创建,由裁判实现,细节不表 */
BSTree FindMin( BSTree T);
BSTree FindMax( BSTree T);
void Inorder(BSTree T);/* 中序遍历,由裁判实现,细节不表 */

int main()
{
	BSTree T,MinP, MaxP;
	ElemType n,e;
	T = CreateBST();
	printf("Inorder:");	Inorder(T);	printf("\n");
	MinP = FindMin(T);
	MaxP = FindMax(T);
	if(MinP) printf("%d is the smallest key\n",MinP->data);
	if(MaxP) printf("%d is the largest key\n",MaxP->data);
	return 0;
}
/* 你的代码将被嵌在这里 */

对于创建好的二叉排序树,如下图:

二叉排序树.png

输出样例:

Inorder: 1 2 3 4 5 6 7 8
1 is the smallest key
8 is the largest key
BSTree FindMin( BSTree T)
{
    BSTree p;
    p = T;
    while(p&&p->lchild)
    {
        p = p->lchild;
    }
    return p;
}
BSTree FindMax( BSTree T)
{
    BSTree p;
    p = T;
    while(p&&p->rchild)
    {
        p = p->rchild;
    }
    return p;
}

点击数:653

    暂无评论

    发送评论 编辑评论

    
    				
    |´・ω・)ノ
    ヾ(≧∇≦*)ゝ
    (☆ω☆)
    (╯‵□′)╯︵┴─┴
     ̄﹃ ̄
    (/ω\)
    ∠( ᐛ 」∠)_
    (๑•̀ㅁ•́ฅ)
    →_→
    ୧(๑•̀⌄•́๑)૭
    ٩(ˊᗜˋ*)و
    (ノ°ο°)ノ
    (´இ皿இ`)
    ⌇●﹏●⌇
    (ฅ´ω`ฅ)
    (╯°A°)╯︵○○○
    φ( ̄∇ ̄o)
    ヾ(´・ ・`。)ノ"
    ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
    (ó﹏ò。)
    Σ(っ °Д °;)っ
    ( ,,´・ω・)ノ"(´っω・`。)
    ╮(╯▽╰)╭
    o(*////▽////*)q
    >﹏<
    ( ๑´•ω•) "(ㆆᴗㆆ)
    😂
    😀
    😅
    😊
    🙂
    🙃
    😌
    😍
    😘
    😜
    😝
    😏
    😒
    🙄
    😳
    😡
    😔
    😫
    😱
    😭
    💩
    👻
    🙌
    🖕
    👍
    👫
    👬
    👭
    🌚
    🌝
    🙈
    💊
    😶
    🙏
    🍦
    🍉
    😣
    Source: github.com/k4yt3x/flowerhd
    颜文字
    Emoji
    小恐龙
    花!
    上一篇
    下一篇