头文件:

#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. Python 对Mysql的操作

    Mysql链接不同的数据库 如果python的模板是按照mysql来写的,后面数据库更换为了Oracle,难道需要重现再来写,当然不是,python提供了API接口,只要编写是面对api,后面的链接会 ...

  2. pat 1029 1029. 旧键盘(20)

    旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在2行中分别给出应该输入的文字.以及实际 ...

  3. Luogu【P3609】蹄子剪刀布(DP+滚动数组)

    题目链接 (突然高兴 又一次瞬间想出转移方程并一遍A掉!!233333(虽然从二叉苹果树那题开始我就发现我的方程好像跟别人不大一样 (所以这样就可以名正言顺的水题解了 设f[i][j][k]表示考虑F ...

  4. 解开Future的神秘面纱之任务执行

    此文承接之前的博文 解开Future的神秘面纱之取消任务 补充一些任务执行的一些细节,并从全局介绍程序的运行情况. 任务提交到执行的流程 前文我们已经了解到一些Future的实现细节,这里我们来梳理一 ...

  5. leetcode 350

    找两个数组的交叉部分,可以用map进行标记 首先对第一个数组进行记录,第二个数组在第一个数组记录基础上进行判断,如果在第一个数组出现过,就记录 class Solution { public: vec ...

  6. K大数查询(bzoj 3110)

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置 ...

  7. 欧拉函数(codevs 4939)

    题目描述 Description 输入一个数n,输出小于n且与n互素的整数个数 输入描述 Input Description 包含多组数据,n=0时结束 测试数据组数不会很多,不必先打表后输出 输出描 ...

  8. 小机房的树(codevs 2370)

    题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...

  9. Mysql字符集与校对规则

    字符集是一套字符和编码的集合,校对规则是用于比较字符集的一套规则. 所以字符集有两部分组成字符集合和对应的编码集合.比如说,现在有这几个字符:A B a b, 假设它们对应的编码分别是00, 01, ...

  10. 清清月儿.net学习技术资料网站

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] http://blog.csdn.net/21aspnet/