二分搜索树排序binary search sorting的c++实现
2011-06-24 11:04
330 查看
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class BST
{
private:
double value;
BST * leftNode;
BST * rightNode;
public:
BST(double v)
{
this->value=v;
this->leftNode=NULL;
this->rightNode=NULL;
}
~BST()
{
}
void addNode(double v) //添加节点
{
if(v<this->value)
{
if(this->leftNode==NULL)
{
this->leftNode=new BST(v);
}
else
{
this->leftNode->addNode(v);
}
}
else
{
if(this->rightNode==NULL)
{
this->rightNode=new BST(v);
}
else
{
this->rightNode->addNode(v);
}
}
}
void walktree() //遍历BST树
{
if(this->leftNode!=NULL)
{
this->leftNode->walktree();
}
cout<<" "<<this->value;
if(this->rightNode!=NULL)
{
this->rightNode->walktree();
}
}
};
int main()
{
cout << "This is a bst search algorithm, enter your number sequence line by line, end with a dot(.) or empty line:" << endl;
string mystr;
double value;
BST* tr=NULL;
do
{
getline(cin,mystr);
if(mystr=="."||mystr=="")
break;
stringstream(mystr) >> value;
if(tr==NULL)
{
tr=new BST(value);
}
else
{
tr->addNode(value);
}
}while(true);
cout<<"After BST sorting, you got the following sequence:/n";
tr->walktree();
return 0;
}
#include <string>
#include <sstream>
using namespace std;
class BST
{
private:
double value;
BST * leftNode;
BST * rightNode;
public:
BST(double v)
{
this->value=v;
this->leftNode=NULL;
this->rightNode=NULL;
}
~BST()
{
}
void addNode(double v) //添加节点
{
if(v<this->value)
{
if(this->leftNode==NULL)
{
this->leftNode=new BST(v);
}
else
{
this->leftNode->addNode(v);
}
}
else
{
if(this->rightNode==NULL)
{
this->rightNode=new BST(v);
}
else
{
this->rightNode->addNode(v);
}
}
}
void walktree() //遍历BST树
{
if(this->leftNode!=NULL)
{
this->leftNode->walktree();
}
cout<<" "<<this->value;
if(this->rightNode!=NULL)
{
this->rightNode->walktree();
}
}
};
int main()
{
cout << "This is a bst search algorithm, enter your number sequence line by line, end with a dot(.) or empty line:" << endl;
string mystr;
double value;
BST* tr=NULL;
do
{
getline(cin,mystr);
if(mystr=="."||mystr=="")
break;
stringstream(mystr) >> value;
if(tr==NULL)
{
tr=new BST(value);
}
else
{
tr->addNode(value);
}
}while(true);
cout<<"After BST sorting, you got the following sequence:/n";
tr->walktree();
return 0;
}
相关文章推荐
- 二分查找树的C++实现 Binary Search Tree(BST)
- PAT程序设计考题——甲级1043(Is It a Binary Search Tree ) C++实现
- 【LeetCode-面试算法经典-Java实现】【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】
- Ackerman函数的递归、全排列的递归实现、整数划分的递归、二分搜索的递归、合并排序的递归、.快速排序
- [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
- convert-sorted-list-to-binary-search-tree(转换排序列表到二进制搜索树)
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- Java使用分治算法实现排序数索引功能示例【二分搜索】
- 编程珠玑第五章习题五——C++实现二分搜索时进行错误检测
- c++ 二分查找的函数 lower_bound & upper_bound & binary_search
- Binary Search Tree的实现 C++版
- 【IOS开发】搜索和排序(好友列表,通讯录的实现,searchbar)
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
- Java 分治算法实现排序数索引(二分搜索)
- 数据结构图文解析之:直接插入排序及其优化(二分插入排序)解析及C++实现
- 二分查找/二分搜索(binary_search)详解
- 二分查找:binary_search、lower_bound和upper_bound的实现
- c++ 二分查找的函数 lower_bound & upper_bound & binary_search