发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
线索二叉树(采用中序遍历)
#include "pch.h" #include <iostream> using namespace std; //定义线索二叉树 typedef struct Tree { int data, LTag, RTag;//定义数据域与标记域 Tree *lchild, *rchild; }Tree,*Trees; Trees pre = NULL;//设置前驱线索 //初始化二叉树 void InitBitTree(Trees*boot) { *boot = (Trees)malloc(sizeof(Tree)); (*boot)->lchild = (*boot)->rchild = NULL; (*boot)->LTag = (*boot)->RTag = 0; } //创建二叉树 void CreateBtTree(Trees &boot) { int num; cout << "请输入数据:"; cin >> num; if (num==0) { boot = NULL; } else { boot =(Trees) malloc(sizeof(Tree)); boot->data = num; boot->lchild = boot->rchild = 0; boot->LTag = boot->RTag = 0; CreateBtTree(boot->lchild); CreateBtTree(boot->rchild); } } //添加线索 void InssertLineTree(Trees &boot) { if (boot!=NULL) { InssertLineTree(boot->lchild);//线索化左子树 if (boot->lchild==NULL) { boot->LTag = 1; boot->lchild = pre;//设置前驱线索 } if (pre!=NULL&&pre->rchild==NULL) { pre->rchild = boot ; pre->RTag = 1; } //当前访问节点为下一个节点的前驱/ pre = boot; //线索化右子树 InssertLineTree(boot->rchild); } } //创建头结点 Trees InOrderThread(Trees &rt) { Trees throot; if (!(throot = (Trees)malloc(sizeof(Tree)))) { cout << "头结点创建失败!" << endl; exit(0); } throot->LTag = 0;//左标记为0 指向左子树 throot->RTag = 1;//右标记为1 指向遍历的前驱 throot->rchild = throot;//右子树指向头结点本身 if (!throot) { //二叉树如果为空,左指针指向头结点本身 throot->lchild = throot; } else { throot->lchild = rt; pre = throot; //插入线索 InssertLineTree(rt); pre->rchild = throot; pre->RTag = 1; throot->rchild = pre; } return throot; } //中序遍历查找前驱 void InPre(Trees boot) { Trees q = NULL; if (boot->LTag==1) { pre = boot->lchild; } else { for (q=boot->lchild; q->RTag==0;q=q->rchild ) { pre=q; } } if (pre) { cout << "用中序遍历找到的前驱为:" << pre->data << endl; } else { cout << "用中序遍历无前驱:" << endl; } } //中序遍历后序节点 void InNext(Trees boot) { Trees q = NULL; if (boot->RTag == 1) { pre = boot->rchild; } else { for (q = boot->rchild; q->LTag == 0; q = q->lchild) { pre = q; } } if (pre) { cout << "用中序遍历找到的后继为:" << pre->data << endl; } else { cout << "用中序遍历无后继:" << endl; } } //中序遍历查找线索二叉树第一个节点 Trees InFirst(Trees boot) { Trees p = boot; if (!p) { return 0; } while (p->LTag==0) { p = p->lchild; } return p; //中序遍历左 根 右 二叉树左左端节 //二叉树的最左端的节点 } //中序遍历线索二叉树 void TinOrder(Trees &throot) { Trees p; p = throot->lchild; while (p!=throot) { while (p->LTag==0)//有左子树 { p = p->lchild; } cout<<p->data<<endl; while (p->RTag==1&&p->rchild!=throot) { p = p->rchild; cout << p->data << endl; } p = p->rchild; } cout << endl; } int main() { Trees boot = NULL; cout << "创建线索二叉树,如果输入0结束:" << endl; CreateBtTree(boot); Trees throot;//头结点 throot=InOrderThread(boot); //进行遍历 TinOrder(throot); InPre(boot); InNext(boot); Trees bt=InFirst(boot); cout << "中序遍历线索二叉树的第一个节点为:" << bt->data << endl; return 0; }
结果为:
来自: 流楚丶格念 > 《待分类》
0条评论
发表
请遵守用户 评论公约
数据结构实验,哈夫曼编码/译码系统
a) 设计递归算法,实现二叉树的基本运算:删除一棵二叉树,求一棵二叉树的高度,求一棵二叉树中叶子结点数,复制一棵二叉树,交换一棵二叉树的左右子树。template <class T> class BTNod...
C++二叉树的建立与遍历
实验三____二叉树的基本操作实现及其应用
二叉树的基本操作实现及其应用。1.熟悉二叉树结点的结构和对二叉树的基本操作。设计程序实现二叉树结点的类型定义和对二叉树的基本操作。voidBinTraverse(BitTree&BT)//按先序序列建立二叉树。voi...
二叉链表表示的二叉树和一些基本操作
//一旦visit()失败,则操作失败{ if (T){ BiTree lchild = T->lchild, rchild = T->rchild; if(Visit(T)) if (PreOrderTraverse...
二叉树遍历
二叉树遍历1. 二叉树遍历1.1 遍历算法:1.先序遍历的递归算法定义: 若二叉树非空,则依次执行如下操作: (1) 访问根结点;
数据结构_二叉树的遍历_课程设计_实验报告
a、调用生成二叉树的函数,从键盘输入二叉树的各个结点b、分别调用先序遍历、中序遍历、后序遍历二叉树的函数,输出所有结点显示的菜单为:请选择遍历算法1.按先序输入二叉树序列以#表示空节点2.先序遍...
深入理解二叉树的非递归遍历
深入理解二叉树的非递归遍历。在三种遍历中,前序和中序遍历的非递归算法都很容易实现,非递归后序遍历实现起来相对来说要难一点。根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点...
二叉树后序遍历(非递归)
二叉树后序遍历(非递归)二叉树的递归遍历算法就不用说了;43else cout''已满''endl; 44} 45BiTree pop(Stack *st) 46{ ...
【经典面试题二】二叉树的递归与非递归遍历(前序、中序、后序)
【经典面试题二】二叉树的递归与非递归遍历(前序、中序、后序)【写在前面】1 void inOrder2(BinTree *root) //非递归中序遍历 2 { 3 stack<BinTree*> s; 4 BinTree *p=root; 5 while(p!=NULL||...
科技领域优质作者
微信扫码,在手机上查看选中内容