头文件:

#include <iostream>
using namespace std; template<class Type>
class Bintree; //结点类
template<class Type>
class BintreeNode
{
friend class Bintree<Type>;
public:
BintreeNode() :data(Type()), leftchild(NULL), rightchild(NULL)
{}
BintreeNode(Type d, BintreeNode<Type> *l = NULL, BintreeNode<Type> *r = NULL) : data(d), leftchild(l), rightchild(r)
{}
private:
BintreeNode<Type> *leftchild;
BintreeNode<Type> *rightchild;
Type data;
}; //二叉树类
template<class Type>
class Bintree
{
public:
Bintree() :Ref(Type()), root(NULL)
{}
Bintree(Type re, BintreeNode<Type> *r = NULL) : Ref(re), root(r)
{}
~Bintree()
{
Destory();
}
public:
//提供接口
void CreatBintree()
{
Creat(root);
} void PreOrder()
{
PreOrder(root);
} void InOrder()
{
InOrder(root);
} void PostOrder()
{
PostOrder(root);
} int Height()
{
return Height(root);
} int Size()
{
return Size(root);
} BintreeNode<Type> *Search(Type key)
{
return Search(root, key);
} BintreeNode<Type> *PreOrder_Find(Type key)
{
return PreOrder_Find(root, key);
} BintreeNode<Type> *InOrder_Find(Type key)
{
return InOrder_Find(root, key);
} BintreeNode<Type> *PostOrder_Find(Type key)
{
return PostOrder_Find(root, key);
} BintreeNode<Type> *Parent(BintreeNode<Type> *q)
{
return Parent(root, q);
} BintreeNode<Type> *Leftchild(Type key)
{
return Leftchild(root, key);
} BintreeNode<Type> *Rightchild(Type key)
{
return Rightchild(root, key);
} Type Root()
{
return Root(root);
} void Destory()
{
return Destory(root);
} bool IsEmpty()
{
return IsEmpty(root);
} void quit_system(int &x)
{
x = 0;
}
protected:
//创建二叉树
void Creat(BintreeNode<Type> *&t)
{
Type input;
cin >> input;
if (input == Ref)
{
t = NULL;
}
else
{
t = new BintreeNode<Type>(input);
Creat(t->leftchild);
Creat(t->rightchild);
}
} // 前序遍历
void PreOrder(const BintreeNode<Type> *t)
{
if (t == NULL)
{
return;
}
else
{
cout << t->data << " ";
PreOrder(t->leftchild);
PreOrder(t->rightchild);
}
} // 中序遍历
void InOrder(const BintreeNode<Type> *t)
{
if (t == NULL)
{
return;
}
else
{
InOrder(t->leftchild);
cout << t->data << " ";
InOrder(t->rightchild);
}
} // 后序遍历
void PostOrder(const BintreeNode<Type> *t)
{
if (t == NULL)
{
return;
}
else
{
PostOrder(t->leftchild);
PostOrder(t->rightchild);
cout << t->data << " ";
}
} // 求高度
int Height(const BintreeNode<Type> *t)
{
if (t == NULL)
return 0;
return (Height(t->leftchild) > Height(t->rightchild)) ? (Height(t->leftchild) + 1) : (Height(t->rightchild) + 1);
} int Size(const BintreeNode<Type> *t)
{
if (t == NULL)
return 0;
return(Size(t->leftchild) + Size(t->rightchild) + 1);
} // 查找一个节点返回其地址
BintreeNode<Type> *Search( BintreeNode<Type> *t,const Type key)
{
if (t == NULL)
{
return NULL;
}
if (t->data == key)
return t;
BintreeNode<Type> *p;
if ((p = Search(t->leftchild, key)) != NULL)
return p;
else
return Search(t->rightchild, key);
} // 前序查找
BintreeNode<Type> *PreOrder_Find(BintreeNode<Type> *t, const Type key)
{
if (t == NULL)
{
return NULL;
}
if (t->data == key)
return t;
BintreeNode<Type> *p;
if ((p = PreOrder_Find(t->leftchild, key)) != NULL)
return p;
else
return PreOrder_Find(t->rightchild, key);
} // 中序查找
BintreeNode<Type> *InOrder_Find(BintreeNode<Type> *t, const Type key)
{
if (t == NULL)
{
return NULL;
}
BintreeNode<Type> *p;
if ((p = InOrder_Find(t->leftchild, key)) != NULL)
return p;
else if (t->data == key)
return t;
else
return InOrder_Find(t->rightchild, key);
} // 后序查找
BintreeNode<Type> *PostOrder_Find(BintreeNode<Type> *t, const Type key)
{
if (t == NULL)
{
return NULL;
}
BintreeNode<Type> *p;
BintreeNode<Type> *q;
if ((p = PostOrder_Find(t->leftchild, key)) != NULL)
return p;
else if ((q = PostOrder_Find(t->rightchild, key)) != NULL)
return q;
else if (t->data = key)
return t;
} // 求父节点并返回其父节点地址
BintreeNode<Type> *Parent(BintreeNode<Type> *&t, BintreeNode<Type> *q)
{
if (t == NULL)
{
return t;
}
if (q == t->leftchild || q == t->rightchild || q == t)
{
return t;
}
BintreeNode<Type> *p;
if ((p = Parent(t->leftchild, q)) != NULL)
{
return p;
}
else
return Parent(t->rightchild, q);
} // 求左孩子
BintreeNode<Type> *Leftchild(BintreeNode<Type> *t, const Type key)
{
BintreeNode<Type> *p = Search(t, key);
if (p == NULL)
{
cout << "the node is not exist!" << endl;
return NULL;
}
if (p->leftchild == NULL)
{
cout << "this node not has leftchild" << endl;
return NULL;
}
else
return p->leftchild;
} // 求右孩子
BintreeNode<Type> *Rightchild(BintreeNode<Type> *t, const Type key)
{
BintreeNode<Type> *p = Search(t, key);
if (p == NULL)
{
cout << "the node is not exist!" << endl;
return NULL;
}
if (p->rightchild == NULL)
{
cout << "this node not has rightchild" << endl;
return NULL;
}
else
return p->rightchild;
} // 求根
Type Root(const BintreeNode<Type> *t)
{
return t->data;
} void Destory(const BintreeNode<Type> *t)
{
if (t != NULL)
{
Destory(t->leftchild);
Destory(t->rightchild);
delete t;
}
} // 看树是否为空
bool IsEmpty(const BintreeNode<Type> *t)
{
return t == NULL;
}
private:
BintreeNode<Type> *root;
Type Ref;
};

页面设计:

#include "Bintree.h"

int main()
{
Bintree<char> bt('#');
int select = 1;
char c;
while (select)
{
cout << "******************************************************************" << endl;
cout << "* [1] creat [2] PreOrder [3] InOrder *" << endl;
cout << "* [4] PostOrder [5] Height [6] Size *" << endl;
cout << "* [7] search [8] PreOrder_Find [9] InOrder_Find *" << endl;
cout << "* [10] PostOrder_Find [11] parent [12] leftchild *" << endl;
cout << "* [13] rightchild [14] root [15] destory *" << endl;
cout << "* [16] Isempty [17] quit_system *" << endl;
cout << "******************************************************************" << endl;
cout << "pleae choose:";
cin >> select;
switch (select)
{
case 1:
cout << "please enter:";
bt.CreatBintree();
break;
case 2:
bt.PreOrder();
cout << endl;
break;
case 3:
bt.InOrder();
cout << endl;
break;
case 4:
bt.PostOrder();
cout << endl;
break;
case 5:
cout << bt.Height() << endl;
break;
case 6:
cout << bt.Size() << endl;
break;
case 7:
cout << "please enter what u want to search:";
cin >> c;
cout << bt.Search(c) << endl;
break;
case 8:
cout << "please enter what u want to search:";
cin >> c;
cout << bt.PreOrder_Find(c) << endl;
break;
case 9:
cout << "please enter what u want to search:";
cin >> c;
cout << bt.InOrder_Find(c) << endl;
break;
case 10:
cout << "please enter what u want to search:";
cin >> c;
cout << bt.PostOrder_Find(c) << endl;
break;
case 11:
cout << "whose parent u wanna find:";
cin >> c;
cout << bt.Parent(bt.Search(c)) << endl;
break;
case 12:
cout << "whose leftchild u wanna find:";
cin >> c;
cout << bt.Leftchild(c) << endl;
break;
case 13:
cout << "whose rightchild u wanna find:";
cin >> c;
cout << bt.Rightchild(c) << endl;
break;
case 14:
cout << bt.Root() << endl;
break;
case 15:
bt.Destory();
break;
case 16:
if (bt.IsEmpty())
{
cout << "it is an empty bintree" << endl;
}
else
{
cout << "it is not an empty bintree" << endl;
}
break;
case 17:
bt.quit_system(select);
break;
default:
break; }
}
return 0;
}

求高度:

中序遍历:

中序遍历查找:

推断是否为空树:

求其父节点:

后序遍历:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

前序遍历:

退出系统:

求其右孩子:

求根:

查找:

求节点个数:

【数据结构】二叉树(c++)的更多相关文章

  1. 什么是泛型?,Set集合,TreeSet集合自然排序和比较器排序,数据结构-二叉树,数据结构-平衡二叉树

    ==知识点== 1.泛型 2.Set集合 3.TreeSet 4.数据结构-二叉树 5.数据结构-平衡二叉树 ==用到的单词== 1.element[ˈelɪmənt] 要素 元素(软) 2.key[ ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java

    前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...

  4. 数据结构——二叉树(Binary Trees)

    非线性数据结构 二叉搜索树(Binary Search Tree) 树的密度=结点数/高度 二叉树类 #pragma once class stnode { public: int nodeValue ...

  5. [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树

    紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...

  6. 数据结构二叉树的所有基本功能实现。(C++版)

    本人刚学数据结构,对树的基本功能网上找不到C++代码 便自己写了一份,贴出方便大家进行测试和学习. 大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢. 结点声明: BinTreeNode. ...

  7. python实战--数据结构二叉树

    此文将讲述如何用python实战解决二叉树实验 前面已经讲述了python语言的基本用法,现在让我们实战一下具体明确python的用法 点击我进入python速成笔记 先看一下最终效果图: 首先我们要 ...

  8. FBI树-数据结构(二叉树)

    问题 B: [2004_p4]FBI树-数据结构 时间限制: 1 Sec  内存限制: 125 MB提交: 57  解决: 46 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称 ...

  9. (2)Java数据结构--二叉树 -和排序算法实现

    === 注释:此人博客对很多个数据结构类都有讲解-并加以实例 Java API —— ArrayList类 & Vector类 & LinkList类Java API —— BigDe ...

  10. Python数据结构——二叉树

    数的特征和定义: 树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构,很象自然界中的树那样.树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构都 ...

随机推荐

  1. eval() 函数 解析json对象

    eval在js中用来运行以js源码组成的字符串. 可以用来改变全局或者局部变量,例如: var globalEval = eval; //定义全局eval函数别名 var a ='global', b ...

  2. Lambert (兰伯特)光照模型

    Lambert (兰伯特)光照模型 是光源照射到物体表面后,向四面八方反射,产生的漫反射效果.这是一种理想的漫反射光照模型.如下图:这个是顶点函数处理后的该光照模型,因此看起来像素不够平滑. 漫反射 ...

  3. 关于加号传递到后端会变为空格的c#例子

    参考博客:http://blog.csdn.net/nsdnresponsibility/article/details/50965262 以前在一次传递参数的情况中遇到,特此记录一下. 之前传递的参 ...

  4. android 之 Intent、broadcast

    Intent的功能有: 在mainActivity中为按钮1添加监听事件: listener1 = new OnClickListener() { @Override    public void o ...

  5. tensorflow在各种环境下搭建与对比

    tensorflow在各种环境下搭建与对比 由于有些训练是要长时间进行训练(几天),才能看出显著的结果,如果只是通过本地的计算机进行训练是不可能的.因此这周花了一些时间调研如何才能让神经网络长时间的进 ...

  6. c++ stack,queue,vector基本操作

    stack 的基本操作有:入栈,如例:s.push(x);出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素.访问栈顶,如例:s.top()判断栈空,如例:s.empty(), ...

  7. 九度oj 题目1111:单词替换

    题目描述: 输入一个字符串,以回车结束(字符串长度<=100).该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写.现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符 ...

  8. 九度oj 题目1102:最小面积子矩阵

    题目描述: 一个N*M的矩阵,找出这个矩阵中所有元素的和不小于K的面积最小的子矩阵(矩阵中元素个数为矩阵面积) 输入: 每个案例第一行三个正整数N,M<=100,表示矩阵大小,和一个整数K接下来 ...

  9. [BZOJ1579] [Usaco2009 Feb]Revamping Trails 道路升级(分层图最短路 + 堆优化dijk)

    传送门 dis[i][j]表示第i个点,更新了j次的最短路 此题不良心,卡spfa #include <queue> #include <cstdio> #include &l ...

  10. 实验三 kali下metasploit的漏洞攻击实践

    一.实验内容 1.使用kali进行靶机的漏洞扫描,利用metasploit选择其中的一个漏洞进行攻击,并获取权限. 2.分析攻击的原理以及获取了什么样的权限. 二.实验要求 1.熟悉kali原理和使用 ...