图解前序遍历线索化二叉树,前序线索二叉树遍历,C\C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
| 二叉树输入过程:ABDH##I##EJ###CF##G## | 前序遍历:ABDHIEJCFG |
|
#include<iostream>
using namespace std;
enum TBT{child=0,thread};
typedef struct tbt
{
struct tbt* lchild;
enum TBT ltag;
char data;
enum TBT rtag;
struct tbt* rchild;
}TBTreeNode,*pTBTree;
int createThreadedBinaryTree(pTBTree& root);
void preorderThreadingBinaryTree(const pTBTree& root,TBTreeNode*&pre);
void preorderThreadedBinaryTreeTraversal(pTBTree root);
int main()
{
TBTreeNode* root = nullptr;
int ret = createThreadedBinaryTree(root);
if(0==ret)
{
TBTreeNode* pre = nullptr;
preorderThreadingBinaryTree(root,pre);
cout<<"preorder traversal Threaded Binary Tree:"<<endl;
preorderThreadedBinaryTreeTraversal(root);
cout<<endl;
}
system("pause");
}
|
int createThreadedBinaryTree(pTBTree& root)
{
char data;
if(cin>>data)
{
if('#'==data)
{
root = nullptr;
return -1;
}
root = new TBTreeNode();
root->data = data;
createThreadedBinaryTree(root->lchild);
createThreadedBinaryTree(root->rchild);
}
return 0;
}
void preorderThreadingBinaryTree(const pTBTree& root,TBTreeNode*&pre)
{
if(nullptr==root)
return ;
/* 参考前序遍历
cout<<root->data;
preorderTraversal(root->lchild);
preorderTraversal(root->rchild);
*/
// 访问顺序:根结点,左子树,右子树
//pre永远指向当前访问结点root的前一个访问过的结点,初始为null
if(nullptr==root->lchild)
{
root->lchild = pre;
root->ltag = thread;
}
if(nullptr!=pre&&nullptr==pre->rchild)
{ //要线索化一个结点的后继,按照前序遍历顺序,只能访问到root的时候才能线索化前一个结点pre的后继,前提pre要不是null,并且没有右子树
pre->rchild = root;
pre->rtag = thread;
}
pre = root;
//区别于中序线索化二叉树,因为当前结点先访问到,同时给线索化了
//所以下面访问左右子树就要判断一下是真的有左右子树而不是线索化的前驱后继
if(child==root->ltag)
{
preorderThreadingBinaryTree(root->lchild,pre);
}
if(child==root->rtag)
{
preorderThreadingBinaryTree(root->rchild,pre);
}
}
void preorderThreadedBinaryTreeTraversal(pTBTree root)
{
if(nullptr==root)
return ;
while(nullptr!=root)
{
while(nullptr!=root->lchild&&
child==root->ltag)
{
cout<<root->data<<" "; //访问根结点
root = root->lchild; //继续访问左子树
}
//左子树的最左边结点
cout<<root->data<<" ";
//通过后继遍历下一个要访问的结点
while(thread==root->rtag) //当前结点有后继
{
cout<<root->rchild->data<<" "; //输出后继
root = root->rchild; //更新根结点
}
//如果结点没有后继,那么就是有右子树
//针对这样的情况,先判断当前结点有没有左子树,有就更新根结点先遍历左子树
if(child==root->ltag)
root = root->lchild;
//没有就遍历右子树
else
root = root->rchild;
}
}
|
图解前序遍历线索化二叉树,前序线索二叉树遍历,C\C++描述的更多相关文章
- 图解中序遍历线索化二叉树,中序线索二叉树遍历,C\C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- C#数据结构-线索化二叉树
为什么线索化二叉树? 对于二叉树的遍历,我们知道每个节点的前驱与后继,但是这是建立在遍历的基础上,否则我们只知道后续的左右子树.现在我们充分利用二叉树左右子树的空节点,分别指向当前节点的前驱.后继,便 ...
- 线索化二叉树的构建与先序,中序遍历(C++版)
贴出学习C++数据结构线索化二叉树的过程, 方便和我一样的新手进行测试和学习 同时欢迎各位大神纠正. 不同与普通二叉树的地方会用背景色填充 //BinTreeNode_Thr.h enum Point ...
- 后序线索化二叉树(Java版)
前面介绍了前序线索化二叉树.中序线索化二叉树,本文将介绍后序线索化二叉树.之所以用单独的一篇文章来分析后序线索化二叉树,是因为后序线索化二叉树比前序.中序要复杂一些:另外在复习线索化二叉树的过程中,大 ...
- 数据结构与算法---线索化二叉树(Threaded BinaryTree)
先看一个问题 将数列 {1, 3, 6, 8, 10, 14 } 构建成一颗二叉树 问题分析: 当我们对上面的二叉树进行中序遍历时,数列为 {8, 3, 10, 1, 6, 14 } 但是 6, 8 ...
- YTU 3026: 中序线索化二叉树
原文链接:https://www.dreamwings.cn/ytu3026/2896.html 3026: 中序线索化二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 9 解决: ...
- JAVA递归实现线索化二叉树
JAVA递归实现线索化二叉树 基础理论 首先,二叉树递归遍历分为先序遍历.中序遍历和后序遍历. 先序遍历为:根节点+左子树+右子树 中序遍历为:左子树+根节点+右子树 后序遍历为:左子树+右子树+根节 ...
- LeetCode 590. N叉树的后序遍历(N-ary Tree Postorder Traversal)
590. N叉树的后序遍历 590. N-ary Tree Postorder Traversal 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. LeetCode590. N-ary Tre ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
随机推荐
- 回车、换行、空格的ASCII码值—(附ASCII码表)
回车.换行.空格的ASCII码值 回车,ASCII码13换行,ASCII码10空格,ASCII码32 Return = CR = 13 = '\x0d'NewLine = ...
- vs2015多行注释与取消多行注释
注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U
- MySQL Connector/J
5.1 Developer Guide 1. MysQL为由Java语言编程的客户端程序提供连接:MySQL Connector/J,这是一个实现Java Database Connectivity( ...
- springmvc: 普通list数据输出json
springmvc: 普通list数据输出json 加入json依赖 <dependency> <groupId>com.fasterxml.jackson.core</ ...
- spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略
spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...
- 【转】 C语言文件操作详解
转自:http://www.cnblogs.com/likebeta/archive/2012/06/16/2551780.html C语言中没有输入输出语句,所有的输入输出功能都用 ANSI C提供 ...
- eclipse---->error and exception 和常用配置
1.eclipse打开后报错:An internal error occurred during: "Initializing Java Tooling". java.lang.N ...
- English trip V1 - B 1. How much is it? 它是多少钱? Teacher:Corrine Key: is/are
In this lesson you will learn to ask about prices. 本节课你将学习询问关于价格 课上内容(Lesson) one piece of two p ...
- You Don't Know JS: this & Object Prototypes( 第2章 this)
this is a binding made for each function invocation, based entirely on its call-site (how the functi ...
- hdu-2419 Boring Game
http://acm.hdu.edu.cn/showproblem.php?pid=2419 给一个图,预分配点值.随后有三种操作,F u v查询与u联通部分大于等于v的最小的数,没有则返回0,U u ...