#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
////////
template <typename T>
class BinarySearchTree{
/////////
private:
struct BinaryNode{
T element;
BinaryNode* left;
BinaryNode* right;
BinaryNode(T ele, BinaryNode* lt, BinaryNode* rt): element(ele),
left(lt), right(rt){}
};
BinaryNode* root;
BinaryNode* clone(BinaryNode* t)const{
if(t == NULL)
return NULL;
return new BinaryNode(t->element, clone(t->left), clone(t->right));
}
void makeEmpty(BinaryNode* &t){//mind here
if(t != NULL){
makeEmpty(t->left);
makeEmpty(t->right);
delete t;//mind here...
}
t = NULL;
}
////////
public:
BinarySearchTree(){
root = new BinaryNode(, NULL, NULL);
}
BinarySearchTree(const BinarySearchTree& rhs): root(clone(rhs.root)){}
const BinarySearchTree& operator=(const BinarySearchTree& rhs){
if(this != &rhs){
makeEmpty(this->root);
root = clone(rhs.root);
}
return *this;
}
~BinarySearchTree(){
if(root != NULL)
makeEmpty(root);
}
/// concrete algorithms
BinaryNode* findMax(const BinaryNode* t){
if(t == NULL)
return NULL;
if(t->right == NULL)
return t;
return findMax(t->right);
}
///
BinaryNode* findMin(const BinaryNode* t){
if(t == NULL)
return NULL;
if(t->left == NULL)
return t;
return findMin(t->left);
} bool isBinarySearchTree(const BinaryNode* root){
if(root != NULL){
if(root->right == NULL && root->left == NULL)
return true;
if(root->left != NULL && root->right == NULL
&& root->left->element < root->element)
return isBinarySearchTree(root->left);
if(root->left == NULL && root->right != NULL
&& root->element < root->right->element)
return isBinarySearchTree(root->right);
if(root->left && root->right
&& root->element < root->right->element
&& root->left->element < root->element)
return isBinarySearchTree(root->left) && isBinarySearchTree(root->right);
return false;
}
return true;
}
void numofNode(const BinaryNode* root, int& num){
if(root != NULL){
if(root->left == NULL && root->right == NULL)
++num;
if(root->left != NULL && root->right == NULL){
++num;
numofNode(root->left, num);
}
if(root->left == NULL && root->right != NULL){
++num;
numofNode(root->right, num);
}
if(root->left != NULL && root->right !=NULL){
++num;
numofNode(root->left, num);
numofNode(root->right, num);
}
}
} void numofLeafNode(const BinaryNode* root, int& num){
if(root != NULL){
if(root->left == NULL && root->right == NULL){
++num;
}
if(root->left != NULL && root->right == NULL){
numofLeafNode(root->left, num);
}
if(root->left == NULL && root->right != NULL){
numofLeafNode(root->right, num);
}
if(root->left != NULL && root->right !=NULL){
numofLeafNode(root->left, num);
numofLeafNode(root->right, num);
}
}
} void numoffullNode(const BinaryNode* root, int& num){
if(root != NULL){
if(root->left == NULL && root->right == NULL){
++num;
}
if(root->left != NULL && root->right == NULL){
numoffullNode(root->left, num);
}
if(root->left == NULL && root->right != NULL){
numoffullNode(root->right, num);
}
if(root->left != NULL && root->right !=NULL){
numofLeafNode(root->left, num);
numofLeafNode(root->right, num);
}
}
} ////
bool contains(const T& x, const BinaryNode* t){
if(t == NULL)
return false;
if(x < t->element)
return contains(x, t->left);
else if(x > t->right)
return contains(x, t->right);
else
return true;
}
///
void insert_s(const T& x, BinaryNode* & t){
if(t == NULL)
t = new BinaryNode(x, NULL, NULL);
if(x < t->element)
insert_s(x, t->left);
else if(x > t->element)
insert_s(x, t->right);
else
;
}
void insert(const T& x){
insert_s(x, root);
}
///
void remove_s(const T& x, BinaryNode* & t){
if(t == NULL)
return;
if(x < t->element)
remove_s(x, t->left);
else if(x, t->element)
remove_s(x, t->right);
else if(t->right != NULL && t->left != NULL){
t->element = findMin(t->right)->element;
remove_s(t->element, t->right);
}
else{
BinaryNode* old = t;
t = (t->left != NULL)? t->left: t->right;
delete old;
}
}
void remove(const T& x){
remove_s(x, root);
}
///
void printTree_s(const BinaryNode* t){
if(t != NULL){
printTree_s(t->left);
printTree_s(t->right);
cout << t->element << endl;
}
}
void printTree(){
printTree_s(root);
} //前序遍历
void PreOrderTraverseRec(const BinaryNode* root){
if(root != NULL){
cout << root->element << endl;
PreOrderTraverseRec(root->left);
PreOrderTraverseRec(root->right);
}
}
void PreOrderTraverseNonRec_1(const BinaryNode* root){
if(root == NULL){
cout << "enmpty tree...\n";
}
else{
stack<BinaryNode*> st;
BinaryNode* curr = root;
st.push(curr);
while(!st.empty()){
curr = st.top();
cout << curr->element << endl;
st.pop();
if(curr->right != NULL)
st.push(curr->right);
if(curr->left != NULL)
st.push(curr->left);
}
}
} void PreOrderTraverseNonRec_2(const BinaryNode* root){
if(root == NULL)
cout << "empty tree...\n";
else{
stack<BinaryNode*> st;
BinaryNode* curr = root;
while(!st.empty() || curr != NULL){
while(curr != NULL){
st.push(curr);
cout << curr->element << endl;
curr = curr->left;
}
if(!st.empty()){
curr = st.top();
st.pop();
curr = curr->right;
}
}
}
}
void PreOrderTraverseMorris(const BinaryNode* root){
BinaryNode* curr = root;
BinaryNode* pre = NULL;
while(curr != NULL){
if(curr->left == NULL){
cout << curr->element << endl;
curr = curr->right;
}
else{
pre = curr->left;
while(pre->right != NULL && pre->right != curr)
pre = pre->right;
if(pre->right == NULL){
cout << curr->element << endl;
pre->right = curr;
curr = curr->left;
}
else{
pre->right = NULL;
curr = curr->right;
}
}
}
} //中序遍历
void InOrderTraverselRec(const BinaryNode* root){
if(root != NULL){
InOrderTraverselRec(root->left);
cout << root->element << endl;
InOrderTraverselRec(root->right);
}
} void InOrderTraverselNonRec(const BinaryNode* root){
if(root == NULL)
cout << "empty tree...\n";
else{
stack<BinaryNode*> st;
BinaryNode* curr = root;
while(!st.empty() || curr != NULL){
while(curr != NULL){
st.push(curr);
curr = curr->left;
}
if(!st.empty()){
curr = st.top();
st.pop();
cout << curr->element << endl;
curr = curr->right;
}
}
}
}
void InOrderTraverselMorris(const BinaryNode* root){
BinaryNode* curr = root;
BinaryNode* pre = NULL;
while(curr != NULL){
if(curr->left == NULL){
cout << curr->element << endl;
curr = curr->right;
}
else{
pre = curr->left;
while(pre->right != NULL && pre->right != curr)
pre = pre->right;
if(pre->right == NULL){
pre->right = curr;
curr = curr->left;
}
else{
pre->right = NULL;
cout << curr->element << endl;
curr = curr->right;
}
}
}
}
//后续遍历
void PostOrderTraverseRec(const BinaryNode* root){
if(root != NULL){
PostOrderTraverseRec(root->left);
PostOrderTraverseRec(root->right);
cout << root->element;
}
} void PostOrderTraverseNonRec_1(const BinaryNode* root){
if(root == NULL)
cout << "empty tree...\n";
else{
stack<BinaryNode*> st;
BinaryNode* curr = root;
BinaryNode* pre = NULL;
while(!st.empty() || curr != NULL){
while(curr != NULL){
st.push(curr);
curr = curr->left;
}
if(!st.empty()){
curr = st.top();
if(curr->right == NULL || pre == curr->right){
cout << curr->element << endl;
st.pop();
pre = curr;
curr == NULL;
}
else
curr = curr->right;
}
}
}
} void PostOrderTraverseNonRec_2(const BinaryNode* root){
if(root == NULL)
cout << "empty tree...\n";
else{
stack<BinaryNode*> st;
BinaryNode* curr = root;
BinaryNode* pre = NULL;
st.push(curr);
while(!st.empty() || curr != NULL){
curr = st.top();
if((curr->left == NULL && curr->right == NULL) ||
((curr->right == NULL && pre == curr->left) ||
pre == curr->right)){
st.pop();
cout << curr->element;
pre = curr;
}
else{
if(curr->right != NULL)
st.push(curr->right);
if(curr->left != NULL)
st.push(curr->left);
}
}
}
} void PostOrderTraverseNonRec_3(const BinaryNode* root){
stack<BinaryNode*> st;
BinaryNode* curr = root;
st.push(curr);
st.push(curr);
while(!st.empty()){
curr = st.top();
st.pop();
if(!st.empty() && curr == st.top()){
if(curr->right){
st.push(curr->right); st.push(curr->right);
}
if(curr->left){
st.push(curr->left); st.push(curr->left);
}
}
else
cout << curr->element << endl;
}
}
}; int main(){
BinarySearchTree<int> lt;
lt.insert();
lt.insert();
lt.insert(-);
lt.printTree();
return ;
}

二叉查找树的C++实现的更多相关文章

  1. 数据结构:二叉查找树(C语言实现)

    数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...

  2. 数据结构笔记--二叉查找树概述以及java代码实现

    一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...

  3. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  4. 平衡二叉查找树(AVL)的理解与实现

    AVL树的介绍 平衡二叉树,又称AVL(Adelson-Velskii和Landis)树,是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它必须保证树的深度是 O(log N).一棵AVL ...

  5. 二叉查找树 C++实现(含完整代码)

    一般二叉树的查找是通过遍历整棵二叉树实现,效率较低.二叉查找树是一种特殊的二叉树,可以提高查找的效率.二叉查找树又称为二叉排序树或二叉搜索树. 二叉查找树的定义 二叉排序树(Binary Search ...

  6. 数据结构——二叉查找树、AVL树

    二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...

  7. Java for LintCode 验证二叉查找树

    给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值.    节点的右子树中的值要严格大于该节点的值.    左右子树也必须是二叉查找树. ...

  8. 数据结构和算法 – 9.二叉树和二叉查找树

      9.1.树的定义   9.2.二叉树 人们把每个节点最多拥有不超过两个子节点的树定义为二叉树.由于限制子节点的数量为 2,人们可以为插入数据.删除数据.以及在二叉树中查找数据编写有效的程序了. 在 ...

  9. 二叉树-二叉查找树-AVL树-遍历

    一.二叉树 定义:每个节点都不能有多于两个的儿子的树. 二叉树节点声明: struct treeNode { elementType element; treeNode * left; treeNod ...

  10. 二叉查找树的Java实现

    为了克服对树结构编程的恐惧感,决心自己实现一遍二叉查找树,以便掌握关于树结构编程的一些技巧和方法.以下是基本思路: [1] 关于容器与封装.封装,是一种非常重要的系统设计思想:无论是面向过程的函数,还 ...

随机推荐

  1. 虚拟机14安装kail Linux

    需要准备虚拟机和kail Linux镜像 1. 2.选择镜像安装,并且添加你的kail Linux镜像文件. 3. 4.在这里需要修改虚拟机名称,也可以不修改就用默认,然后在修改kail Linux的 ...

  2. 第30月第13天 supportedInterfaceOrientationsForWindow旋转

    1. 对于做视频横屏播放的情况下:做旋转有3种方法. 第一种:就是网上说的用旋转矩阵方法CGAffineTransformMakeRotation来做,直接旋转某个view,之后setFrame,至于 ...

  3. react-native不是内部或 外部命令,也不是可运行的程序或批处理文件

    1.执行node命令时提示:node不是内部或外部命令,也不是可运行的程序或批处理文件. 原因环境变量没有指向node安装目录 path:C:\Program Files\nodejs\ 2.reac ...

  4. SpringMVC-简单总结

    要学习一项技术,首先要知道, 它是什么, 为什么要用它 , 它由哪些东西组成, 每个东西是干什么的, 它们怎么综合在一起的 参考博客: 平凡希: https://www.cnblogs.com/xia ...

  5. 2018-2019 网络对抗技术 20165231 Exp4 恶意代码分析

    实验目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systrac ...

  6. Timeline高级扩展

    转载于http://forum.china.unity3d.com/thread-32200-1-1.html通过demo讲解了timeline更加复杂的使用方式 Timeline是创建过场动画和影片 ...

  7. JUC--volatiley&CAS

    public class VolatileTest { public static void main(String[] args) { ThreadDemo td = new ThreadDemo( ...

  8. springboot动态多数据源

    参考文章:https://www.cnblogs.com/hehehaha/p/6147096.html 前言 目标是springboot工程支持多个MySQL数据源,在代码层面上,同一个SQL(Ma ...

  9. docker容器和镜像的导入和导出

    镜像的保存 [root@wxtest1607 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat8 3.0 90457edaf6 ...

  10. linux 磁盘io监控

    我们在线上linux服务器排查问题时,一般会通过top.free.netstat.df -h等命令排查cpu.内存.网络和磁盘等问题.有的时候我们需要更进一步了解磁盘io的使用情况,那么本文就是重点讲 ...