头文件:

#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. Java面试——HashCode的作用原理和实例解析

    ,也就是说,我们先通过 HashCode来判断两个类是否存放某个桶里,但这个桶里可能有很多类,那么我们就需要再通过 equals 在这个桶里找到我们要的类. 请看下面这个例子 : public cla ...

  2. SGU 149 树形DP Computer Network

    这道题搜了一晚上的题解,外加自己想了半个早上,终于想得很透彻了.于是打算好好写一写这题题解,而且这种做法比网上大多数题解要简单而且代码也比较简洁. 首先要把题读懂,把输入读懂,这实际上是一颗有向树.第 ...

  3. pytorch中的math operation: torch.bmm()

    torch.bmm(batch1, batch2, out=None) → Tensor Performs a batch matrix-matrix product of matrices stor ...

  4. J2EE 中间件 JVM 集群

    [转]J2EE 中间件 JVM 集群 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 jvm应用服务器weblogicjvm集群 1 前言 越来越多的关键任务和大型应用 ...

  5. Python logging 学习

    基本用法: import logging #初始化logger 对象 logger = logging.getLogger("main") #设置logger对象基础级别,后面的h ...

  6. iOS第三方-百度地图地图SDK(一)

    前言 最近项目忙完了准备把百度地图的方法都熟悉一遍,基于百度地图2.10.0,写demo的同时也写下博客来记录下 模拟器设置 我直接就复制我以前写过的一篇的图了,懒得截图... 获取百度地图KEY 让 ...

  7. hadoop2.7.0分布式系统搭建(ubuntu14.04)

    因为使用需要,在自己小本上建了四个虚拟机,打算搭建一个1+3的hadoop分布式系统. 环境:hadoop2.7.0+ubuntu14.04 (64位) 首先分别为搭建好的虚拟机的各主机重命名 方法: ...

  8. BZOJ 4589 Hard Nim ——FWT

    [题目分析] 位运算下的卷积问题. FWT直接做. 但还是不太民白,发明者要承担泽任的. [代码] #include <cstdio> #include <cstring> # ...

  9. bzoj3196 二逼平衡树 树套树(线段树套Treap)

    Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4697  Solved: 1798[Submit][Status][D ...

  10. 自己写了一个超级简便且傻瓜式的且功能强大的CSV组件(并且代码优雅,绝对没有一行多余的代码)

    github地址: https://github.com/yangfeixxx/chipsCSV.git 解决的问题:解决了传统的CSV工具类对于实体类无法自动化封装为带表头的CSV文件的痛点,在读取 ...