//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的更多相关文章

  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. 使用powershell部署WSP包

    代码如下,先卸载,然后删除,然后添加,然后安装. 卸载之后会重启IIS,所以等待几秒钟,直到卸载完成. 添加snapin $snapin = Get-PSSnapin | Where-Object { ...

  2. mac安装Aws cli失败

    OS X EI 10.11 报错信息如下: Found existing installation: six 1.4.1 DEPRECATION: Uninstalling a distutils i ...

  3. Android Animation学习(六) View Animation介绍

    Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...

  4. C语言堆栈入门——堆和栈的区别

    来看一个网上很流行的经典例子: main.cpp int a = 0; 全局初始化区 char *p1; 全局未初始化区 main() { int b; 栈 char s[] = "abc& ...

  5. 开发https应用

    开发https应用 SSL, 或者Secure Socket Layer,是一种允许web浏览器和web服务器通过一个安全的连接进行交流的技术.这意味着将被发送的数据在一端被翻译成密码,传送出去,然后 ...

  6. 【iOS】block的使用

    Block 是iOS在4.0之后新增的程式语法,一般用于回调方法,功能上和delegate类似.本文将讲解block的几种常见的使用方法,当然,block中最值得注意的还是它的内存管理,我将在< ...

  7. 【Andorid】短视频拍摄SDK——Vitamio Recorder 2.0 发布(支持ffmpeg命令行)

    简介 VCamera SDK Android 版(短视频拍摄SDK)是炫一下(北京)科技有限公司推出的软件开发工具包,为Android开发者提供简单.快捷的接口,帮助开发者实现Android平台上的短 ...

  8. MySQL分表自增ID解决方案

    当我们对MySQL进行分表操作后,将不能依赖MySQL的自动增量来产生唯一ID了,因为数据已经分散到多个表中. 应尽量避免使用自增IP来做为主键,为数据库分表操作带来极大的不便. 在postgreSQ ...

  9. 关于学习javascript的一些建议

    有被朋友或同事问到过,要如何学习前端技术,他们大多是已经掌握其他语言的程序员,或是计算机相关专业的在校生. 每次被问到,总要组织回忆一番,本着DRY原则,我还是根据我学习javascript(下文都简 ...

  10. yii2整合百度编辑器umeditor

    作者:白狼 出处:www.manks.top/article/yii2_umeditor 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责 ...