【数据结构】搜索二叉树的相关操作
2016-03-28 21:53
597 查看
1.查找最小元素
方法1:Position FindMin(BinTree BST) { if(!BST) return NULL; else if(!BST->left) return BST; else return FindMin(BST->left); }
方法2:
int minValue(struct node* node) { struct node* current = node; while(current->left!=NULL) { current = current->left; } return (current->data); }
方法1和方法2实质上没有太大区别。
2.查找最大元素
由查找最小元素的算法同理可以得到查找最大元素的算法如下:方法1:
Position FindMax(BinTree BST) { if(!BST) return NULL; else if(!BST->right) return BST; else return FindMin(BST->right); }
方法2:
int maxValue(struct node* node) { struct node* current = node; while(current->right!=NULL) { current = current->right; } return (current->data); }
3.搜索二叉树插入结点
struct node* insert(struct* node,int data) { if(node==NULL) { node = (node*)malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; } else { if(data<=node->data) node->left = insert(node->left,data); else node->right = insert(node->right,data); } return node; }
相关文章推荐
- 单链表-----数据结构
- 大话数据结构读书笔记系列(三)线性表
- 数据结构,链表基本操作
- 【Redis源码剖析】 - Redis内置数据结构之字符串sds
- poj 2985(并查集+线段树求K大数)
- 树状数组学习
- 可变数据结构的编程
- 基于DFS的求割点算法
- 最小生成树算法
- 最短路径算法
- 数据结构(三)实现AVL树
- PHP数据结构之——链表
- 大话数据结构——栈的应用四则元算表达式求值(后缀表达式计算结果)
- 数据结构与算法(六):技术面试题
- 从零开始_学_数据结构(六)——排序(冒泡、插入、希尔、简单选择、归并、快速)
- python数据结构——层次遍历的两种方法
- 数据结构之七大排序算法
- 二叉查找树的实现
- 【数据结构】队列
- 数据结构 - 求二叉树中结点的最大距离(C++)