数据结构——线索化二叉树和哈夫曼树[通俗易懂]

数据结构——线索化二叉树和哈夫曼树[通俗易懂]线索化二叉树和哈夫曼树基础知识介绍与代码分析一、基础知识介绍二、代码分析:线索二叉树(采用中序遍历)#include “pch.h”#include <iostream>using namespace std;//定义线索二叉树typedef struct Tree{ int data, LTag, RTag; //定义数据域与标记域 Tre…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

线索化二叉树和哈夫曼树基础知识介绍与代码分析

一、基础知识介绍

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、代码分析:

线索二叉树(采用中序遍历)

#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;
}

结果为:

居中

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170981.html原文链接:https://javaforall.net

(0)
上一篇 2022年8月18日 上午9:00
下一篇 2022年8月18日 上午9:16


相关推荐

  • 敏捷软件开发之原则篇

    敏捷软件开发之原则篇1 我们最优化先要做的是通过尽早的 持续的交付有减脂的软件来使客户满意 2 即使到了开发的后期 也欢迎改变需求 敏捷过程利用变化来为客户创造竞争优势 3 经常性地交付可以工作的软件 交付的间隔可以从几周到几个月 交付的时间间隔越短越好 4 在整个项目开发期间 业务人员和开发人员必须天天都在一起工作 5 围绕被激励起来的个人构建项目 给他们踢空所需的环境和支持 并且信任他们能够完成工

    2026年3月20日
    2
  • datagrid控件详解_datagridview控件不显示数据

    datagrid控件详解_datagridview控件不显示数据解决方法:在代码中必须有下列突显部分.privatevoidInitializeComponent()  {      this.bnRefresh.Click+=newSystem.EventHandler(this.bnRefresh_Click);   this.bnAdd.Click+=newSystem.EventHandler(this.bnAdd_Clic

    2022年10月13日
    5
  • 移位指令实现乘法

    移位指令实现乘法includeirvine32.inc;.dataidword0;sumqword0;str1byte”请输入16进制的(32位整数)乘数和被乘数”,0str2byte”乘积为:”,0;jdword0;.codemainprocL1:movedx,offsetstr1;callwritestring;movj,0;moveax,0;eax为…

    2022年6月3日
    50
  • Idea激活码最新教程2023.3版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2023.3版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2023 3 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2023 3 成功激活

    2025年5月27日
    8
  • 如何使用Docker Compose安装Drupal

    如何使用Docker Compose安装DrupalTheauthorselectedUnitedNationsFoundationtoreceiveadonationaspartoftheWriteforDOnationsprogram.作者选择联合国基金会作为WriteforDOnations计划的一部分接受捐赠。TheoriginalWordPressversionofthistut…

    2022年7月20日
    27
  • java中的io流知识总结_java中的io流开发用的多吗

    java中的io流知识总结_java中的io流开发用的多吗通过前面的简单学习,我们已经能够大致了解了关于文件的操作,但是能够明显感受到在执行其他的操作的时候,还是会有一些不方便的地方存在,因此今天我们会学习另外四个IO流来帮助我们对文件进行操作,这四个流分别是缓冲流、转换流、序列化、打印流。好了,废话不多说,我直接开始今天的学习吧!……

    2022年10月20日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号