图解前序遍历线索化二叉树,前序线索二叉树遍历,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 ...
随机推荐
- 子数组最小值的总和 Sum of Subarray Minimums
2018-09-27 23:33:49 问题描述: 问题求解: 方法一.DP(MLE) 动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划, ...
- 通过cookies跳过验证码登陆页面,直接访问网站的其它URL
我每次手动访问去NN网的一家酒店,就不需要登陆,一旦我用脚本打开就会让我登陆,而登陆页面又有验证码,不想识别验证码,所以就想:“通过cookies跳过验证码登陆页面,直接访问网站的其它URL” 转 ...
- ffmpeg 无损改变纵横比aspect
最后发觉, potplayer 里 Ctrl+Enter 或者 Enter 可以扩展到整个屏幕/保持比例,根本不需要额外去转换 如果整个视频都要改的话,把 -ss -t 参数去掉 ffmpeg -ss ...
- English trip M1 - AC6 How to make salad? Teacher:Patrick
In this lesson you will learn to give instructions. 在本课中,您将学习如何提供说明. 课上内容(Lesson) How to make a sal ...
- p1473 Zero Sum
搜索,最后判断一下是否结果为0就行. #include <iostream> #include <cstdio> #include <cmath> #include ...
- android ------ Emulator: error: x86 emulation currently requires hardware acceleration
我创建 Android 模拟器,运行项目时出现了一个这样的错误: 如下: emulator ERROR:x86 emulation currently requires hardware accele ...
- 小程序获取openid 小程序授权
小程序获取openid 小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系. wx.login(Object object) 调用接口获取登录凭证(cod ...
- css之transform属性
定义元素的旋转(rotate),缩放(scale),移动(translate),倾斜(skew) rotate rotate(angle) 定义 2D 旋转,在参数中规定角度. rotate3d(x, ...
- 5月16 JSON的一些知识点及AJAX的应用
什么是JSON? JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,(文本数据交换格式).JSON简单说就是javascript中的对象和数组, ...
- Matlab-2:二分法工具箱
function g=dichotomy(f,tol) %this routine uses bisection to find a zero of user-supplied %continuous ...