| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1098 人关注过本帖
标题:刮风啦,下雨啦,种树啦,大家乘凉啦~
取消只看楼主 加入收藏
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
 问题点数:0 回复次数:1 
刮风啦,下雨啦,种树啦,大家乘凉啦~

嘿嘿,不多言语,小道一篇,愿闻者入 诚可谓之经典的二叉排序树,常言道有树好乘凉~^—_—^||| #include <iostream.h>

struct tree { int data; tree *right,*left; };

class Btree { tree *root; public: Btree() { root=NULL; } void create_btree(int); void display() { inorder(root); cout<<endl; } void inorder( tree* ); };

void Btree::create_btree(int x) { tree *newnode=new tree; newnode->data=x; newnode->right=newnode->left=NULL; if(root==NULL) root=newnode; else { tree *back; tree *current=root; while(current!=NULL) { back=current; if(current->data>x) current=current->left; else current=current->right; } if(back->data>x) back->left=newnode; else back->right=newnode; } }

void Btree::inorder(tree *tmp) { if(tmp!=NULL) { inorder(tmp->left); cout<<tmp->data<< " "; inorder(tmp->right); } }

int main() { Btree A; int arr[]={7,4,1,5,12,8,13,11}; cout<<"建立排序二叉树顺序:"<<endl; for( int i=0 ; i<8 ; i++ ) { cout<<arr[i]<<" "; A.create_btree(arr[i]); } cout<<endl<<"中序遍历序列:"<<endl; A.display(); return 0; }

[此贴子已经被作者于2005-1-14 20:53:33编辑过]

搜索更多相关主题的帖子: tree void 乘凉 root int 
2005-01-14 20:50
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 
我试过了,能完成排序要求。
如果斑竹大人真的碰到问题,还请明言问题在哪,在下愿意受教。

2005-01-15 21:28
快速回复:刮风啦,下雨啦,种树啦,大家乘凉啦~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017708 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved