头文件:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class Type>
  5. class Bintree;
  6.  
  7. //结点类
  8. template<class Type>
  9. class BintreeNode
  10. {
  11. friend class Bintree<Type>;
  12. public:
  13. BintreeNode() :data(Type()), leftchild(NULL), rightchild(NULL)
  14. {}
  15. BintreeNode(Type d, BintreeNode<Type> *l = NULL, BintreeNode<Type> *r = NULL) : data(d), leftchild(l), rightchild(r)
  16. {}
  17. private:
  18. BintreeNode<Type> *leftchild;
  19. BintreeNode<Type> *rightchild;
  20. Type data;
  21. };
  22.  
  23. //二叉树类
  24. template<class Type>
  25. class Bintree
  26. {
  27. public:
  28. Bintree() :Ref(Type()), root(NULL)
  29. {}
  30. Bintree(Type re, BintreeNode<Type> *r = NULL) : Ref(re), root(r)
  31. {}
  32. ~Bintree()
  33. {
  34. Destory();
  35. }
  36. public:
  37. //提供接口
  38. void CreatBintree()
  39. {
  40. Creat(root);
  41. }
  42.  
  43. void PreOrder()
  44. {
  45. PreOrder(root);
  46. }
  47.  
  48. void InOrder()
  49. {
  50. InOrder(root);
  51. }
  52.  
  53. void PostOrder()
  54. {
  55. PostOrder(root);
  56. }
  57.  
  58. int Height()
  59. {
  60. return Height(root);
  61. }
  62.  
  63. int Size()
  64. {
  65. return Size(root);
  66. }
  67.  
  68. BintreeNode<Type> *Search(Type key)
  69. {
  70. return Search(root, key);
  71. }
  72.  
  73. BintreeNode<Type> *PreOrder_Find(Type key)
  74. {
  75. return PreOrder_Find(root, key);
  76. }
  77.  
  78. BintreeNode<Type> *InOrder_Find(Type key)
  79. {
  80. return InOrder_Find(root, key);
  81. }
  82.  
  83. BintreeNode<Type> *PostOrder_Find(Type key)
  84. {
  85. return PostOrder_Find(root, key);
  86. }
  87.  
  88. BintreeNode<Type> *Parent(BintreeNode<Type> *q)
  89. {
  90. return Parent(root, q);
  91. }
  92.  
  93. BintreeNode<Type> *Leftchild(Type key)
  94. {
  95. return Leftchild(root, key);
  96. }
  97.  
  98. BintreeNode<Type> *Rightchild(Type key)
  99. {
  100. return Rightchild(root, key);
  101. }
  102.  
  103. Type Root()
  104. {
  105. return Root(root);
  106. }
  107.  
  108. void Destory()
  109. {
  110. return Destory(root);
  111. }
  112.  
  113. bool IsEmpty()
  114. {
  115. return IsEmpty(root);
  116. }
  117.  
  118. void quit_system(int &x)
  119. {
  120. x = 0;
  121. }
  122. protected:
  123. //创建二叉树
  124. void Creat(BintreeNode<Type> *&t)
  125. {
  126. Type input;
  127. cin >> input;
  128. if (input == Ref)
  129. {
  130. t = NULL;
  131. }
  132. else
  133. {
  134. t = new BintreeNode<Type>(input);
  135. Creat(t->leftchild);
  136. Creat(t->rightchild);
  137. }
  138. }
  139.  
  140. // 前序遍历
  141. void PreOrder(const BintreeNode<Type> *t)
  142. {
  143. if (t == NULL)
  144. {
  145. return;
  146. }
  147. else
  148. {
  149. cout << t->data << " ";
  150. PreOrder(t->leftchild);
  151. PreOrder(t->rightchild);
  152. }
  153. }
  154.  
  155. // 中序遍历
  156. void InOrder(const BintreeNode<Type> *t)
  157. {
  158. if (t == NULL)
  159. {
  160. return;
  161. }
  162. else
  163. {
  164. InOrder(t->leftchild);
  165. cout << t->data << " ";
  166. InOrder(t->rightchild);
  167. }
  168. }
  169.  
  170. // 后序遍历
  171. void PostOrder(const BintreeNode<Type> *t)
  172. {
  173. if (t == NULL)
  174. {
  175. return;
  176. }
  177. else
  178. {
  179. PostOrder(t->leftchild);
  180. PostOrder(t->rightchild);
  181. cout << t->data << " ";
  182. }
  183. }
  184.  
  185. // 求高度
  186. int Height(const BintreeNode<Type> *t)
  187. {
  188. if (t == NULL)
  189. return 0;
  190. return (Height(t->leftchild) > Height(t->rightchild)) ?
  191.  
  192. (Height(t->leftchild) + 1) : (Height(t->rightchild) + 1);
  193. }
  194.  
  195. int Size(const BintreeNode<Type> *t)
  196. {
  197. if (t == NULL)
  198. return 0;
  199. return(Size(t->leftchild) + Size(t->rightchild) + 1);
  200. }
  201.  
  202. // 查找一个节点返回其地址
  203. BintreeNode<Type> *Search( BintreeNode<Type> *t,const Type key)
  204. {
  205. if (t == NULL)
  206. {
  207. return NULL;
  208. }
  209. if (t->data == key)
  210. return t;
  211. BintreeNode<Type> *p;
  212. if ((p = Search(t->leftchild, key)) != NULL)
  213. return p;
  214. else
  215. return Search(t->rightchild, key);
  216. }
  217.  
  218. // 前序查找
  219. BintreeNode<Type> *PreOrder_Find(BintreeNode<Type> *t, const Type key)
  220. {
  221. if (t == NULL)
  222. {
  223. return NULL;
  224. }
  225. if (t->data == key)
  226. return t;
  227. BintreeNode<Type> *p;
  228. if ((p = PreOrder_Find(t->leftchild, key)) != NULL)
  229. return p;
  230. else
  231. return PreOrder_Find(t->rightchild, key);
  232. }
  233.  
  234. // 中序查找
  235. BintreeNode<Type> *InOrder_Find(BintreeNode<Type> *t, const Type key)
  236. {
  237. if (t == NULL)
  238. {
  239. return NULL;
  240. }
  241. BintreeNode<Type> *p;
  242. if ((p = InOrder_Find(t->leftchild, key)) != NULL)
  243. return p;
  244. else if (t->data == key)
  245. return t;
  246. else
  247. return InOrder_Find(t->rightchild, key);
  248. }
  249.  
  250. // 后序查找
  251. BintreeNode<Type> *PostOrder_Find(BintreeNode<Type> *t, const Type key)
  252. {
  253. if (t == NULL)
  254. {
  255. return NULL;
  256. }
  257. BintreeNode<Type> *p;
  258. BintreeNode<Type> *q;
  259. if ((p = PostOrder_Find(t->leftchild, key)) != NULL)
  260. return p;
  261. else if ((q = PostOrder_Find(t->rightchild, key)) != NULL)
  262. return q;
  263. else if (t->data = key)
  264. return t;
  265. }
  266.  
  267. // 求父节点并返回其父节点地址
  268. BintreeNode<Type> *Parent(BintreeNode<Type> *&t, BintreeNode<Type> *q)
  269. {
  270. if (t == NULL)
  271. {
  272. return t;
  273. }
  274. if (q == t->leftchild || q == t->rightchild || q == t)
  275. {
  276. return t;
  277. }
  278. BintreeNode<Type> *p;
  279. if ((p = Parent(t->leftchild, q)) != NULL)
  280. {
  281. return p;
  282. }
  283. else
  284. return Parent(t->rightchild, q);
  285. }
  286.  
  287. // 求左孩子
  288. BintreeNode<Type> *Leftchild(BintreeNode<Type> *t, const Type key)
  289. {
  290. BintreeNode<Type> *p = Search(t, key);
  291. if (p == NULL)
  292. {
  293. cout << "the node is not exist!" << endl;
  294. return NULL;
  295. }
  296. if (p->leftchild == NULL)
  297. {
  298. cout << "this node not has leftchild" << endl;
  299. return NULL;
  300. }
  301. else
  302. return p->leftchild;
  303. }
  304.  
  305. // 求右孩子
  306. BintreeNode<Type> *Rightchild(BintreeNode<Type> *t, const Type key)
  307. {
  308. BintreeNode<Type> *p = Search(t, key);
  309. if (p == NULL)
  310. {
  311. cout << "the node is not exist!" << endl;
  312. return NULL;
  313. }
  314. if (p->rightchild == NULL)
  315. {
  316. cout << "this node not has rightchild" << endl;
  317. return NULL;
  318. }
  319. else
  320. return p->rightchild;
  321. }
  322.  
  323. // 求根
  324. Type Root(const BintreeNode<Type> *t)
  325. {
  326. return t->data;
  327. }
  328.  
  329. void Destory(const BintreeNode<Type> *t)
  330. {
  331. if (t != NULL)
  332. {
  333. Destory(t->leftchild);
  334. Destory(t->rightchild);
  335. delete t;
  336. }
  337. }
  338.  
  339. // 看树是否为空
  340. bool IsEmpty(const BintreeNode<Type> *t)
  341. {
  342. return t == NULL;
  343. }
  344. private:
  345. BintreeNode<Type> *root;
  346. Type Ref;
  347. };

页面设计:

  1. #include "Bintree.h"
  2.  
  3. int main()
  4. {
  5. Bintree<char> bt('#');
  6. int select = 1;
  7. char c;
  8. while (select)
  9. {
  10. cout << "******************************************************************" << endl;
  11. cout << "* [1] creat [2] PreOrder [3] InOrder *" << endl;
  12. cout << "* [4] PostOrder [5] Height [6] Size *" << endl;
  13. cout << "* [7] search [8] PreOrder_Find [9] InOrder_Find *" << endl;
  14. cout << "* [10] PostOrder_Find [11] parent [12] leftchild *" << endl;
  15. cout << "* [13] rightchild [14] root [15] destory *" << endl;
  16. cout << "* [16] Isempty [17] quit_system *" << endl;
  17. cout << "******************************************************************" << endl;
  18. cout << "pleae choose:";
  19. cin >> select;
  20. switch (select)
  21. {
  22. case 1:
  23. cout << "please enter:";
  24. bt.CreatBintree();
  25. break;
  26. case 2:
  27. bt.PreOrder();
  28. cout << endl;
  29. break;
  30. case 3:
  31. bt.InOrder();
  32. cout << endl;
  33. break;
  34. case 4:
  35. bt.PostOrder();
  36. cout << endl;
  37. break;
  38. case 5:
  39. cout << bt.Height() << endl;
  40. break;
  41. case 6:
  42. cout << bt.Size() << endl;
  43. break;
  44. case 7:
  45. cout << "please enter what u want to search:";
  46. cin >> c;
  47. cout << bt.Search(c) << endl;
  48. break;
  49. case 8:
  50. cout << "please enter what u want to search:";
  51. cin >> c;
  52. cout << bt.PreOrder_Find(c) << endl;
  53. break;
  54. case 9:
  55. cout << "please enter what u want to search:";
  56. cin >> c;
  57. cout << bt.InOrder_Find(c) << endl;
  58. break;
  59. case 10:
  60. cout << "please enter what u want to search:";
  61. cin >> c;
  62. cout << bt.PostOrder_Find(c) << endl;
  63. break;
  64. case 11:
  65. cout << "whose parent u wanna find:";
  66. cin >> c;
  67. cout << bt.Parent(bt.Search(c)) << endl;
  68. break;
  69. case 12:
  70. cout << "whose leftchild u wanna find:";
  71. cin >> c;
  72. cout << bt.Leftchild(c) << endl;
  73. break;
  74. case 13:
  75. cout << "whose rightchild u wanna find:";
  76. cin >> c;
  77. cout << bt.Rightchild(c) << endl;
  78. break;
  79. case 14:
  80. cout << bt.Root() << endl;
  81. break;
  82. case 15:
  83. bt.Destory();
  84. break;
  85. case 16:
  86. if (bt.IsEmpty())
  87. {
  88. cout << "it is an empty bintree" << endl;
  89. }
  90. else
  91. {
  92. cout << "it is not an empty bintree" << endl;
  93. }
  94. break;
  95. case 17:
  96. bt.quit_system(select);
  97. break;
  98. default:
  99. break;
  100.  
  101. }
  102. }
  103. return 0;
  104. }

求高度:

中序遍历:

中序遍历查找:

推断是否为空树:

求其父节点:

后序遍历:

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. WPF IP地址输入控件的实现

    一.前言 WPF没有内置IP地址输入控件,因此我们需要通过自己定义实现. 我们先看一下IP地址输入控件有什么特性: 输满三个数字焦点会往右移 键盘←→可以空光标移动 任意位置可复制整段IP地址,且支持 ...

  2. 【04】Math图解

    [04]Math知识图    

  3. luogu2219 [HAOI2007]修筑绿化带

    和「理想的正方形」比较相似,需要先掌握那道题. 花坛外头每一边必须套上绿化带 #include <iostream> #include <cstdio> using names ...

  4. mysql replication常见错误整理

    这篇文章旨在记录MySQL Replication的常见错误,包括自己工作中遇到的与网友在工作中遇到的,方面自己及别人以后进行查找.每个案例都是通过Last_IO_Errno/Last_IO_Erro ...

  5. C/C++的类型安全

    类型安全很大程度上可以等价于内存安全,类型安全的代码不会试图访问自己没被授权的内存区域.“类型安全”常被用来形容编程语言,其根据在于该门编程语言是否提供保障类型安全的机制:有的时候也用“类型安全”形容 ...

  6. 使用pipework将Docker容器桥接到本地网络环境中

    在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主机的网卡桥接起来,再给Docker容器配上IP就可以了.pipew ...

  7. spring实战 — spring数据库事务

    欢迎加入程序员的世界,添物科技为您服务. 欢迎关注添物网的微信(微信号:tianwukeji),微博(weibo.com/91tianwu/),或下载添物APP,及时获取最新信息. 免费加入QQ群:5 ...

  8. 【Go】错误处理

    · error类型是一个接口类型,也是一个Go语言的内建类型.在这个接口类型的声明中只包含了一个方法Error.这个方法不接受任何参数,但是会返回一个string类型的结果.它的作用是返回错误信息的字 ...

  9. 【bzoj2229】[Zjoi2011]最小割 分治+网络流最小割

    题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...

  10. P3147 [USACO16OPEN]262144 (贪心)

    题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-262,144),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: ...