数据结构与算法分析-查找二叉树
2008-11-21 08:57
113 查看
由于本人初学数据结构与算法,此写的为学习心得,希望园内的朋友给予指点。
查找二叉树的定义,所有节点的左子树均比该结点小,右子树均比该节点大。如下所示为一个正解的查找二叉树:
6
5 7
1 9
8 10
根据定义,查找二叉树的节点应包含一个存储数据,两个指针,分别指向节点的左、右子树,如下所示。
Code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "BTree.h"
using namespace std;
int main(int argc, char *argv[])
{
//随机生成一棵树
BTree<int> tree;
srand((unsigned)time(NULL));
for(int i=0; i<20; ++i)
{
tree.Insert(rand() / 20);
}
cout << "前序:" << endl;
tree.PreOrder();
cout << endl;
cout << "中序" << endl;
tree.InOrder();
cout << endl;
cout << "后序" << endl;
tree.PostOrder();
cout << endl;
cout << "层次" << endl;
tree.LevelOrder();
cout << endl;
if(tree.Find(14))
{
cout << "14 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "14 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
if(tree.Find(30))
{
cout << "30 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "30 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
if(tree.Find(3))
{
cout << "3 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "3 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
if(tree.Find(5))
{
cout << "5 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "5 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
return 0;
}
以上代码在本人电脑中测试通过,如发现问题与代码的不当之请告之本人。
查找二叉树的定义,所有节点的左子树均比该结点小,右子树均比该节点大。如下所示为一个正解的查找二叉树:
6
5 7
1 9
8 10
根据定义,查找二叉树的节点应包含一个存储数据,两个指针,分别指向节点的左、右子树,如下所示。
Code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "BTree.h"
using namespace std;
int main(int argc, char *argv[])
{
//随机生成一棵树
BTree<int> tree;
srand((unsigned)time(NULL));
for(int i=0; i<20; ++i)
{
tree.Insert(rand() / 20);
}
cout << "前序:" << endl;
tree.PreOrder();
cout << endl;
cout << "中序" << endl;
tree.InOrder();
cout << endl;
cout << "后序" << endl;
tree.PostOrder();
cout << endl;
cout << "层次" << endl;
tree.LevelOrder();
cout << endl;
if(tree.Find(14))
{
cout << "14 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "14 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
if(tree.Find(30))
{
cout << "30 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "30 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
if(tree.Find(3))
{
cout << "3 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "3 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
if(tree.Find(5))
{
cout << "5 is in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
else
{
cout << "5 is not in the tree,search for " ;
tree.PrintStatic();
cout << endl;
}
return 0;
}
以上代码在本人电脑中测试通过,如发现问题与代码的不当之请告之本人。
相关文章推荐
- 数据结构与算法分析 c++11 查找二叉树 BinarySearchTree
- 二叉树中查找两个节点的最低公共祖先
- 冒泡排序,快速排序,插入排序以及二叉树查找的O级时间的比较
- 二叉树构建(查找,增加,删除,前、中、后序遍历)
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 每周数据结构【5】:在二叉树中递归查找为data值的点
- 二叉树查找、匹配字符串、快速排序
- 数据结构与算法(c++)——查找二叉树与中序遍历
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 十二、平衡二叉树(2-3查找树、红黑二叉树)
- 查找二叉树任意两个节点的父节点
- 第十四周 项目2--二叉树排序树中查找的路径
- 查找二叉树(树)
- 查找二叉树的简单认识
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周项目2-二叉树排序树中查找的路径
- 第十四周项目二 二叉树排序树中查找的路径
- 第14周项目2-二叉树排序树中查找的路径
- 二叉树公共祖先节点的查找
- 二叉树之查找指定节点所在的层数