//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. iOS开发-canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"

    转载自:http://www.jianshu.com/p/e38a609f786e

  2. iOS开发之动画中的时间

    概述 在动画中,我们会指定动画的持续时间.例如 scaleAnimation.duration = self.config.appearDuration 那么这个时间是怎么定义的呢?是指的绝对时间吗? ...

  3. Android开发实战(十八):Android Studio 优秀插件:GsonFormat

    Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat Android Studio 优秀插件(二): Parcelable Code Gen ...

  4. Swfit 字符与字符串

    Swfit 字符与字符串 OC 定义字符 char charValue = 'a'; Swift 定义字符 var charValue:Character = "a" Unicod ...

  5. Zend Studio 12 安装及破解

    安装: 1.下载最新版本Zend Studio:http://downloads.zend.com/studio-eclipse/12.0.0/ZendStudio-12.0.0-win32.win3 ...

  6. 将dll程序集添加到缓存里

    1.点击开始---所有程序---...如下图 并以管理员身份运行. 2.输入命令行 gacutil.exe /i D:\Word\CRS_BPM_Sln\SourceCode\BPM\Referenc ...

  7. Mongodb Manual阅读笔记:CH4 管理

    4 管理 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔 ...

  8. Java Security:Java加密框架(JCA)简要说明

    加密服务总是关联到一个特定的算法或类型,它既提供了密码操作(如Digital Signature或MessageDigest),生成或供应所需的加密材料(Key或Parameters)加密操作,也会以 ...

  9. UNABLE TO PURGE A RECORD(二)

    上一篇文章说明了bug出现的原因和原理分析,要修复bug似乎已经水到渠成了,但远没有这么简单,只因为“并发”.要修复问题,首先要做的第一件事情是稳定的复现问题.由于数据库系统是一个并发系统,并且这个b ...

  10. 十五天精通WCF——第九天 高级玩法之自定义Behavior

    终于我又看完了二期爱情保卫战,太酸爽了,推荐链接:http://www.iqiyi.com/a_19rrgublqh.html?vfm=2008_aldbd,不多说,谁看谁入迷,下面言归正传, 看看这 ...