[root@VM_253_237_tlinux ~/tree/print]# cat ctree.h
typedef struct node *link;
struct node{
int item;link l,r;
};
void print_tree(struct node * root);
#include <math.h>
#include <curses.h>
#include "ctree.h"
static void recur_print( struct node * node, int line, int col ){
if( node == NULL ){
move( line, col ) ;
addstr("*");//此处为加*号的代码,用来占位,可去掉。
return ;
}
int t = COLS/pow( 2, line+2 );
char buf[9];
sprintf(buf,"%-d", node->item ) ;
move( line , col ) ;
addstr( buf ) ;
if( node->l != NULL || node->r != NULL ){
move(line+1, col-t-1 ) ;
addstr("[");
move(line+1, col+t+3) ;
addstr("]");
}
recur_print( node->l, line+1, col-t ) ;
recur_print( node->r, line+1, col+t ) ;
}
void print_tree(struct node * root){
initscr() ;
clear();
recur_print(root, 0, COLS/2);
move(LINES-1, COLS-1) ;
refresh() ;
getchar() ;
endwin() ;
}
[root@VM_253_237_tlinux ~/tree/print]# cat bst.c
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "ctree.h"
#define N 10
link NODE(int item,link l,link r){
link t=malloc(sizeof *t);
t->item=item;t->l=l;t->r=r;
return t;
}
link insert_node(link t,int item){
if(t==NULL) return NODE(item,NULL,NULL);
if(item<t->item)
t->l=insert_node(t->l,item);
else
t->r=insert_node(t->r,item);
return t;
}
void pprint(link t){
printf("(");
if(t!=NULL){
printf("%d",t->item);
pprint(t->l);
pprint(t->r);
}
printf(")");
}
int bst_search(link t,int item){
if(t==NULL) return 0;
if(item<t->item)return bst_search(t->l,item);
else if(item>t->item) return bst_search(t->r,item);
else return 1;
}
link bst_remove(link t,int item){
if(t==NULL) return NULL;
if(item<t->item)
t->l=bst_remove(t->l,item);
else if(item>t->item){
t->r=bst_remove(t->r,item);
}else {
link x;
if(t->l){
for(x=t->l;x->r;x=x->r){;}
t->item=x->item;
t->l=bst_remove(t->l,t->item);
}else if(t->r){
for(x=t->r;x->l;x=x->l){;}
t->item=x->item;
t->r=bst_remove(t->l,t->item);
}else{
free(t);t=NULL;
}
}
return t;
}
int main(){
srand(time(NULL));
int i ;link root=NULL;
for(i=0;i<N;i++) root =insert_node(root,rand()%100);
printf("\t\\tree");pprint(root);
print_tree(root);
return 0;
}
执行:
gcc -lcurses -lm bst.c ctree.c
跟那个还是有点差距
不过也差不多了哈哈哈
- 大小: 7.1 KB
- 大小: 70.9 KB
分享到:
相关推荐
平衡二叉树-红黑树的实现
数据结构 树、二叉树、红黑树、堆、图等结构教程
数据结构 和算法分析 二叉树、红黑树、堆、图等数据结构
高级数据结构代码 红黑树 二叉树 B树
描述了红黑树的基本结构 以及与二叉树的性能比较
该生成器可以根据用户的要求生成任何用户所需要的类型的二叉树
talerbtree红黑树二叉树.zip
2,为什么索引结构默认使用B+Tree,而不是Hash,二叉树,红黑树? 3、MySQL里记录货币用什么字段类型好 4、数据库自增主键可能遇到什么问题。 5、从锁的类别角度讲,MySQL都有哪些锁呢? 6、索引失效情况? 7、优化...
红黑树的本质是二叉树,二叉树在插入元素的时候是根据关键字(可以理解为用来识别每个节点的id,一般是hash 来判断)的大小来判断插入到哪一个分支的,如下图所示: (备注:大写字母代表每个节点相当于每个节点的...
红黑树&二叉树
B树和二叉树的区别:二叉树最多能有两个子节点;B树最多只能有M个子节点,最少有三个子节点 三、B+树 介绍:B+树是B树的升级版本,就目前情况,绝大部分都已经用B+树代替了B树了,文件管理、索引等等 四、红黑树 ...
红黑树是一种特化的AVL树(平衡二叉树),都是在进行插入和删除操作时通过特定操作保持二叉查找树的平衡,从而获得较高的查找性能。 它虽然是复杂的,但它的最坏情况运行时间也是非常良好的,并且在实践中是高效的:...
主要实现有普通树的生成、查询方法,与继承于普通树的二叉树、红黑树的实现。
二叉树 非递归实现 数据结构 c++ 广度遍历,非递归实现广度遍历生成二叉树。 数据结构与算法上机作业。
1按先序次序输入二叉树中结点的值(一个字符),`0`表示空树,生成二叉树的二叉链表存储结构。然后按中序和后序顺序遍历二叉树输出结果。
这是一个关于二叉树的代码,演示了红黑树的数据结构。仅供大家学习和参考!
26红黑树 27红黑树_0 28红黑树_1 29红黑树_2 30红黑树_3 31红黑树_4 32红黑树_5 33红黑树_6 34堆 35堆排序 36哈希与映射的概述 37B树有什么用 38B树的概念 39图_邻接矩阵 40图_邻接表 41图_DFS 42图_BFS
用先序生成二叉树,然后按先序,中序,后序和层次对二叉树进行排序
使用java语言编程实现了平衡二叉树、二叉树、二叉搜索树、红黑树四种树相关的数据结构,还实现了多种排序算法。并且是在J2EE下实现的。
第一,散列表中的数据是无序存储的,如果要输出有序的数据,需要先进行排序 第二,散列表扩容耗时很多,而且当遇到散列冲突时,性能不稳定,尽管二叉查找树的性能不稳定,