排序二叉树构建二叉排序树,依次输入56,78,34,45,85,45,36,91,84,78,两个45和78
排序二叉树 时间:2021-09-12 阅读:(
)
构造一个二叉排序树
看了百度知道之前有人回答 “二叉排序树的插入 如果遇到 相同的节点 怎么办”,你也可以看一下该问题的答案,个人觉得挺有道理的。
根据这个结论,该二叉树这样排
12
/
5 17
/ /
3 5 14 20
9 15
/
8 10
或者
12
/
5 17
/ /
3 9 14 20
/
5 8 10 15
都可以,看你的要求,只要中序遍历是递增即可。
上述两个中序遍历都是
3 5 5 8 9 10 12 14 15 17 20最佳排序二叉树在结构上的特点是什么
展开全部
堆排序就是相当于一个排序二叉树,只是它是根节点的优先级别大于任何儿子的优先级别,这样可以每次删除根节点,然后调整整个堆。
program heap;
var a:array[1..10000] of integer;
n,i:integer;
procedure down(i:integer);
var x,j:integer;
begin
x:=a[i];
j:=i*2;
while j<=n do
begin
if a[j]>a[j+1] then j:=j+1;
if a[j]begin
a[i]:=a[j];
i:=j;
j:=i*2;
end else break;
end;
a[i]:=x;
end;
procedure delete(i);
begin
n:=n-1;
if (n=0)or(i=n+1) then exit
else
begin
a[i]:=a[n+1];
down(i);
end;
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
for i:=n div 2 downto 1 do down(i);
for i:=1 to n do
begin
write(a[1]);
delete(1);
end;
end.
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela用C语言实现二叉排序树
#include<stdlib.h> #include<stdio.h> int counter; /*计数器*/ struct tree /*声明书的结构*/ {struct tree *left; int data; struct tree *right; }; typedef struct tree treenode; /*声明新类型树的结构*/ typedef treenode *b_tree; /*声明二叉树的链表*/ b_tree insert_node(b_tree root,int node) /*插入二叉树的结点*/ {b_tree newnode; b_tree currentnode; b_tree parentnode; newnode=(b_tree)malloc(sizeof(treenode)); /*分配新结点空间*/ newnode->data=node; newnode->right=NULL; newnode->left=NULL; if(root==NULL)return newnode; else{currentnode=root; while(currentnode!=NULL) {parentnode=currentnode; if(currentnode->data>node) currentnode=currentnode->left; else currentnode=currentnode->right; } if(parentnode->data>node) parentnode->left=newnode; else parentnode->right=newnode; } return root; /*返回根结点的指针*/ } b_tree create_btree(int *data, int len) /*建立二叉树*/ { b_tree root=NULL; int i; for(i=0;i<len;i++) root=insert_node(root,data[i]); return root; } void inorder(b_tree point) { /*中序遍历二叉树*/ if(point!=NULL) {inorder(point->left); printf("
%d
",point->data); /*处理结点内容*/ inorder(point->right); } } int tree_search(b_tree root,int key) /*查找二叉树*/ { b_tree pointer; pointer=root; counter=0; while(pointer!=NULL) { counter++; if(pointer->data<key) pointer=pointer->right; else if(pointer->data>key) pointer=pointer->left; else if(pointer->data==key) return 1; } return 0; } void main() { b_tree root=NULL; int m,i,index; /*读入输入值所用的暂存变量*/ float max=0; int nodelist[20]; /*声明存储输入数据之数组*/ int value; printf("
Please input the elements of binary tree(exit for 0):
"); index=0; scanf("%d",&value); /*读取输入值放到变量value*/ while(value!=0) {nodelist[index]=value; index=index+1; scanf("%d",&value); } printf("%d",index); root=create_btree(nodelist,index); printf("
the inorder traversal result is :"); inorder(root); printf("
"); for(m=0;m<index;m++) { tree_search(root,nodelist[m]); max+=counter; /*计算查找长度*/ } printf("the average long is: %.2f
",max/index); /*输出average查找长度*/ getch(); }利用二叉排序树对顺序表进行排序 (1)生成一个顺序表L;(2)对所生成的顺序表L构造二叉排序树(3)利用栈结构
用C或C++语言实现的功能:
(1)生成一个顺序表L;
(2)对所生成的顺序表L构造二叉排序树
(3)利用栈结构实现中序遍历二叉排序树;
(4)中序遍历所构造的二叉排序树将记录由小到大输出。
急!急!求二叉排序树程序
#include <iostream.h>
//二叉排序树类的前视定义
template<class Type> class BSTree;
//二叉排序树结点类的定义
template<class Type>
class BSTreeNode
{friend class BSTree <Type>;
private:
Type data; //数据域(在此为关键字)
BSTreeNode <Type> *leftChild, *rightChild;
public:
BSTreeNode():leftChild(NULL),rightChild (NULL){};//构造函数
BSTreeNode(const Type &d) : data (d), leftChild (NULL),rightChild (NULL) {} ; //构造函数
~BSTreeNode(){}; //析构函数
};
//二叉排序树的类定义
template <class Type>
class BSTree
{
private:
BSTreeNode<Type> * root; //二叉排序树的根指针
void Insert ( const Type & x, BSTreeNode<Type> *&p );
//在p为根的二叉排序树上插入数据元素x
void Delete ( const Type & k, BSTreeNode<Type> *&p );
//在p为根的二叉排序树上删除关键字为k的结点
BSTreeNode<Type> * Min(BSTreeNode<Type> *p);
BSTreeNode<Type> * Find(const Type &k, BSTreeNode<Type> *p );
//在p为根的二叉排序树上查找关键字为k的结点
void InOrder(BSTreeNode<Type> *r);
public:
BSTree() : root (NULL) {}//构造函数
Type Find ( const Type &k ) {//查找
BSTreeNode<Type>* p=Find ( k, root );
if(p==NULL){
cout<<"There is not a node that equals "<<k<<endl;
return NULL;
}
return p->data;
}
void Insert(const Type &x) { Insert ( x, root ); } //插入
void Delete(const Type &k) { Delete ( k, root ); } //删除
void SortDisplay(){InOrder(root);cout<<endl;}
};
template <class Type>
void BSTree<Type>::
Insert(const Type &x, BSTreeNode<Type> * &p) {
//在p为根的二叉排序树插入结点的递归算法
if ( p == NULL ) //空二叉树
p = new BSTreeNode<Type>(x); //创建数据元素x的结点
else if (x<p->data )
//在左子树插入
Insert ( x, p->leftChild );
else if ( x > p->data )
//在右子树插入
Insert ( x, p->rightChild );
else //结点x已存在
{
cout << "There has node x" << endl;
}
}
template <class Type>
BSTreeNode<Type> * BSTree<Type> ::
Find (const Type &k, BSTreeNode<Type> *p) {
//在p为根的二叉排序树上进行查找的递归算法
if ( p == NULL ) return NULL; //查找失败
else if ( k<p->data )
//在左子树上递归查找
return Find (k,p->leftChild );
else if (k>p->data )
//在右子树上递归查找
return Find ( k, p->rightChild );
else return p; //相等,查找成功
}
template <class Type>
void BSTree<Type> ::
Delete(const Type &k, BSTreeNode<Type> * &p){
//在p为根的二叉排序树上删除关键字为k的结点
if ( p != NULL ){
BSTreeNode<Type> * temp;
if ( k < p->data )
Delete ( k, p->leftChild );
else if ( k > p->data )
Delete ( k, p->rightChild );
else if ( p->leftChild != NULL && p->rightChild != NULL ){
BSTreeNode<Type> * tparent=p;
temp=p->rightChild;
while(temp->leftChild!=NULL){
tparent=temp;
temp=temp->leftChild;
}
p->data = temp->data;
if(tparent==p)
Delete ( p->data,tparent->rightChild );
else Delete ( p->data,tparent->leftChild );
}
else {
temp = p;
if ( p->leftChild == NULL )
p = p->rightChild;
else if ( p->rightChild == NULL )
p = p->leftChild;
delete temp;
}
}
}
template <class Type>
void BSTree<Type>::InOrder(BSTreeNode<Type>* r){
if(r!=NULL){
InOrder(r->leftChild);
cout<<r->data<<" ";
InOrder(r->rightChild);
}
}
void main (){
BSTree<int> a;
int x;
cout<<"输入序列:"<<" ";
for(int i=0;;i++){
cin>>x;
if(x==-1)break;
a.Insert(x);
}
cout<<endl;
cout<<"输出中序遍历序列应为:";
a.SortDisplay();
cout<<endl;
cout<<"输入需要删除的整数:"<<" ";
int y;
cin>>y;
cout<<endl;
cout<<"查找结果是:"<<a.Find(y)<<endl;
cout<<endl;
a.Delete(y);
cout<<"输出删除后的中序遍历序列应为:"<<" ";
a.SortDisplay();
}构建二叉排序树,依次输入56,78,34,45,85,45,36,91,84,78,两个45和78
题目中应该几乎没有这种情况吧,像这一题,可以先试着把后一个45插到原45的左子树下(其实右子树下也是一样的)然后按照平衡二叉树的规则去做变换,而结果的78由于本题里前一个78的右子树上已经有了节点了,也只能插在做子树上面。
ps:以上都是个人见解,因为实际中并未见过此例~
稳爱云(www.wenaiyun.com)是创建于2021年的国人IDC商家,主要目前要出售香港VPS、香港独立服务器、美国高防VPS、美国CERA VPS 等目前在售VPS线路有三网CN2、CN2 GIA,该公司旗下产品均采用KVM虚拟化架构。机房采用业内口碑最好香港沙田机房,稳定,好用,数据安全。线路采用三网(电信,联通,移动)回程电信cn2、cn2 gia优质网络,延迟低,速度快。自行封装的...
Hostkey.com成立于2007年的荷兰公司,主要运营服务器出租与托管,其次是VPS、域名、域名证书,各种软件授权等。hostkey当前运作荷兰阿姆斯特丹、俄罗斯莫斯科、美国纽约等数据中心。支持Paypal,信用卡,Webmoney,以及支付宝等付款方式。禁止VPN,代理,Tor,网络诈骗,儿童色情,Spam,网络扫描,俄罗斯色情,俄罗斯电影,俄罗斯MP3,俄罗斯Trackers,以及俄罗斯法...
ParkInHost主机商是首次介绍到的主机商,这个商家是2013年的印度主机商,隶属于印度DiggDigital公司,主营业务有俄罗斯、荷兰、德国等机房的抗投诉虚拟主机、VPS主机和独立服务器。也看到商家的数据中心还有中国香港和美国、法国等,不过香港机房肯定不是直连的。根据曾经对于抗投诉外贸主机的了解,虽然ParkInHost以无视DMCA的抗投诉VPS和抗投诉服务器,但是,我们还是要做好数据备...
排序二叉树为你推荐
java队列怎样用java代码实现一个队列支付宝账单查询支付宝电子账单怎么查询小项目家庭办厂小项目有哪些?家庭小厂60个t320T320是3G手机吗,有TX用过吗?怎么样啊?sd卡座我是一家手机生产厂的采购员,想知道按键开关、SD卡座什么厂家生产的好啊。知道的说说。谢谢海淀区公司注册在北京海淀区注册的有限责任公司,要增加自然人股东,需要准备哪些材料?360官网打不开360官网进不了怎么办趋势防毒如何破解趋势防病毒墙安全工程师待遇安全工程就业的待遇怎么样,分销渠道案例关于nike公司的分销渠道以及营销策略?
域名批量查询 cn域名注册 greengeeks kvmla vultr美国与日本 kdata 好看的留言 win8升级win10正式版 警告本网站 发包服务器 嘟牛 京东商城0元抢购 免费吧 免费高速空间 vip域名 web服务器安全 如何注册阿里云邮箱 永久免费空间 购买空间 汤博乐 更多