c++实现二叉搜索树
自己实现了一下二叉搜索树的数据结构。记录一下:
#include <iostream> using namespace std; struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int value) { val=value; left=NULL; right=NULL; }
}; class SearchTree{ public:
SearchTree();
~SearchTree();
void Destory(TreeNode *);
void Insertnode(int);
void Preorder(TreeNode *);
void Inorder(TreeNode *);
void Postorder(TreeNode *);
void Predisplay();
void Indisplay();
void Postdisplay();
private:
TreeNode *root; }; SearchTree::SearchTree()
{
root=NULL;
} SearchTree::~SearchTree()
{
cout<<"析构二叉搜索树:"<<endl;
Destory(root);
} void SearchTree::Destory(TreeNode *node)
{
if(node!=NULL)
{
Destory(node->left);
Destory(node->right);
cout<<node->val<<" ";
delete node;
}
} void SearchTree::Insertnode(int value)
{
if(root==NULL)
root=new TreeNode(value);
else
{
TreeNode *p,*pre;
pre=p=root;
while(p)
{
if(p->val==value)
return;
else if(p->val>value)
{
pre=p;
p=p->left;
}
else
{
pre=p;
p=p->right;
} }
p=new TreeNode(value);
if(pre->val>value)
pre->left=p;
else
pre->right=p;
}
} void SearchTree::Predisplay()
{
Preorder(root);
} void SearchTree::Preorder(TreeNode *root)
{
if(root)
{
cout<<root->val<<" ";
Preorder(root->left);
Preorder(root->right);
}
} void SearchTree::Indisplay()
{
Inorder(root);
} void SearchTree::Inorder(TreeNode *root)
{
if(root)
{
Inorder(root->left);
cout<<root->val<<" ";
Inorder(root->right);
}
} void SearchTree::Postdisplay()
{
Postorder(root);
} void SearchTree::Postorder(TreeNode *root)
{
if(root)
{
Postorder(root->left);
Postorder(root->right);
cout<<root->val<<" ";
}
} int main()
{
SearchTree t;
int a[]={7,4,2,3,15,35,6,45,55,20,1,14};
int n=sizeof(a)/sizeof(a[0]);
cout<<"构造二叉搜索树:"<<endl;
for(int i=0;i<n;++i)
{
cout<<a[i]<<" ";
t.Insertnode(a[i]);
}
cout<<endl<<"先序遍历序列: "<<endl;
t.Predisplay();
cout<<endl<<"中序遍历序列: "<<endl;
t.Indisplay();
cout<<endl<<"后序遍历序列: "<<endl;
t.Postdisplay();
cout<<endl;
return 0;
}
c++实现二叉搜索树的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- 旅行商(sale)
旅行商(sale) 题目描述 camp国有n座城市,由1,2,-,n编号.城市由n–1条双向道路相连.任意两个城市之间存在唯一的道路连通.有m个旅行商,第i个旅行商会从城市ai旅行到城市bi,贩卖ci ...
- java 后台封装json数据学习总结(二)
一.JSONArray的应用 从json数组中得到相应java数组,如果要获取java数组中的元素,只需要遍历该数组. /* * 从json数组中得到相应java数组 * JSONArray下的toA ...
- python和tensorflow安装
一.Python安装 python采用anaconda安装,简单方便,下载python3.6的anaconda linux64的sh安装文件. 1.bash Anaconda-2.1.0-Linux ...
- js复制到粘贴板
http://www.cnblogs.com/52fhy/p/5383813.html(移动端有兼容性问题) 要页面加载完直接绑定事件,否则第一次点击是绑定事件,第二次才触发事件 移动端需要设置tex ...
- 1.docker学习
Docker —— 从入门到实践 http://udn.yyuap.com/doc/docker_practice/introduction/index.html 非常详细的Docker学习教程 ht ...
- react 基础语法复习1- 搭建开发环境
之前有看过阮一峰老师的react教程跟着做了一遍,学习了一下.好久没看,有点忘记了,这次跟着脚手架工具系统的复习一遍.顺便学习学习 react-router 和 redux 首先,脚手架工具我使用的是 ...
- html5 的 webScoket 和 C# 建立Socket连接
最近使用的web项目中,需要服务器直接触发前端显示效果. 所以研究了一下websocket: 名词解释: WebSocketWebSocket协议是一种双向通信协议,它建立在TCP之上,同http一样 ...
- spring boot 排除个别配置类的代码
废话不说,直接上代码 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfigu ...
- 在vscode中使用pylint-django插件解决pylint的一些不必要的错误提示【转】
转自:http://www.cnblogs.com/chaojihexiang/p/6417835.html 微软的vscode编辑器是一个好东西,通过vscode编辑python程序非常的方便.推荐 ...
- java 读写操作
java代码: 写入: public void getNotice(HttpServletRequest request, String notice){ String message = JSON. ...