二叉查找树 ADT
来源:互联网 发布:容我喝一杯82年的java 编辑:程序博客网 时间:2024/06/06 00:55
二叉树是每个节点最多有两个子树的数结构,即左子树、右子树。二叉树的第i层至多有2^(i-1)个节点,深度为k的二叉树至多有(2^k) - 1个节点。
二叉查找树:对于树中的每个节点X,他的左子树中所有关键字值小于X的关键字值,而它的右子树中所有关键字值大于X的关键字值。
二叉搜索树可以方便的实现搜索算法。在搜索元素x的时候,我们可以将x和根节点比较:
如果x等于根节点,那么找到x,停止搜索 (终止条件)
如果x小于根节点,那么搜索左子树
如果x大于根节点,那么搜索右子树
二叉搜索树所需要进行的操作次数最多与树的深度相等。n个节点的二叉搜索树的深度最多为n,最少为log(n)。
二叉树的基本操作:
/*二叉树的一些基本操作*/#include <stdio.h>#include <stdlib.h>typedef struct node *position;typedef int ElementType;struct node //定义树节点{ position parent; ElementType element; position lchild; position rchild;};typedef struct node *TREE;void print_sorted_tree(TREE); //打印树position find_min(TREE); //寻找最小元素position find_max(TREE); //寻找最大元素position fina_value(TREE, ElementType); //寻找指定元素position insert_value(TREE, ElementType); //插入一个指定元素ElementType delete_node(position); //删除节点static int is_root(position); //判断是否是根节点static int is_leaf(position); //判断是否是树叶static ElementType delete_leaf(position); //删除叶节点static void insert_node_to_nonempty_tree(TREE, position); //插入节点int main(){ TREE tree; position np; ElementType element; tree = NULL; int a[] = {18, 5, 2, 8, 81, 101}; for(int i = 0; i < 6; i++) tree = insert_value(tree, a[i]); printf("Original tree: \n"); print_sorted_tree(tree); np = fina_value(tree, 81); if (np != NULL) { delete_node(np); printf("\nAfter deletion:\n"); print_sorted_tree(tree); } system("pause"); return 0;}void print_sorted_tree(TREE tr){ if(tr == NULL) return; print_sorted_tree(tr->lchild); printf("%d \n", tr->element); print_sorted_tree(tr->rchild);}position find_min(TREE tr){ position np; np = tr; if( np == NULL) return NULL; while (np ->lchild != NULL) { np = np->lchild; } return np;}position find_max(TREE tr){ position np; np = tr; if(np == NULL) return NULL; while(np->rchild != NULL) np = np->rchild; return np;}position fina_value(TREE tr, ElementType value){ if(tr == NULL) return NULL; if ( tr->element == value) { return tr; } else if(value < tr->element) return fina_value(tr->lchild, value); else return fina_value(tr->rchild, value);}ElementType delete_node(position np){ position replace; ElementType element; if(is_leaf(np)) return delete_leaf(np); else { replace = (np->lchild!= NULL)? find_max(np->lchild) : find_min(np->rchild); element = np->element; np->element = delete_node(replace); return element; }}position insert_value(TREE tr, ElementType value){ position np; np = (position) malloc(sizeof (struct node)); np->element = value; np->parent = NULL; np->rchild = NULL; np->lchild = NULL; if(tr == NULL) tr = np; else insert_node_to_nonempty_tree(tr, np); return tr;}static int is_root(position np){ return (np->parent == NULL);}static int is_leaf(position np){ return (np->lchild == NULL && np->rchild == NULL);}static ElementType delete_leaf(position np){ ElementType element; position parent; element = np->element; parent = np->parent; if (!is_root(np)) { if(parent->lchild == np) parent->lchild =NULL; else parent->rchild = NULL; } free(np); return element;}static void insert_node_to_nonempty_tree(TREE tr, position np){ if ( np->element <= tr->element) { if (tr->lchild == NULL) { tr->lchild = np; np->parent = tr; return; } else insert_node_to_nonempty_tree(tr->lchild, np); } else if (np->element > tr->element) { if (tr->rchild == NULL) { tr->rchild = np; np->parent = tr; return; } else insert_node_to_nonempty_tree(tr->rchild, np); }}
参考:树及二叉树
二叉树维基百科
0 0
- 二叉查找树 ADT
- ADT 二叉查找树
- ADT 二叉查找树
- 二叉查找树 ADT实现
- 查找树ADT-二叉查找树
- 查找树ADT之二叉查找树
- 查找树ADT--二叉查找树
- 查找树ADT--二叉查找树
- JAVA之查找二叉树ADT
- 数据结构(scheme) -- 抽象数据类型(ADT) -- 二叉查找树
- 数据结构学习——二叉查找树ADT(程序化)
- ADT(二叉查找树)和AVL(平衡二叉树)总结
- 二叉树ADT
- ADT-二叉树
- 二叉树 ADT 的实现
- 查找--二叉查找树
- 二叉树、二叉查找树
- 二叉树 & 二叉查找树
- php配置及原理(资料)
- 初,SQLLIGOD
- 当ecplise中的tomcat6不能使用,只能使用tomcat7的时候
- 17、Objective-C在ARC下结合GCD的单例模式和宏模版
- gnuplot
- 二叉查找树 ADT
- 合并果子(multiset)
- 图像分割
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
- JS原型学习笔记
- 一起talk Vim吧(第二回:Vim变脸)
- 图像的腐蚀与膨胀
- 黑马程序员_Java语言基础_循环结构
- 《C语言及程序设计》实践参考——带姓名的成绩单