大话数据结构——树

#include <string>
#include <iostream>
#include <vector>
using namespace std;

typedef enum {Link, Thread} PointerTag;            /* Link==0表示指向左右孩子指针, */
										           /* Thread==1表示指向前驱或后继的线索 */
typedef struct BitNode
{
	char data;                                     /* 结点数据 */                 
	BitNode *lchild, *rchild;                      /* 左右孩子指针 */
	PointerTag LTag;
	PointerTag RTag;                               /* 左右标志 */

}*PBitNode;


void CreateBiTree(PBitNode &T);
void InOrderThreading(PBitNode &T, PBitNode &H);
void InThreading(PBitNode &T);
void InOrderTraverse(PBitNode T);


/* 按前序输入构造二叉树T */
void CreateBiTree(PBitNode &T)                    //必须传递引用
{
	char ch;
	cin >> ch;
	if (ch == '#')
		T = NULL;
	else
	{
		T = (BitNode*)malloc(sizeof(BitNode));
		if (!T)
			exit(OVERFLOW);
		(T)->data = ch;                        /* 生成根结点(前序) */
		CreateBiTree(T->lchild);               /* 递归构造左子树 */
		if (T->lchild)
			T->LTag = Link;
		CreateBiTree(T->rchild);               /* 递归构造右子树 */
		if (T->rchild)
			T->RTag = Link;
	}
}

PBitNode pre = (PBitNode)malloc(sizeof(BitNode));
/* 中序遍历进行中序线索化 */
void InThreading(PBitNode &T)
{
	if (T)
	{
		InThreading(T->lchild);                /* 递归左子树线索化 */
		if (!T->lchild)                        /* 没有左孩子 */
		{
			T->LTag = Thread;                  /* 前驱线索 */
			T->lchild = pre;                   /* 左孩子指针指向前驱 */
		}
		if (!pre->rchild)
		{
			pre->RTag = Thread;
			pre->rchild = T;                   /* 前驱右孩子指针指向后继(当前结点p) */
		}
		pre = T;                               /* 保持pre指向p的前驱 */
		InThreading(T->rchild);                /* 递归右子树线索化 */
	}
		

}

/* 中序遍历二叉树T,并将其中序线索化 */
void InOrderThreading(PBitNode &T, PBitNode &H)
{
//此子函数初始化方式详见上图 H为图中头指针
	H = (PBitNode)malloc(sizeof(BitNode));
	H->LTag = Link;
	H->RTag = Thread;
	H->lchild = T;
	H->rchild = H;
	pre = H;
	InThreading(T);                     /* 中序遍历进行中序线索化 */
	pre->rchild = H;                    /* 最后一个结点线索化 */
	pre->RTag = Thread;
	H->rchild = pre;
}




/* 中序遍历二叉线索树T(头结点)的非递归算法 */
void InOrderTraverse(PBitNode H)
{
	PBitNode P = H->lchild;
	while (P != H)
	{
		while (P->LTag == Link)
			P = P->lchild;
		cout << P->data << endl;
		while (P->RTag == Thread & P->rchild != H)
		{
			P = P->rchild;
			cout << P->data << endl;
		}
		P = P->rchild;
	}
}

//递归前序遍历二叉树
void PreOrderTraverse(PBitNode T)
{
	if (T == NULL)
		return;
	cout << T->data << endl;
	PreOrderTraverse(T->lchild);
	PreOrderTraverse(T->rchild);
}




int main()
{
	PBitNode Tree, Head;
	
	CreateBiTree(Tree);									//构造二叉树
	cout << "Creation's done" << endl;
	PreOrderTraverse(Tree);								//中序遍历二叉树
	cout << "PreOrderTraverse's done" << endl;
	InOrderThreading(Tree, Head);						//线索化二叉树
	cout << "InOrderThreading's done" << endl;
	InOrderTraverse(Head);								//线索遍历
	cout << "InOrderTraverse's done" << endl;
	getchar();
	getchar();
	return 0;
}

 

来源:网络


智能推荐

大话数据结构文摘

第1章 数据结构绪论 程序设计=数据结构+算法 数据结构:是相互之间存在一种或多种特定关系的数据元素的集合 1.逻辑结构 :是指数据对象中数据元素之间的相互关系 a:集合结构  b:线性结构  c:树形结构  d:图形结构 2.物理结构:在计算机中的存储形式 a: 循序存储  b: 链式存储 第2章 算法 算法: 是解决特定问题求解步骤的描述,在计算机中表现...

【笔记】《大话数据结构》

写在前面 第1章 数据结构绪论 第2章 算法 第3章 线性表 第4章 栈与队列 第5章 串 第6章 树 第7章 图 第8章 查找 第9章 排序 写在前面 快速的过了一遍,对于初学者来说讲的很细,很有助于理解;对于有一定基础的人可能会觉得叙述太墨迹。。。 第1章 数据结构绪论 程序设计=数据结构+算法 数据:是描述客观事物的符号,是计算机中可以操作的对象,是能被计算机识别,并输入给计算机处理的符号集...

笔记——大话数据结构

目录   一、数据结构概述 二、线性表 三、栈 四、队列 五、串 六、树 七、图 八、查找 一、数据结构概述 数据结构根据视点不同,有逻辑结构和物理结构之分   逻辑结构用来描述数据元素之间的逻辑关系   物理结构是面向计算机的,它的目标是将数据元素及其他们之间的逻辑关系存储到计算机的内存中   逻辑结构     &n...

大话数据结构(一)

算法是解决特定问题求解步骤的描述,在计算机中表现为指令的有限序列,并且每条指令表示一个或多个操作。 算法是描述解决问题的方法。算法是解决特定问题求解步骤的描述。在计算机中表现为指令的有限序列,并且每条 指令表示一个或多个操作。 算法:输入、输出、又穷性、确定性和可行性。 输入输出:算法至少有一个或多个输出。 又穷性:算法在执行有限的步骤之后,自动结束而不会出现无限循环,并且每一个步骤在可接受的时间...

《大话数据结构》绪论

数据结构绪论 基本概念和术语 数据结构:是相互之间存在一种或多种特定关系的数据元素的集合。 数据:是描述客观事物的符号,是能输入到计算机中处理,计算机可识别可操作的符号集合。数据不仅仅包括整型、实型等数值类型,还包括字符及声音、图像、视频等非数值类型。数据其实就是符号。 数据元素:是组成数据的,有一定意义的基本单位。在计算机中会把它当做整体处理。 举个例子:鸡鸭为禽类的数据元素。 数据项:数据元素...

猜你喜欢

大话数据结构PDF

《大话数据结构》相关 下载地址 重难点目录 下载地址 云盘: touch here. 提取码: 93dc 重难点目录 第一章 绪论 逻辑结构,物理存储结构(顺序和链式) 数据结构类型 第二章 算法 时间复杂度和空间复杂度,计算方法 最坏情况、平均情况。 第三章 链表 动态链表:指针,常用 静态链表:数组,无指针语言(Basic)使用,较少用 循环链表:(单循环)next 双向链表:next,pri...

场效应管放大电路

金属-氧化物-半导体(MOS)场效应管 N沟道增强型MOSFET 栅源加电压,在电场作用下产生沟道。产生沟道的门限开启电压VT。 漏源加电压,产生电压梯度,导致沟道夹断。预夹断的临界条件 输出特性 特性方程 可变电阻区                         &...

【响应式】foundation栅格布局的“尝鲜”与“填坑”

  提到响应式,就不得不提两个响应式框架——bootstrap和foundation。在标题上我已经说明白啦,今天给大家介绍的是foundation框架。 何为“尝鲜”?就是带大伙初步一下foundation的灵活和强大 何为“踩坑”?就是我把我使用的时候踩过的坑给标个记号,这样大伙用的时候就可以“绕道而...

word2vec笔记

word2vec 词向量 one hot Distributed representation CBOW&Skip-Gram CBOW Skip-Gram sigmoid函数 Huffman树 基于Hierarchical Softmax的模型 基于Negative Sampling的模型 本文基于word2vec原理CBOW与Skip-Gram模型基础 CBOW与Skip-Gram的模型...

2021-03-14

官网:https://router.vuejs.org/zh/guide/essentials/navigation.html 一、安装路由 npm install vue-router --save-dev 在src目录下创建router目录 使用 vuecli3创建项目选择路由会自动创建route目录 在main.js引入router 二、配置路由 1、 hash: 使用 URL hash 值...

问答精选

SQL, update command not ending properly

It keeps saying : ORA-00933: SQL command not properly ended Pls help me or give me a link to a solution You can use a correlated subquery instead:...

How can I escape $.each loop with my data?

I'm doing an Json call to retrieve an a list of locations with information details for each location. longitude and latitude are included in this info. I am using Google's distance matrix api to get t...

How to display all the columns (and their type) in all tables of all schemas in a database?

Suppose you have a database which has an 'n' number of schemas with an 'n' number of tables each. Each of these contain an 'n' number of columns. How would I print all this data along with the data ty...

How to set the java.library.path in intelliJ Idea

Could anyone please help how do I solve this error: I am using IDEA IDE as a first time, and have been using Resin_4.0.37 as a server to test my work. As soon as I start my lcoal server in debug mode ...

How to calculate mouse coordinate based on resolution c#

i am trying to develop a remote desktop apps with c#. so i have couple of question regarding mouse coordinate calculation based on picture box suppose i have picture box and i want to capture mouse co...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答