//Header.h

  1. #ifndef _HEAD_
  2. #define _HEAD_
  3.  
  4. #include <queue>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. typedef char TElemType;
  9. typedef int Status;
  10. #define OK 0
  11. #define ERROR -2
  12. #define OverFlow -1
  13.  
  14. //普通二叉树
  15. typedef struct BiTNode
  16. {
  17. TElemType data;
  18. struct BiTNode *lchild, *rchild;
  19. } BiTNode, *BiTree;
  20.  
  21. //线索二叉树
  22. typedef enum {Link, Thread} PointerTag;
  23.  
  24. typedef struct BiThrNode
  25. {
  26. TElemType data;
  27. struct BiThrNode *lchild, *rchild;
  28. PointerTag LTag, RTag;
  29. } BiThrNode, *BiThrTree;
  30.  
  31. //仅为普通二叉树
  32. void CreateBiTree(BiTree &T);
  33. void PreOrderTraverse(BiTree T);
  34. void LevelTraverse(BiTree T);
  35.  
  36. //线索二叉树
  37. void CreateBiThrTree(BiThrTree &T);
  38. Status InOrderThread_Head(BiThrTree &head, BiThrTree &T);
  39. Status InOrderTraverse_Thr(BiThrTree T);
  40.  
  41. #endif

//Functions.cpp

  1. #include "Header.h"
  2.  
  3. //因为BiTree是一个结构体,所以这里必须用“引用&”,否则将会新建一个空的BiTree,导致在创建二叉树时,创建失败(我们指定的BiTree为空);
  4. //进而导致后面的遍历直接退出(因为传进去的BiTree不管是否为“引用&”都为空);
  5. //另外只要创建的树正确,那么在遍历的时候不论是否“引用&”都可以得到正确的遍历顺序
  6.  
  7. //创建二叉树:过程理解为先序
  8. void CreateBiTree(BiTree &T)
  9. {
  10. TElemType ch;
  11. cin >> ch;
  12. if (ch == '#')
  13. T = NULL;
  14. else
  15. {
  16. T = (BiTree)malloc(sizeof(BiTNode));
  17. if (T == NULL)
  18. {
  19. cout << "Create BinaryTree failed!";
  20. exit(OverFlow);
  21. }
  22. T->data = ch;
  23. CreateBiTree(T->lchild);
  24. CreateBiTree(T->rchild);
  25. }
  26. }
  27.  
  28. //先序遍历
  29. void PreOrderTraverse(BiTree T)
  30. {
  31. if (T == NULL)
  32. return;
  33. cout << T->data;
  34. PreOrderTraverse(T->lchild);
  35. PreOrderTraverse(T->rchild);
  36. }
  37.  
  38. //深度优先遍历
  39.  
  40. //广度优先遍历
  41.  
  42. //层次遍历
  43. void LevelTraverse(BiTree T)
  44. {
  45. BiTree temp;
  46. queue<BiTree>q;
  47. q.push(T);
  48. do
  49. {
  50. temp = q.front();
  51. cout << temp->data;
  52. q.pop();
  53. if (temp->lchild != NULL)
  54. {
  55. q.push(temp->lchild);
  56. }
  57. if (temp->rchild != NULL)
  58. {
  59. q.push(temp->rchild);
  60. }
  61. } while (!q.empty());
  62. }
  63.  
  64. //前面是二叉树,下面是线索二叉树
  65. BiThrTree pre;
  66.  
  67. void CreateBiThrTree(BiThrTree &T)
  68. {
  69. TElemType ch;
  70. cin >> ch;
  71. if (ch == '#')
  72. T = NULL;
  73. else
  74. {
  75. T = (BiThrTree)malloc(sizeof(BiThrNode));
  76. if (T == NULL)
  77. {
  78. cout << "Create BinaryTree failed!";
  79. exit(OverFlow);
  80. }
  81. T->data = ch;
  82. CreateBiThrTree(T->lchild);
  83. CreateBiThrTree(T->rchild);
  84. }
  85. }
  86.  
  87. void SetPointerTag(BiThrTree &p) //实际上只需要设置初始值就行
  88. {
  89. if (p)
  90. {
  91. p->LTag = Link;
  92. p->RTag = Link;
  93. SetPointerTag(p->lchild);
  94. SetPointerTag(p->rchild);
  95. }
  96. }
  97. void InThreading(BiThrTree &p) //p:present
  98. {
  99. if (p)
  100. {
  101. InThreading(p->lchild);
  102. //两个if都是线索结点
  103. if (p->lchild == NULL) //这里千万不要写错,要看清楚:这里是没有左孩子,而不是有左孩子
  104. {
  105. p->LTag = Thread;
  106. p->lchild = pre;
  107. }
  108. if (pre->rchild == NULL) //前驱没有右孩子
  109. {
  110. pre->RTag = Thread;
  111. pre->rchild = p;
  112. }
  113. pre = p;
  114. InThreading(p->rchild);
  115. }
  116. }
  117.  
  118. //建立头结点,中序线索二叉树本来的其余结点
  119. Status InOrderThread_Head(BiThrTree &head, BiThrTree &T)
  120. {
  121. if (head == NULL)
  122. {
  123. return ERROR;
  124. }
  125.  
  126. head->rchild = head;
  127. head->RTag = Link;
  128.  
  129. if (T == NULL) //如果为NULL
  130. {
  131. head->lchild = head;
  132. head->LTag = Link;
  133. }
  134. else
  135. {
  136. pre = head;
  137. head->lchild = T; //第一步
  138. head->LTag = Link;
  139. SetPointerTag(T);
  140. InThreading(T); //找到最后一个结点
  141. pre->rchild = head; //第四步
  142. pre->RTag = Thread;
  143. head->rchild = pre; //第二步
  144. }
  145. return OK;
  146. }
  147.  
  148. Status InOrderTraverse_Thr(BiThrTree T)
  149. {
  150. BiThrTree p;
  151. p = T->lchild;
  152. while (p != T)
  153. {
  154. while (p->LTag == Link)
  155. p = p->lchild;
  156. cout << p->data;
  157. while (p->RTag == Thread && p->rchild != T)
  158. {
  159. p = p->rchild;
  160. cout << p->data;
  161. }
  162. p = p->rchild;
  163. }
  164. return OK;
  165. }

//Main.cpp

  1. #include "Header.h"
  2.  
  3. int main()
  4. {
  5. int choice;
  6. cout << "1:普通二叉树" << endl << "2:线索二叉树" << endl;
  7. cin >> choice;
  8. switch (choice)
  9. {
  10. case :
  11. BiTree binaryTree;
  12. CreateBiTree(binaryTree);
  13. PreOrderTraverse(binaryTree);
  14. cout << endl;
  15. LevelTraverse(binaryTree);
  16. cout << endl;
  17. break;
  18. case :
  19. //必须用一个新的函数,新建一个树,因为数据结构已被改变——>然后建立头节点(就像链表),
  20. //并随即线索化——>像链表一样遍历(相对于普通树的遍历,减少了递归的堆栈导致的返回次数)
  21. BiThrTree threadBinaryTree;
  22. CreateBiThrTree(threadBinaryTree);
  23. BiThrTree head = (BiThrTree)malloc(sizeof(BiThrNode));
  24. InOrderThread_Head(head, threadBinaryTree);
  25. InOrderTraverse_Thr(head);
  26. cout << endl;
  27. break;
  28. }
  29. return ;
  30. }

Tree的更多相关文章

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

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

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  10. Tree树节点选中及取消和指定节点的隐藏

    指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...

随机推荐

  1. DevExpress VCL 13.1.4支持Delphi /C++Builder XE5

    DevExpress VCL 13.1.4支持Delphi /C++Builder XE5 重大变化 ExpressLibrary dxHalfOfPi常数声明已经从cxGeometry单元移到了cx ...

  2. 在Sharepoint 2013中,使用JS判断当前用户是否在某个组里面

    使用Sharepoint客户端对象模型,判断当前用户是否在某个组里面. 在View 和 Edit List Item的时候使用,使用户编辑修改List Item的时候有权限的区分. 在Edit 页面加 ...

  3. 解决windows防火墙无法启动的问题

    windows防火墙突然无法开启,找个各种方法,最后还是通过微软自动的修复工具修复的: 网址如下: https://support.microsoft.com/zh-cn/mats/windows_f ...

  4. android加固系列—6.仿爱加密等第三方加固平台之动态加载dex防止apk被反编译

    [版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5402599.html ] 此方案的目的是隐藏源码防止直接性的反编译查看源码,原理是加密编译好的 ...

  5. 【代码笔记】iOS-使图片两边不拉伸,中间拉伸

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  6. redis如何执行redis命令

    Redis 命令 Redis 命令用于在 redis 服务上执行操作.所以我们必须要启动Redis服务程序,也就是redis安装目录下的redis-server.exe,你可以双击执行,也可以打开cm ...

  7. 熟练掌握js中this的用法,解析this在不同应用场景的作用

    由于其运行期绑定的特性,JavaScript 中的 this 含义要丰富得多,它可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式. JavaScript 中函数的调用有以下几种方式:作 ...

  8. IO流05--毕向东JAVA基础教程视频学习笔记

    Day20 10 创建java文件列表11 Properties简述12 Properties存取13 Properties存取配置文件14 Properties练习15 PrintWriter16 ...

  9. INBOUND_CONNECT_TIMEOUT与SQLNET.INBOUND_CONNECT_TIMEOUT小结

    关于sqlnet.ora的参数SQLNET.INBOUND_CONNECT_TIMEOUT,它表示等待用户认证超时的时间,单位是秒,缺省值是60秒,如果用户认证超时了,服务器日志alert.log显示 ...

  10. 0013 Java学习笔记-面向对象-static、静态变量、静态方法、静态块、单例类

    static可以修饰哪些成员 成员变量---可以修饰 构造方法---不可以 方法---可以修饰 初始化块---可以修饰 内部类(包括接口.枚举)---可以修饰 总的来说:静态成员不能访问非静态成员 静 ...