前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构——线索化二叉树和哈夫曼树[通俗易懂]

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

作者头像
全栈程序员站长
发布2022-09-23 19:56:04
2330
发布2022-09-23 19:56:04
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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

一、基础知识介绍

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

二、代码分析:

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

代码语言:javascript
复制
#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.cn/170981.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 线索化二叉树和哈夫曼树基础知识介绍与代码分析
  • 一、基础知识介绍
  • 二、代码分析:
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档