Tree
//Header.h
- #ifndef _HEAD_
- #define _HEAD_
- #include <queue>
- #include <iostream>
- using namespace std;
- typedef char TElemType;
- typedef int Status;
- #define OK 0
- #define ERROR -2
- #define OverFlow -1
- //普通二叉树
- typedef struct BiTNode
- {
- TElemType data;
- struct BiTNode *lchild, *rchild;
- } BiTNode, *BiTree;
- //线索二叉树
- typedef enum {Link, Thread} PointerTag;
- typedef struct BiThrNode
- {
- TElemType data;
- struct BiThrNode *lchild, *rchild;
- PointerTag LTag, RTag;
- } BiThrNode, *BiThrTree;
- //仅为普通二叉树
- void CreateBiTree(BiTree &T);
- void PreOrderTraverse(BiTree T);
- void LevelTraverse(BiTree T);
- //线索二叉树
- void CreateBiThrTree(BiThrTree &T);
- Status InOrderThread_Head(BiThrTree &head, BiThrTree &T);
- Status InOrderTraverse_Thr(BiThrTree T);
- #endif
//Functions.cpp
- #include "Header.h"
- //因为BiTree是一个结构体,所以这里必须用“引用&”,否则将会新建一个空的BiTree,导致在创建二叉树时,创建失败(我们指定的BiTree为空);
- //进而导致后面的遍历直接退出(因为传进去的BiTree不管是否为“引用&”都为空);
- //另外只要创建的树正确,那么在遍历的时候不论是否“引用&”都可以得到正确的遍历顺序
- //创建二叉树:过程理解为先序
- void CreateBiTree(BiTree &T)
- {
- TElemType ch;
- cin >> ch;
- if (ch == '#')
- T = NULL;
- else
- {
- T = (BiTree)malloc(sizeof(BiTNode));
- if (T == NULL)
- {
- cout << "Create BinaryTree failed!";
- exit(OverFlow);
- }
- T->data = ch;
- CreateBiTree(T->lchild);
- CreateBiTree(T->rchild);
- }
- }
- //先序遍历
- void PreOrderTraverse(BiTree T)
- {
- if (T == NULL)
- return;
- cout << T->data;
- PreOrderTraverse(T->lchild);
- PreOrderTraverse(T->rchild);
- }
- //深度优先遍历
- //广度优先遍历
- //层次遍历
- void LevelTraverse(BiTree T)
- {
- BiTree temp;
- queue<BiTree>q;
- q.push(T);
- do
- {
- temp = q.front();
- cout << temp->data;
- q.pop();
- if (temp->lchild != NULL)
- {
- q.push(temp->lchild);
- }
- if (temp->rchild != NULL)
- {
- q.push(temp->rchild);
- }
- } while (!q.empty());
- }
- //前面是二叉树,下面是线索二叉树
- BiThrTree pre;
- void CreateBiThrTree(BiThrTree &T)
- {
- TElemType ch;
- cin >> ch;
- if (ch == '#')
- T = NULL;
- else
- {
- T = (BiThrTree)malloc(sizeof(BiThrNode));
- if (T == NULL)
- {
- cout << "Create BinaryTree failed!";
- exit(OverFlow);
- }
- T->data = ch;
- CreateBiThrTree(T->lchild);
- CreateBiThrTree(T->rchild);
- }
- }
- void SetPointerTag(BiThrTree &p) //实际上只需要设置初始值就行
- {
- if (p)
- {
- p->LTag = Link;
- p->RTag = Link;
- SetPointerTag(p->lchild);
- SetPointerTag(p->rchild);
- }
- }
- void InThreading(BiThrTree &p) //p:present
- {
- if (p)
- {
- InThreading(p->lchild);
- //两个if都是线索结点
- if (p->lchild == NULL) //这里千万不要写错,要看清楚:这里是没有左孩子,而不是有左孩子
- {
- p->LTag = Thread;
- p->lchild = pre;
- }
- if (pre->rchild == NULL) //前驱没有右孩子
- {
- pre->RTag = Thread;
- pre->rchild = p;
- }
- pre = p;
- InThreading(p->rchild);
- }
- }
- //建立头结点,中序线索二叉树本来的其余结点
- Status InOrderThread_Head(BiThrTree &head, BiThrTree &T)
- {
- if (head == NULL)
- {
- return ERROR;
- }
- head->rchild = head;
- head->RTag = Link;
- if (T == NULL) //如果为NULL
- {
- head->lchild = head;
- head->LTag = Link;
- }
- else
- {
- pre = head;
- head->lchild = T; //第一步
- head->LTag = Link;
- SetPointerTag(T);
- InThreading(T); //找到最后一个结点
- pre->rchild = head; //第四步
- pre->RTag = Thread;
- head->rchild = pre; //第二步
- }
- return OK;
- }
- Status InOrderTraverse_Thr(BiThrTree T)
- {
- BiThrTree p;
- p = T->lchild;
- while (p != T)
- {
- while (p->LTag == Link)
- p = p->lchild;
- cout << p->data;
- while (p->RTag == Thread && p->rchild != T)
- {
- p = p->rchild;
- cout << p->data;
- }
- p = p->rchild;
- }
- return OK;
- }
//Main.cpp
- #include "Header.h"
- int main()
- {
- int choice;
- cout << "1:普通二叉树" << endl << "2:线索二叉树" << endl;
- cin >> choice;
- switch (choice)
- {
- case :
- BiTree binaryTree;
- CreateBiTree(binaryTree);
- PreOrderTraverse(binaryTree);
- cout << endl;
- LevelTraverse(binaryTree);
- cout << endl;
- break;
- case :
- //必须用一个新的函数,新建一个树,因为数据结构已被改变——>然后建立头节点(就像链表),
- //并随即线索化——>像链表一样遍历(相对于普通树的遍历,减少了递归的堆栈导致的返回次数)
- BiThrTree threadBinaryTree;
- CreateBiThrTree(threadBinaryTree);
- BiThrTree head = (BiThrTree)malloc(sizeof(BiThrNode));
- InOrderThread_Head(head, threadBinaryTree);
- InOrderTraverse_Thr(head);
- cout << endl;
- break;
- }
- return ;
- }
Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- DevExpress VCL 13.1.4支持Delphi /C++Builder XE5
DevExpress VCL 13.1.4支持Delphi /C++Builder XE5 重大变化 ExpressLibrary dxHalfOfPi常数声明已经从cxGeometry单元移到了cx ...
- 在Sharepoint 2013中,使用JS判断当前用户是否在某个组里面
使用Sharepoint客户端对象模型,判断当前用户是否在某个组里面. 在View 和 Edit List Item的时候使用,使用户编辑修改List Item的时候有权限的区分. 在Edit 页面加 ...
- 解决windows防火墙无法启动的问题
windows防火墙突然无法开启,找个各种方法,最后还是通过微软自动的修复工具修复的: 网址如下: https://support.microsoft.com/zh-cn/mats/windows_f ...
- android加固系列—6.仿爱加密等第三方加固平台之动态加载dex防止apk被反编译
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5402599.html ] 此方案的目的是隐藏源码防止直接性的反编译查看源码,原理是加密编译好的 ...
- 【代码笔记】iOS-使图片两边不拉伸,中间拉伸
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...
- redis如何执行redis命令
Redis 命令 Redis 命令用于在 redis 服务上执行操作.所以我们必须要启动Redis服务程序,也就是redis安装目录下的redis-server.exe,你可以双击执行,也可以打开cm ...
- 熟练掌握js中this的用法,解析this在不同应用场景的作用
由于其运行期绑定的特性,JavaScript 中的 this 含义要丰富得多,它可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式. JavaScript 中函数的调用有以下几种方式:作 ...
- IO流05--毕向东JAVA基础教程视频学习笔记
Day20 10 创建java文件列表11 Properties简述12 Properties存取13 Properties存取配置文件14 Properties练习15 PrintWriter16 ...
- INBOUND_CONNECT_TIMEOUT与SQLNET.INBOUND_CONNECT_TIMEOUT小结
关于sqlnet.ora的参数SQLNET.INBOUND_CONNECT_TIMEOUT,它表示等待用户认证超时的时间,单位是秒,缺省值是60秒,如果用户认证超时了,服务器日志alert.log显示 ...
- 0013 Java学习笔记-面向对象-static、静态变量、静态方法、静态块、单例类
static可以修饰哪些成员 成员变量---可以修饰 构造方法---不可以 方法---可以修饰 初始化块---可以修饰 内部类(包括接口.枚举)---可以修饰 总的来说:静态成员不能访问非静态成员 静 ...