#include<iostream>
#include<stdlib.h>
#define maxsize 100 using namespace std; typedef struct BTNode{
char val;
struct BTNode *lchild, *rchild;
int ltag, rtag;
}BTNode, *BiNode; BTNode* stack[maxsize];
int top = -1; /**
* 找到中序遍历的第一个起始顶点
*/
inline BTNode* First(BTNode *root){
while(root->ltag == 0)
root = root->lchild;
return root;
} /**
* 找到中序遍历的下一个顶点,树已被中序线索化
*/
inline BTNode* InOrderNext(BTNode *root){
if(root->rtag == 0)
return First(root->rchild);
else
return root->rchild;
} /**
* debug,用来打印线索化之后的树的信息
*/
inline void debug(){
for(int i = 0; i <= top; i++){
cout << "[debug]: BTNode->[";
cout << (void *) stack[i];
cout << "]" << endl;
cout << " [val] :" << stack[i]->val << endl;
cout << " [lchild]:";
cout << (void *) stack[i]->lchild << endl;
cout << " [rchild]:";
cout << (void *) stack[i]->rchild << endl;
cout << " [ltag] :" << stack[i]->ltag << endl
<< " [rtag] :" << stack[i]->rtag << endl;
}
} void CreateTree(BTNode *&root);
void PreOrderThreading(BTNode *root, BTNode *&pre);
void InOrderThreading(BTNode *root, BTNode *&pre);
void PreOrderThreadingTraversal(BTNode *root);
void InOrderThreadingTraversal(BTNode *root);
void PostOrderThreading(BTNode *root, BTNode *&pre);
void PostOrderThreadingTraversal(BTNode *root); void CreateTree(BTNode *&root){
char c;
cin >> c;
if(c != '#'){
root = (BTNode *) malloc(sizeof(struct BTNode));
if(!root){
cerr << "No More Memory!" << endl;
exit(-1);
}
root->val = c;
root->lchild = root->rchild = NULL;
root->ltag = root->rtag = 0;
CreateTree(root->lchild);
CreateTree(root->rchild);
}else{
root = NULL;
}
} void PreOrderThreading(BTNode *root, BTNode *&pre){
if(root){
if(!root->lchild){
root->lchild = pre;
root->ltag = 1;
}
if(pre && !pre->rchild){
pre->rchild = root;
pre->rtag = 1;
}
pre = root;
if(root->ltag == 0){
PreOrderThreading(root->lchild, pre);
}
if(root->rtag == 0){
PreOrderThreading(root->rchild, pre);
}
}
} void PreOrderThreadingTraversal(BTNode *root){
if(root){
cout << root->val << " ";
if(root->ltag == 0){
PreOrderThreadingTraversal(root->lchild);
}else{
PreOrderThreadingTraversal(root->rchild);
}
}
} void InOrderThreading(BTNode *root, BTNode *&pre){
if(root){
stack[++top] = root;
InOrderThreading(root->lchild, pre);
if(!root->lchild){
root->lchild = pre;
root->ltag = 1;
}
if(pre && !pre->rchild){
pre->rchild = root;
pre->rtag = 1;
}
pre = root;
InOrderThreading(root->rchild, pre);
}
} void InOrderThreadingTraversal(BTNode *root){
for(BTNode *p = First(root); p; p = InOrderNext(p))
cout << p->val << " ";
} void PostOrderThreading(BTNode *root, BTNode *&pre){
if(root){
stack[++top] = root;
PostOrderThreading(root->lchild, pre);
PostOrderThreading(root->rchild, pre);
if(!root->lchild){
root->lchild = pre;
root->ltag = 1;
}
if(pre && !pre->rchild){
pre->rchild = root;
pre->rtag = 1;
}
pre = root;
}
} void PostOrderThreadingTraversal(BTNode *root){
//PostOrder需要在原有的节点的类型定义上增加一个
//parent节点,否则,遍历的时候可能会没有办法
//回到上一层节点当中
} int main(){
BTNode *root, *pre = NULL;
CreateTree(root);
//PostOrderThreading(root, pre);
//debug();
//PostOrderThreadingTraversal(root);
return 0;
}

线索二叉树C++实现的更多相关文章

  1. 数据结构《9》----Threaded Binary Tree 线索二叉树

    对于任意一棵节点数为 n 的二叉树,NULL 指针的数目为  n+1 , 线索树就是利用这些 "浪费" 了的指针的数据结构. Definition: "A binary ...

  2. 线索二叉树Threaded binary tree

    摘要   按照某种遍历方式对二叉树进行遍历,可以把二叉树中所有结点排序为一个线性序列.在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结点.这 ...

  3. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  4. 树和二叉树->线索二叉树

    文字描述 从二叉树的遍历可知,遍历二叉树的输出结果可看成一个线性队列,使得每个结点(除第一个和最后一个外)在这个线形队列中有且仅有一个前驱和一个后继.但是当采用二叉链表作为二叉树的存储结构时,只能得到 ...

  5. 图解中序遍历线索化二叉树,中序线索二叉树遍历,C\C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...

  7. 数据结构之线索二叉树——C语言实现

     线索二叉树操作 (1) 线索二叉树的表示:将每个节点中为空的做指针与右指针分别用于指针节点的前驱和后续,即可得到线索二叉树. (2) 分类:先序线索二叉树,中序线索二叉树,后续线索二叉树 (3) 增 ...

  8. 后序线索二叉树中查找结点*p的后继

    在后序线索二叉树中查找结点*p的后继: 1.若结点*p为根,则无后继:2.若结点*p为其双亲的右孩子,则其后继为其双亲:3.若结点*p为其双亲的左孩子,且双亲无右子女,则其后继为其双亲:4.若结点*p ...

  9. 线索二叉树的理解和实现(Java)

    线索二叉树的基本概念 我们按某种方式对二叉树进行遍历,将二叉树中所有节点排序为一个线性序列,在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结 ...

  10. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

随机推荐

  1. 【转】vue1.0与2.0的一些区别

    一.生命周期钩子的差别 vue1.0的生命周期如下: vue2.0的生命周期如下: 用一张表格来做对比: 二.代码片段 在vue1.0中可以在template编写时出现: <template&g ...

  2. MySQL查询基础

    MySQL查询 DQL(Data Query Language ) 1.排序查询 # 语法: select 字段 from 表名 order by 字段1 [降序/升序],字段2 [降序/升序],.. ...

  3. 解决 VS Code 中 golang.org 被墙导致的 Go 插件安装失败问题

    微软官方开发的 Go for Visual Studio Code 插件为 Go 语言 提供了丰富的支持.在 VS Code 中首次打开 Go 工作区后,VS Code 会自动检测当前开发环境为 Go ...

  4. AVR单片机教程——LCD1602

    本文隶属于AVR单片机教程系列.   显示屏 开发板套件里有两块屏幕,大的是LCD(液晶显示),小的是OLED(有机发光二极管).正与你所想的相反,短小精悍的比较贵,而本讲的主题--LCD1602-- ...

  5. Firebase REST API

    use firebase and firesharp to do a Library management system . look github.com/ziyasal/FireSharp

  6. 1114 记录一点点吧 RP Axure

  7. hge引擎使用技巧

    图片周围最好留出一像素,即上下左右都多出一像素.然后使用pngopt.exe处理一下.这样可以减少图片拉伸.旋转时边界模糊的情况 图片宽高最好是 2的N次方

  8. 团队项目——Alpha1版本

    团队项目-Alpha版本发布1 一.格式描述 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ ...

  9. Kdenlive-开始

    版权声明:原创文章,未经博主允许不得转载 这是 Kdenlive 系列文章的第一篇 说明 在 Linux 下的视频编辑的软件并不多,作为其中之一的 kdenlive 在网上的教程就更少了.于是自己琢磨 ...

  10. ios--->上下拉刷新控件MJRefresh

    上下拉刷新控件MJRefresh 一.类结构 MJRefreshComponent.h MJRefreshHeader.h MJRefreshFooter.h MJRefreshAutoFooter. ...