20120919-二叉树 数据结构《数据结构与算法分析》
时间:2022-04-22
本文章向大家介绍20120919-二叉树 数据结构《数据结构与算法分析》,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
又是一次的毕业季,羡慕嫉妒啊....
二叉查找树类的框架:
1 template <typename Comparable>
2 class BinarySearchTree
3 {
4 public:
5 BinarySearchTree();
6 BinarySearchTree(const BinarySearchTree & rhs)
7 ~BinarySearchTree();
8
9 const Comparable & findMin() const;
10 const Comparable & findMax() const;
11
12 bool contains(const Comparable & x ) const;
13 bool isEmpty() const;
14 void printTree() const;
15
16 void makeEmpty();
17 void insert(const Comparable & x);
18 void remove(const Comparable & x);
19
20 const BinarySearchTree & operator = (const BinarySearchTree & rhs);
21
22 private:
23 struct BinaryNode
24 {
25 Comparable element;
26 BinaryNode *left;
27 BinaryNode *right;
28
29 BinaryNode(const Comparable & theElement,BinaryNode *lt,BinaryNode *rt):element(theElement),left(lt),right(rt){}
30 };
31
32 BinaryNode *root;
33
34 void insert (const Comparable & x,BinaryNode * & t) const;
35 void remove (const Comparable & x ,BinaryNode * & t) const;
36 BinaryNode * findMin(BinaryNode *t) const;
37 BinaryNode * findMax(BinaryNode *t) const;
38 bool contains( const Comparable & x,BinaryNode * t) const;
39 void makeEmpty( BinaryNode * & t);
40 void printTree(BinaryNode *t) const;
41 BinaryNode * clone(BinaryNode *t) const;
42 };
contains insert remove三种操作递归列表:
bool contains (const Comparable & x) const
{
return contains(x,root)
}
void insert(const Comparable & x)
{
insert (x,root);
}
void remove(const Comparable & x)
{
remove(x,root);
}
二叉查找树的contains操作:
1 bool contains(const Comparable & x,BinaryNode * t) const
2 {
3 if( t == NUll )
4 return false;
5 else if ( x < t->element )
6 return contains(x,t->left );
7 else if (t->element < t)
8 return contains(x,t->right);
9 else
10 return true;
11 }
使用函数对象 实现 二叉查找树:
template <typename Object,typename Comparator = less<Object>>
class BinarySearchTree
{
public:
private:
BinaryNode * root;
Comparable isLessThan;
bool contains( const Object & x,BinaryNode *t ) const
{
if(t == NULL)
return false;
else if (isLessThan(x,t->element))
return contains(x,t->left);
else if (isLessThan(t->element,x))
return contains(x,t->right);
else
return true;
}
};
findMin方法的递归实现:
1 BinaryNode * findMin( BinaryNode * t) const
2 {
3 if( t == NULL)
4 return NULL;
5 if(t->left == NULL)
6 return t;
7 return findMin(t->left);
8 }
findMax方法的递归实现:
1 BinaryNode * findMax(BinaryNode * t) const
2 {
3 if(t != NULL)
4 while( t ->right !=NULL)
5 t = t->right;
6 return t;
7 }
二叉查找树插入操作:
1 void insert( const Comparable & x,BinaryNode * & t )
2 {
3 if( t== NULL)
4 t = new BinaryNode(x,NULL,NULL);
5 else if (x<t->element)
6 insert(x,t->left);
7 else if (t->element < x)
8 insert(x,t->right);
9 else
10 ;
11 }
二叉查找树 删除操作:
1 void remove (const Comparable & x,BinaryNode * & t)
2 {
3 if( t == NULL)
4 return;
5 if( x < t->element)
6 remove( x,t->left);
7 else if ( t->element < x)
8 remove(x,t->right);
9 else if (t->left != NULL && t->right!=NULL )
10 {
11 t->element = findMin( t->right)->element;
12 remove(t->element , t->right);
13 }
14 else
15 {
16 BinaryNode *oldNode = t;
17 t = ( t->left !=NULL) ? t->left : t->right;
18 delete oldNode;
19 }
20 }
析构函数递归实现makeEmpty
~BinarySearchTree()
{
makEmpty();
}
void makeEmpty(BinaryNode * & t)
{
if( t != NULL)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
operator= 递归实现clone:
1 const BinarySearchTree & operator=( const BianrySearchTree & rhs)
2 {
3 if(this != &rhs)
4 {
5 makeEmpty();
6 root = clone(rhs.root);
7 }
8 return *this;
9 }
10
11 BinaryNode * clone( BinaryNode * t) const
12 {
13 if( t == NULL)
14 return NULL;
15 return new BinaryNode ( t->element,clone(t->left),clone(t->right));
16 }
- YOLO,一种简易快捷的目标检测算法
- Java基础-22总结转换流,随机访问流,合并流,序列化流,Properties
- openshift镜像构建
- 关于数据库无法登录的问题反思(r6笔记第49天)
- SQL中distinct的用法
- 数据结构是哈希表(hashTable)(一)
- 关于奇怪的并行进程分析(三)(r6笔记第47天)
- 数据结构是哈希表(hashTable)(二)
- Oracle的Replace函数与translate函数详解与比较
- 关于奇怪的并行进程分析(二) (r6笔记第46天)
- Linux性能分析工具与图形化方法
- MySQL和Oracle中的隐式转换(r6笔记第45天)
- R语言的数据导入与导出(write.table,CAT)
- gqlplus的简单使用(r6笔记第43天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- vuejs小例子之实现图片的切换
- c++之函数模板案例
- 【python-leetcode15-双指针】最接近的三数之和
- c++之普通函数和模板函数的区别
- 【python-leetcode15-双指针】三个数之和为零
- 百度easydl之图像分类构建是否佩戴口罩模型
- c++之普通函数和模板函数的调用规则
- 解决github打不开问题
- vuejs之第一个vue程序
- c++之函数模板的局限性
- c++之模板-类模板
- springboot监控之监控管理
- c++之类模板和函数模板的区别
- 【python-leetcode977-双指针】有序数组的平方
- springboot分布式之springcloud-Eureka注册中心