数据结构严蔚敏版代码整理

第五章 树和二叉树

树是n个节点的有限集,或为空树,或为非空树,对于非空树有:

(1)有且仅有一个称之为根节点

(2)除了根节点其他节点可以分为互不相交的有限集,其中每个有限集本身又是一棵树,并称之为根的子树。

#include<iostream>
#include<stack>
using namespace std;
typedef struct TNode{
	ElemType data;
	struct TNode* lchild, * rchild;
}TNode,*T;
Status InitTree(T& tree) {
	tree = new TNode;
	if (!tree)
		return ERROR;
	tree->lchild = NULL;
	tree->rchild = NULL;
	return OK;
}
void CreateTree(T &tree) {
	ElemType data;
	cin >> data;
	if (data == -1)
		tree = NULL;
	else {
		tree = new TNode;
		tree->data = data;
		CreateTree(tree->lchild);
		CreateTree(tree->rchild);
	}
}
void InOrderTraverse(T tree) {
	if (tree) {
		InOrderTraverse(tree->lchild);
		cout << tree->data;
		InOrderTraverse(tree->rchild);
	}
}
void InOrderTraverseByStack(T tree) {
	stack<T>* stx = new stack<T>;
	while (!stx->empty() || tree) {
		if (tree) {
			stx->push(tree);
			tree = tree->lchild;
		}
		else {
			tree = stx->top();
			stx->pop();
			cout << tree->data;
			tree = tree->rchild;
		}
	}
}
void Copy(T tree, T& newTree) {
	if (tree) {
		newTree = new TNode;
		newTree->data = tree->data;
		Copy(tree->lchild, newTree->lchild);
		Copy(tree->rchild, newTree->rchild);
	}
	else {
		newTree = NULL;
	}
}
int Depth(T tree) {
	if (tree) {
		int depthLc = Depth(tree->lchild);
		int depthRc = Depth(tree->rchild);
		return 1 + (depthLc > depthRc ? depthLc:depthRc);
	}
	return 0;
}
int NodeCount(T tree) {
	if (tree) {
		return 1 + NodeCount(tree->lchild) + NodeCount(tree->rchild);
	}
	return 0;
}
int main(void) {
	//输入测试数据:1 2 3 -1 4 -1 -1 5 -1 -1 6 -1 7 -1 -1
	T tree;
	T p;
	InitTree(tree);
	CreateTree(tree);
	InOrderTraverse(tree);
	cout << endl;
	InOrderTraverseByStack(tree);
	cout << endl;
	Copy(tree, p);
	InOrderTraverse(p);
	cout << endl;
	cout << Depth(tree) << endl;
	cout << NodeCount(tree) << endl;
	return 0;
}

线索二叉树是用来保存递归遍历动态过程中才存在有关前驱和后继相关信息的手段。

#include<iostream>
using namespace std;
typedef struct TNode {
	ElemType data;
	struct TNode* lchild, * rchild;
	int lTag, rTag;
}TNode,*T;
T pre = NULL;
Status InitTree(T &tree) {
	tree = new TNode;
	if (!tree)
		return ERROR;
	tree->lchild = NULL;
	tree->rchild = NULL;
	return OK;
}
void CreateTree(T& tree) {
	ElemType data;
	cin >> data;
	if (data == -1)
		tree = NULL;
	else {
		tree = new TNode;
		tree->data = data;
		CreateTree(tree->lchild);
		CreateTree(tree->rchild);
	}
}
void InOrderTraverse(T tree) {
	if (tree) {
		InOrderTraverse(tree->lchild);
		cout << tree->data;
		InOrderTraverse(tree->rchild);
	}
}
void InitThreading(T tree) {
	if (tree) {
		InitThreading(tree->lchild);
		if (tree->lchild) {
			tree->lTag = 0;
		}
		else {
			tree->lchild = pre;
			tree->lTag = 1;
		}
		if (pre) {
			if (pre->rchild) {
				pre->rTag = 0;
			}
			else {
				pre->rchild = tree;
				pre->rTag = 1;
			}
		}
		pre = tree;
		InitThreading(tree->rchild);
	}
}
void TraverseThreading(T tree) {
	while (tree&&(!(tree->rTag == 1) || tree->rchild)) {
		while (tree->lTag == 0 && tree->lchild)tree = tree->lchild;
		cout << tree->data;
		while (tree->rTag == 1 && tree->rchild) {
			tree = tree->rchild;
			cout << tree->data;
		}
		tree = tree->rchild;
	}
}
int main(void) {
	T tree;
	//输入测试数据:1 2 3 -1 4 -1 -1 5 -1 -1 6 -1 7 -1 -1
	CreateTree(tree);
	InOrderTraverse(tree);
	cout << endl;
	InitThreading(tree);
	TraverseThreading(tree);
	return 0;
}

哈夫曼树是带权路径最短树,可以用于压缩算法,文件的编码和译码。

#define CODESIZE 10
#define MAX 65535
#include<iostream>
#include<iomanip>
using namespace std;

typedef struct huffmanNode {
	char sign;
	int weight;
	struct huffmanNode* lchild, * rchild, * parents;
	char* huffmancode;
}HNode,*HT;
void CreateHuffmanTree(HT*& ht,int n,char *signs,int *weights) {
	ht = new HT[n];
	for (int i = 0; i < n; i++) {
		ht[i] = new HNode;
		ht[i]->lchild = ht[i]->rchild = NULL;
		ht[i]->parents = NULL;
		ht[i]->weight = weights[i];
		ht[i]->sign = signs[i];
		ht[i]->huffmancode = NULL;
	}
	for (int k = 0; k < n - 1; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n-i-1; j++) {
				if (ht[j]->weight > ht[j + 1]->weight) {
					HT temp = ht[j];
					ht[j] = ht[j+1];
					ht[j+1] = temp;
				}
			}
		}
		//for (int i = 0; i < n; i++) {
		//	cout << ht[i]->weight << " ";
		//}
		//cout << endl;
		HT root = new HNode;
		root->parents = NULL;
		root->lchild = new HNode;
		root->rchild = new HNode;
		root->lchild->huffmancode = NULL;
		root->rchild->huffmancode = NULL;
		root->huffmancode = NULL;
		root->lchild->weight = ht[0]->weight;
		root->rchild->weight = ht[1]->weight;
		root->lchild->sign = ht[0]->sign;
		root->rchild->sign = ht[1]->sign;
		root->lchild->lchild = ht[0]->lchild;
		root->lchild->rchild = ht[0]->rchild;
		root->rchild->lchild = ht[1]->lchild;
		root->rchild->rchild = ht[1]->rchild;
		root->lchild->parents = root;
		root->rchild->parents = root;
		if(root->lchild->lchild)
			root->lchild->lchild->parents = root->lchild;
		if(root->lchild->rchild)
			root->lchild->rchild->parents = root->lchild;
		if(root->rchild->lchild)
			root->rchild->lchild->parents = root->rchild;
		if(root->rchild->rchild)
			root->rchild->rchild->parents = root->rchild;
		root->weight = root->lchild->weight + root->rchild->weight;
		root->sign = '#';
		ht[0] = root;
		ht[1]->weight = MAX;
		//for (int i = 0; i < n; i++) {
		//	cout << ht[i]->weight << " ";
		//}
		//cout << endl;
	}
}
void InOrderTraverse(HT ht) {
	if (ht) {
		cout << ht->sign << " " <<setw(6)<< ht->weight<<" ";
		if (ht->huffmancode) {
			cout << "code:";
			for (int i = 0; ht->huffmancode[i]; i++) {
				cout << ht->huffmancode[i];
			}
			cout << endl;
		}
		else cout << endl;
		InOrderTraverse(ht->lchild);
		InOrderTraverse(ht->rchild);
	}
}
void HuffmanCodeDriver(HT ht) {
	if (ht) {
		ht->huffmancode = new char[CODESIZE];
		for (int i = 0; i < CODESIZE; i++)ht->huffmancode[i] = '\0';
		if (ht->parents) {
			if (ht->parents->lchild == ht) {
				for (int i = 0; i < CODESIZE; i++) {
					ht->huffmancode[i] = ht->parents->huffmancode[i];
					if (ht->huffmancode[i] == '\0') {
						ht->huffmancode[i] = '0';
						break;
					}
				}
			}
			else {
				for (int i = 0; i < CODESIZE; i++) {
					ht->huffmancode[i] = ht->parents->huffmancode[i];
					if (ht->huffmancode[i] == '\0') {
						ht->huffmancode[i] = '1';
						break;
					}
				}
			}
		}
		HuffmanCodeDriver(ht->lchild);
		HuffmanCodeDriver(ht->rchild);
	}
}
int main(void) {
	char ch[] = { 'a','b','c','d','e','f','g' };
	int weight[] = { 5,2,3,3,8,9,1 };
	HT *ht;
	CreateHuffmanTree(ht, 7, ch, weight);
	HuffmanCodeDriver(ht[0]);
	InOrderTraverse(ht[0]);
	return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1.1 数组和字符串 2 1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维数组应用 3 1.1.3 一维数组的高级应用 5 范例1-3 一维数组的高级应用 5 1.1.4 显示杨辉三角 7 范例1-4 显示杨辉三角 7 ∷相关函数:c函数 8 1.1.5 魔方阵 9 范例1-5 魔方阵 9 1.1.6 三维数组的表示 14 范例1-6 三维数组的表示 14 ∷相关函数:InitArray函数 1.1.7 多项式的数组表示 17 范例1-7 多项式数组的表示 17 1.1.8 查找矩阵的马鞍点 19 范例1-8 查找矩阵的马鞍点 19 ∷相关函数:Get_Saddle函数 1.1.9 对角矩阵建立 21 范例1-9 对角矩阵建立 21 ∷相关函数:Store函数 1.1.10 三对角矩阵的建立 22 范例1-10 三对角矩阵的建立 22 ∷相关函数:Store函数 1.1.11 三角矩阵建立 24 范例1-11 三角矩阵建立 24 ∷相关函数:Store函数 1.1.12 对称矩阵的建立 25 范例1-12 对称矩阵的建立 25 ∷相关函数:store函数 1.1.13 字符串长度的计算 28 范例1-13 字符串长度的计算 28 ∷相关函数:strlen函数 1.1.14 字符串的复制 29 范例1-14 字符串的复制 29 ∷相关函数:strcpy函数 1.1.15 字符串的替换 31 范例1-15 字符串的替换 31 ∷相关函数:strrep函数 1.1.16 字符串的删除 33 范例1-16 字符串的删除 33 ∷相关函数:strdel函数 1.1.17 字符串的比较 35 范例1-17 字符串的比较 35 ∷相关函数:strcmp函数 1.1.18 字符串的抽取 36 范例1-18 字符串的抽取 36 ∷相关函数:substr函数 1.1.19 字符串的分割 38 范例1-19 字符串的分割 38 ∷相关函数:partition函数 1.1.20 字符串的插入 40 范例1-20 字符串的插入 40 ∷相关函数:insert函数 1.1.21 字符串的匹配 42 范例1-21 字符串的匹配 42 ∷相关函数:nfind函数 1.1.22 字符串的合并 43 范例1-22 字符串的合并 43 ∷相关函数:catstr函数 1.1.23 文本编辑 45 范例1-23 文本编辑 45 ∷相关函数:StrAssign函数 1.2 栈和队列 54 1.2.1 用数组仿真堆栈 54 范例1-24 用数组仿真堆栈 54 ∷相关函数:push函数 pop函数 1.2.2 用链表仿真堆栈 57 范例1-25 用链表仿真堆栈 57 ∷相关函数:push函数 pop函数 1.2.3 顺序栈公用 59 范例1-26 顺序栈公用 59 ∷相关函数:push函数 pop函数 1.2.4 进制转换问题 61 范例1-27 进制转换问题 61 ∷相关函数:MultiBaseOutput函数 1.2.5 顺序队列操作 64 范例1-28 顺序队列操作 64 ∷相关函数:push函数 pop函数 1.2.6 循环队列 66 范例1-29 循环队列 66 ∷相关函数:EnQueue函数 DeQueue函数 1.2.7 链队列的入队、出队 69 范例1-30 链队列入队、出队 69 ∷相关函数:push函数 pop函数 1.2.8 舞伴问题 71 范例1-31 舞伴问题 71 ∷相关函数:EnQueue函数 DeQueue函数 DancePartner函数 1.3 链表 75 1.3.1 头插法建立单链表 75 范例1-32 头插法建立单链表 75 ∷相关函数:createlist函数 1.3.2 限制链表长度建立单链表 77 范例1-33 限制链表长度建立长单链表 77 ∷相关函数:createlist函数 1.3.3 尾插法建立单链表 79 范例1-34 尾插法建立单链表 79 ∷相关函数:createlist函数 1.3.4 按序号查找单链表 80 范例1-35 按序号查找单链表 80 ∷相关函数:getnode函数 1.3.5 按值查找单链表 82 范例1-36 按值查找单链表 82 ∷相关函数:locatenode函数 1.3.6 链表的插入 84 范例1-37 链表的插入 84 ∷相关函数:insertnode函数 1.3.7 链表的删除 86 范例1-38 链表的删除 86 ∷相关函数:deletelist函数 1.3.8 归并两个单链表 88 范例1-39 归并两个单链表 88 ∷相关函数:concatenate函数 1.3.9 动态堆栈 90 范例1-40

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alasnot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值