从大到小打印BST结点

本文介绍了如何编写一个函数,按照从大到小的顺序打印二叉搜索树(BST)的节点值。通过调整中序遍历(RNL)顺序,实现了从最高值到最低值的节点打印。提供了一个包含创建BST、插入节点以及倒序中序遍历的C++代码示例。
问题描述:Write a function that prints out the node values for a BST in sorted order from highest to lowst.
问题分析:BST是一种特殊的二叉树,对于每一个结点,其左子树所有结点的值都小与该结点值,右子树所有结点的值都大于该结点值。所以利用in-order遍历能很好地打印出排好序的结点。不过需要注意的是,平常使用的in-order是LNR,而在此程序中应该把in-order打印的顺序改变为RNL.
代码实现:
#include <iostream>  
using namespace std;  
  
// BST的结点  
typedef struct node  
{  
    int key;  
    struct node *lChild, *rChild;  
}Node, *BST;  
  
// 在给定的BST中插入结点,其数据域为element, 使之称为新的BST  
bool BSTInsert(Node * &p, int element)  
{  
    if(NULL == p) // 空树  
    {  
        p = new Node;  
        p->key = element;  
        p->lChild = p->rChild = NULL;  
        return true;  
    }  
  
    if(element == p->key) // BST中不能有相等的值  
        return false;  
  
    if(element < p->key)  // 递归  
        return BSTInsert(p->lChild, element);  
  
    return BSTInsert(p->rChild, element); // 递归  
}  
  
// 建立BST  
void createBST(Node * &T, int a[],
6-4 二叉搜索树的操作集 分数 30 作者 陈越 单位 浙江学 本题要求实现给定二叉搜索树的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; }; 函数Insert将X插入二叉搜索树BST并返回结果树的根结点指针; 函数Delete将X从二叉搜索树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
最新发布
11-08
6-12 二叉搜索树查找 分数 9 作者 周强 单位 青岛学 请实现一个函数,进行二叉搜索树(Binary Search Tree)查找操作。 二叉搜索树定义如下: typedef struct TNode *BST; //struct TNode * 类型重定义为 BST struct TNode{ int data; // 结点数据 BST left; // 左子树指针 BST right; // 右子树指针 }; 函数接口定义:: void searchX(BST bt, int x); 其中bt为进行查找操作的二叉搜索树根指针,x为待查找的结点。 题目保证输入的二叉搜索树非空且不超过200个结点结点数值在整型int范围内。 函数打印二叉树搜索树bt中查找x的过程(即打印从根节点至x结点的路径上各结点的值),如果没找到x结点,则最终打印 Not Found!。 每行打印一个结点的值或Not Found!并以换行符结束,不要加多余空格等字符。 裁判测试程序样例: #include<stdio.h> #include<stdlib.h> typedef struct TNode *BST; //struct TNode * 类型重定义为 BST struct TNode{ int data; // 结点数据 BST left; // 左子树指针 BST right; // 右子树指针 }; BST createNode(int data){ // 创建结点 BST tmp; /* 函数实现细节省略 */ return tmp; } BST findNode(BST bt, int data){ // 查找结点 /* 函数实现细节省略 */ } int insert(BST bt, int parent, int dir, int data){ // 插入结点 /* 在二叉搜索树bt中的parent结点下插入子结点data, dir为0表示左子结点, dir为1表示右子结点*/ /* 函数实现细节省略 */ } BST createBinTree(){ // 创建二叉搜索树 /* 实现细节仅供参考 */ int total, data; scanf("%d",&total); // 输入结点总数 if(total==0) return NULL; scanf("%d",&data); // 输入根结点BST bt; bt = createNode(data); // 创建根结点 if(!bt) return NULL; int parent,dir; for(int i=1;i<total;i++){ scanf("%d%d%d",&parent, &dir, &data); insert(bt,parent, dir, data); // 插入其他结点 } return bt; } void searchX(BST bt, int x); // 这是需要你实现的函数原型 int main(){ /* 实现细节仅供参考,与实际的测试程序中的main()可能不相同 */ BST bt; bt = createBinTree(); // 创建二叉搜索树 int k; scanf("%d", &k); // 输入要查找的结点k searchX(bt, k); // 查找k return 0; } /* 你所提交的代码将被嵌入在本行开始的位置(当然,不是嵌入在注释符中间) */ 二叉搜索树举例: image.png 以下输入样例基于此图的二叉搜索树及以上样例测试程序规定的输入格式。 输入样例1: 4 10 10 1 30 30 0 20 30 1 40 20 输出样例1: 10 30 20
06-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值