图解前序遍历线索化二叉树,前序线索二叉树遍历,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 ...
随机推荐
- vue2总结
1.ref可跨页面取.vue文件的所有内容: <!--echart图自定义公用模板--> <echar-tem ref="echar"> </echa ...
- 大数据 - spark-sql 常用命令
--spark启动 spark-sql --退出 spark-sql> quit; --退出spark-sql or spark-sql> exit; 1.查看已有的database sh ...
- python paramiko自动登录网络设备抓取配置信息
ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostn ...
- Lab 3-4
Analyze the malware found in the file Lab03-04.exe using basic dynamic analysis tools. (This program ...
- hadoop挂载多硬盘,ZZ-- multiple disks per node
hadoop挂载多硬盘 ...multiple disks per node multiple disks per node Read more at: http://www.queryhome.c ...
- Vue音乐项目笔记(一)
看到一位小可爱的手记,这里记录一下自己需要注意的地方的链接 1.手写轮播图(上) https://blog.csdn.net/weixin_40814356/article/details/80298 ...
- Python特点
用一种方法,最好只用一种方法来做一件事 1.面向对象(解决一个问题,先考虑由“谁”来做,怎么做是“谁”的职责) 函数.模块.数字.字符串都是对象 在Python中一切皆对象 完全支持继承.重载.多重继 ...
- linux安装curl扩展
1.获得安装包,从网上直接下载或者其他途径,这里直接wget wget http://curl.haxx.se/download/curl-7.20.0.tar.gz 2.解压到当前目录(或者 htt ...
- 背包DP 存在异或条件的状态转移问题
题目链接 分析:有大佬说可以用线性基写,可惜我不会,这是用DP写的 题目明确说明可到达的位置只与能值有关,和下标无关,我们就可以排个序,这样每个数可以转移的区间就是它的所有后缀 我们可以用dp[i][ ...
- 使用LVM方式安装Ubuntu 16.04
--- By 小甘丶 注: 这里只讲解如何配置LVM,其他不再陈述! 这个方法,通用的!只要操作系统支持LVM即可!(个人推测,尚未证实) 配置好虚拟机后,开始安装,先进入Ubuntu使用界面,对磁盘 ...