数据结构实验二

一、实验目的
1、线性表的链表实现:遍历、查找、插入、删除、翻转
2、栈的链式存储结构实现:入栈、出栈
3、队列的链式存储结构的实现:入队、出队
4、线性表、栈和队列的应用实现
二、使用仪器、器材
微机一台
操作系统:WinXP
编程软件:C++

三、实验内容及原理
利用二叉树的二叉链式存储结构设计并实现各种操作算法。
1、二叉树的基本操作算法实现
(1) 利用二叉树字符串“A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”创建二叉树的二叉链式存储结构;
(2) 输出该二叉树;
(3) 输出‘H’节点的左、右孩子结点值;
(4) 输出该二叉树的结点个数、叶子结点个数、二叉树的度和高度。
2、二叉树的各种遍历算法实现
实现上述二叉树的先序、中序和后序遍历的递归和非递归算法。
3、线索二叉树的遍历
中序线索化上述二叉树并找出根结点的前驱和后继。
4、构造哈夫曼树和哈夫曼编码的算法实现
统计下面一段英文的不同字符个数和每个字符的出现频率,利用统计数据构造构造哈夫曼树和哈夫曼编码。
The Chinese official said he viewed the Trump Presidency not as an aberration but as the product of a failing political system. This jibes with other accounts. The Chinese leadership believes that the United States, and Western democracies in general, haven’t risen to the challenge of a globalized economy, which necessitates big changes in production patterns, as well as major upgrades in education and public infrastructure. In Trump and Trumpism, the Chinese see an inevitable backlash to this failure.

1、二叉树的基本操作运算:

#include "pch.h"
#include <iostream>
#define OK 1
#define MAXSIZE 200
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct BiTNode
{
   
	ElemType data;
	struct BiTNode *lchild, *rchild;  
}BiTNode, *BiTree;
//创建二叉树
Status CreateBiTree(BiTree &T)
{
   
	BiTree S[MAXSIZE];
	BiTNode *p = NULL;
	int top = 0, k = 0;  //top表示一维数组的下标;
	T = NULL;
	char ch;
	cin >> ch;
	while (ch != '#')
	{
   
		switch (ch)
		{
   
		case '(':S[++top] = p; k = 1;
			break;
		case ')':top--;
			break;
		case ',':k = 2;
			break;
		default:
			p = new BiTNode;
			p->data = ch;
			p->lchild = p->rchild = NULL;
			if (T == NULL) T = p;
			else
			{
   
				switch (k)
				{
   
				case 1:S[top]->lchild = p;
					break;
				case 2:S[top]->rchild = p;
					break;
				}
			}
			break;
		}
		cin >> ch;
	}
	return OK;
}
//线序遍历输出二叉树
Status ShowBiTree(BiTree T)
{
   
	cout << T->data;
	if (T->lchild != NULL || T->rchild != NULL)
	{
   
		cout << "(";
		if (T->lchild != NULL)
		{
   
			ShowBiTree(T->lchild);
		}
		if (T->rchild != NULL)
		{
   
			cout << ",";
			ShowBiTree(T->rchild);
			cout << ")";
		}
	}
	return OK;
}
//‘H’节点的左、右孩子结点值
Status SBiTree(BiTree T)
{
   
	if (T == NULL) return 0;
	else if (T->data == 'H' && T->lchild != NULL && T->rchild != NULL)
	{
   
		cout << "H" << "的左孩子为" << T->lchild->data << endl;
		cout << "H" << "的右孩子为" << T->rchild->data << endl;
	}
	else {
   
		SBiTree(T->lchild);
		SBiTree(T->rchild);
	}
	return OK;
}
//统计二叉树中结点的个数
int NodeCount(BiTree T)
{
   
	if (T == NULL) return 0;
	else return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}
//统计二叉树中叶子结点个数
int LeafCount(BiTree T)
{
   
	if (T == NULL) return 0;
	else if (T->lchild == NULL && T->rchild == NULL) 
	{
   
		return 1;
	}
	else
	{
   
		return LeafCount(T->lchild) + LeafCount(T->rchild);
	}
}
//统计二叉树的度
int DepthBiTree(BiTree T)
{
   
	if (T == NULL) 
		return 0;
	else if ((T->lchild != NULL && T->rchild == NULL) || (T->lchild == NULL && T->rchild != NULL))
		return 1;
	else if (T->lchild != NULL && T->rchild != NULL)
		return 2;
}
//统计二叉树的深度
int Depth(BiTree T)
{
   
	if (T == NULL)
		return 0;
	else
	{
   
		int m 
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值