【数据结构】二叉树(c++)
头文件:
- #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++)的更多相关文章
- 什么是泛型?,Set集合,TreeSet集合自然排序和比较器排序,数据结构-二叉树,数据结构-平衡二叉树
==知识点== 1.泛型 2.Set集合 3.TreeSet 4.数据结构-二叉树 5.数据结构-平衡二叉树 ==用到的单词== 1.element[ˈelɪmənt] 要素 元素(软) 2.key[ ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- 数据结构——二叉树(Binary Trees)
非线性数据结构 二叉搜索树(Binary Search Tree) 树的密度=结点数/高度 二叉树类 #pragma once class stnode { public: int nodeValue ...
- [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树
紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...
- 数据结构二叉树的所有基本功能实现。(C++版)
本人刚学数据结构,对树的基本功能网上找不到C++代码 便自己写了一份,贴出方便大家进行测试和学习. 大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢. 结点声明: BinTreeNode. ...
- python实战--数据结构二叉树
此文将讲述如何用python实战解决二叉树实验 前面已经讲述了python语言的基本用法,现在让我们实战一下具体明确python的用法 点击我进入python速成笔记 先看一下最终效果图: 首先我们要 ...
- FBI树-数据结构(二叉树)
问题 B: [2004_p4]FBI树-数据结构 时间限制: 1 Sec 内存限制: 125 MB提交: 57 解决: 46 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称 ...
- (2)Java数据结构--二叉树 -和排序算法实现
=== 注释:此人博客对很多个数据结构类都有讲解-并加以实例 Java API —— ArrayList类 & Vector类 & LinkList类Java API —— BigDe ...
- Python数据结构——二叉树
数的特征和定义: 树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构,很象自然界中的树那样.树结构在客观世界中广泛存在,如人类社会的族谱和各种社会组织机构都 ...
随机推荐
- WPF IP地址输入控件的实现
一.前言 WPF没有内置IP地址输入控件,因此我们需要通过自己定义实现. 我们先看一下IP地址输入控件有什么特性: 输满三个数字焦点会往右移 键盘←→可以空光标移动 任意位置可复制整段IP地址,且支持 ...
- 【04】Math图解
[04]Math知识图
- luogu2219 [HAOI2007]修筑绿化带
和「理想的正方形」比较相似,需要先掌握那道题. 花坛外头每一边必须套上绿化带 #include <iostream> #include <cstdio> using names ...
- mysql replication常见错误整理
这篇文章旨在记录MySQL Replication的常见错误,包括自己工作中遇到的与网友在工作中遇到的,方面自己及别人以后进行查找.每个案例都是通过Last_IO_Errno/Last_IO_Erro ...
- C/C++的类型安全
类型安全很大程度上可以等价于内存安全,类型安全的代码不会试图访问自己没被授权的内存区域.“类型安全”常被用来形容编程语言,其根据在于该门编程语言是否提供保障类型安全的机制:有的时候也用“类型安全”形容 ...
- 使用pipework将Docker容器桥接到本地网络环境中
在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主机的网卡桥接起来,再给Docker容器配上IP就可以了.pipew ...
- spring实战 — spring数据库事务
欢迎加入程序员的世界,添物科技为您服务. 欢迎关注添物网的微信(微信号:tianwukeji),微博(weibo.com/91tianwu/),或下载添物APP,及时获取最新信息. 免费加入QQ群:5 ...
- 【Go】错误处理
· error类型是一个接口类型,也是一个Go语言的内建类型.在这个接口类型的声明中只包含了一个方法Error.这个方法不接受任何参数,但是会返回一个string类型的结果.它的作用是返回错误信息的字 ...
- 【bzoj2229】[Zjoi2011]最小割 分治+网络流最小割
题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...
- P3147 [USACO16OPEN]262144 (贪心)
题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-262,144),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: ...