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 ...
随机推荐
- EasyUI序列化提交学习总结
jquery easyui将form表单元素的值序列化成对象 form表单 <form id="ff"> <input type="text" ...
- Java语法糖(二)
语法糖之四:内部类 内部类:顾名思义,在类的内部在定义一个类.内部类仅仅是编译时的概念,编译成字节码后,内部类会生成单独的Class文件. 四种:成员内部类.局部内部类.匿名内部类.静态内部类. 1. ...
- HDOJ 1398 Square Coins
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 如何将Windows8系统的磁盘格式(GPT格式)转换成Windows 7系统的磁盘格式(MBR格式)
知识点分析:随机预装Win8的电脑,磁盘为GPT格式的,如果需要安装Win7等早期版本系统,需要转换为MBR格式的,使用Diskpart命令即可完成转换.操作步骤: 注意:转换磁盘格式需要清空磁盘中的 ...
- 关于超大binlog事件的问题
我手里维护了一个项目,其功能是用Java模拟一个MariaDB的slave库连接到主库,对从主库传输过来的binlog事件进行监听与分析 碰到一个问题是: 如果主库做了一个很大的修改操作(比方说直接d ...
- 2018年东北农业大学春季校赛 I wyh的物品【01分数规划/二分】
链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...
- POJ 1961 Period KMP算法之next数组的应用
题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...
- [JLOI2015]有意义的字符串
4002: [JLOI2015]有意义的字符串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1000 Solved: 436[Submit][St ...
- 求用delphi编写的LRC校验位算法函数,急!!!
求用delphi编写的LRC校验位算法函数,急!!! 某命令串为":010200000001FC" 其16进制为“3A 30 31 30 32 30 30 30 30 30 30 ...
- 80端口被屏蔽解决方法,80端口穿透之NAT端口映射技术
介绍一种NAT端口映射技术应用,达到80端口穿透目的,解决80端口被屏蔽的问题,也是80端口被屏蔽解决方法中经常用到的. 80端口穿透类似80端口转发,因为80端口被屏蔽,在数据层面来说是不能直接访问 ...