西工大NOJ数据结构理论——016.计算二叉树叶子结点数目(耿6.14)

33 篇文章 8 订阅

一.初级思路

拿到题之后的好习惯——先在纸上写思路

1.构建二叉树:

char* BinTreeBuilding(PBinTreeNode ptr_node,char *ptr_str){
	//创建二叉树 
	ptr_node->info=*ptr_str;
	if(*ptr_str!='#'){
		PBinTreeNode left=CreateBinTreeNode();
		left->dad=ptr_node;ptr_node->lchild=left;
		ptr_str++;
		ptr_str=BinTreeBuilding(left,ptr_str)+1;
		PBinTreeNode right=CreateBinTreeNode();
		right->dad=ptr_node;ptr_node->rchild=right;
		return BinTreeBuilding(right,ptr_str);
	} 
	else{
		return ptr_str;
	}
}

2.计算二叉树叶子结点数目:

void LeavesCounting(PBinTreeNode ptr_node,int* ptr_count){
	//统计二叉树中叶子结点的数目 
	if(ptr_node->lchild->info!='#'){
		LeavesCounting(ptr_node->lchild,ptr_count);
	}
	if(ptr_node->rchild->info!='#'){
		LeavesCounting(ptr_node->rchild,ptr_count);
	}
	if(ptr_node->lchild->info=='#'&&ptr_node->rchild->info=='#'){
		(*ptr_count)++;
	}
}

最后,源码分享:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef char DataType;
typedef struct BinTreeNode{
	DataType info;
	struct BinTreeNode* lchild;
	struct BinTreeNode* rchild;
	struct BinTreeNode* dad;
}BinTreeNode,*PBinTreeNode; 

void InitializeBinTreeNode(PBinTreeNode PNode){
	//初始化二叉树中的一个结点 
	PNode->info='0';
	PNode->lchild=NULL;
	PNode->rchild=NULL;
	PNode->dad=NULL;
}

PBinTreeNode CreateBinTreeNode(){
	//创建二叉树中的一个结点 
	PBinTreeNode PNode=(PBinTreeNode)malloc(sizeof(BinTreeNode));
	if(PNode==NULL){
		printf("out of space!");
	}else{
		InitializeBinTreeNode(PNode);
		return PNode;
	}
}

char* BinTreeBuilding(PBinTreeNode ptr_node,char *ptr_str){
	//创建二叉树 
	ptr_node->info=*ptr_str;
	if(*ptr_str!='#'){
		PBinTreeNode left=CreateBinTreeNode();
		left->dad=ptr_node;ptr_node->lchild=left;
		ptr_str++;
		ptr_str=BinTreeBuilding(left,ptr_str)+1;
		PBinTreeNode right=CreateBinTreeNode();
		right->dad=ptr_node;ptr_node->rchild=right;
		return BinTreeBuilding(right,ptr_str);
	} 
	else{
		return ptr_str;
	}
}

void LeavesCounting(PBinTreeNode ptr_node,int* ptr_count){
	//统计二叉树中叶子结点的数目 
	if(ptr_node->lchild->info!='#'){
		LeavesCounting(ptr_node->lchild,ptr_count);
	}
	if(ptr_node->rchild->info!='#'){
		LeavesCounting(ptr_node->rchild,ptr_count);
	}
	if(ptr_node->lchild->info=='#'&&ptr_node->rchild->info=='#'){
		(*ptr_count)++;
	}
}

int main(){
	char str[1000];
	scanf("%s",str);
	char *ptr_str=str;
	PBinTreeNode ptr_tree=CreateBinTreeNode();
	BinTreeBuilding(ptr_tree,ptr_str);
	int leaf_count=0;
	int *ptr_count=&leaf_count;
	LeavesCounting(ptr_tree,ptr_count);
	printf("%d",leaf_count);
	return 0;
}

推荐大家像我这样,先在纸上把思路都理顺了,最后一步再去上机敲代码,这样子效率会提升很多很多。 

二.思路改进

我们改进“二叉树创建函数”:

1.用getchar()代替字符指针ptr_str;

2.将结点的创立放到子函数中完成

3.在结构体struct BinTreeNode的定义中,去掉指向父节点的指针“*dad”。

改进后的“二叉树创建函数”如下所示:

PBinTreeNode BinTreeBuilding(){
	//这里,用getchar()代替字符指针 
	char s=getchar();
	if(s!='#'){
		PBinTreeNode cur=CreateBinTreeNode();
		cur->info=s;
		//将结点的创立下放到子函数中 
		cur->lchild=BinTreeBuilding();
		cur->rchild=BinTreeBuilding();
		//去掉了指向父结点的指针 
		return cur;
	}else{
		return NULL;
	}
}

我们还要改进“叶子结点统计函数”:

1.我们利用好函数的返回值,而不单单只是利用指针

改进后的“叶子结点统计函数”如下所示:

int LeavesCounting(PBinTreeNode ptr_node){
	//统计二叉树中叶子结点的数目 
	if(ptr_node->lchild==NULL&&ptr_node->rchild==NULL){
		return 1;
	}else{
		int n_lchild=0,n_rchild=0;
		if(ptr_node->lchild!=NULL){
			n_lchild=LeavesCounting(ptr_node->lchild);
		}
		if(ptr_node->rchild!=NULL){
			n_rchild=LeavesCounting(ptr_node->rchild);
		}
		return n_lchild+n_rchild;
	}
}

改进后完整的源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef char DataType;
typedef struct BinTreeNode{
	DataType info;
	struct BinTreeNode* lchild;
	struct BinTreeNode* rchild;
}BinTreeNode,*PBinTreeNode; 

void InitializeBinTreeNode(PBinTreeNode PNode){
	//初始化二叉树中的一个结点 
	PNode->info='0';
	PNode->lchild=NULL;
	PNode->rchild=NULL;
}

PBinTreeNode CreateBinTreeNode(){
	//创建二叉树中的一个结点 
	PBinTreeNode PNode=(PBinTreeNode)malloc(sizeof(BinTreeNode));
	if(PNode==NULL){
		printf("out of space!");
	}else{
		InitializeBinTreeNode(PNode);
		return PNode;
	}
}

PBinTreeNode BinTreeBuilding(){
	//这里,用getchar()代替字符指针 
	char s=getchar();
	if(s!='#'){
		PBinTreeNode cur=CreateBinTreeNode();
		cur->info=s;
		//将结点的创立下放到子函数中 
		cur->lchild=BinTreeBuilding();
		cur->rchild=BinTreeBuilding();
		//去掉了指向父结点的指针 
		return cur;
	}else{
		return NULL;
	}
}

int LeavesCounting(PBinTreeNode ptr_node){
	//统计二叉树中叶子结点的数目 
	if(ptr_node->lchild==NULL&&ptr_node->rchild==NULL){
		return 1;
	}else{
		int n_lchild=0,n_rchild=0;
		if(ptr_node->lchild!=NULL){
			n_lchild=LeavesCounting(ptr_node->lchild);
		}
		if(ptr_node->rchild!=NULL){
			n_rchild=LeavesCounting(ptr_node->rchild);
		}
		return n_lchild+n_rchild;
	}
}

int main(){
	PBinTreeNode ptr_node=BinTreeBuilding();
	int leaf_count=LeavesCounting(ptr_node);
	printf("%d",leaf_count);
	return 0;
}

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
哈夫曼编码是一种常用的数据压缩算法,可以将原始数据转换为更短的编码,从而减少存储空间。它的基本思想是:根据字符出现的频率,构建一颗二叉树,使得出现频率高的字符离根节点近,出现频率低的字符离根节点远。然后,对于每个字符,从根节点出发,沿着对应的路径到达该字符所在的叶子节点,记录下路径,作为该字符的编码。 哈夫曼编码的具体实现步骤如下: 1. 统计每个字符在原始数据中出现的频率。 2. 根据字符的频率构建哈夫曼树。构建方法可以采用贪心策略,每次选择出现频率最低的两个字符,将它们作为左右子节点,父节点的权值为两个子节点的权值之和。重复这个过程,直到只剩下一个根节点。 3. 对哈夫曼树进行遍历,记录下每个字符的编码,为了避免编码产生歧义,通常规定左子节点为0,右子节点为1。 4. 将原始数据中的每个字符,用它对应的编码来代替。这一步可以通过哈夫曼树来实现。 5. 将编码后的数据存储起来。此时,由于每个字符的编码长度不同,所以压缩后的数据长度也不同,但总体上来说,压缩效果通常是比较好的。 实现哈夫曼编码的关键在于构建哈夫曼树和计算每个字符的编码。构建哈夫曼树可以采用优先队列来实现,每次从队列中取出两个权值最小的节点,合并成一个节点,再将合并后的节点插入队列中。计算每个字符的编码可以采用递归遍历哈夫曼树的方式,从根节点出发,如果走到了左子节点,则将0添加到编码中,如果走到了右子节点,则将1添加到编码中,直到走到叶子节点为止。 以下是基于C++的代码实现,供参考: ```c++ #include <iostream> #include <queue> #include <string> #include <unordered_map> using namespace std; // 定义哈夫曼树节点的结构体 struct Node { char ch; // 字符 int freq; // 出现频率 Node* left; // 左子节点 Node* right; // 右子节点 Node(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {} }; // 定义哈夫曼树节点的比较函数,用于优先队列的排序 struct cmp { bool operator() (Node* a, Node* b) { return a->freq > b->freq; } }; // 构建哈夫曼树的函数 Node* buildHuffmanTree(unordered_map<char, int> freq) { priority_queue<Node*, vector<Node*>, cmp> pq; for (auto p : freq) { pq.push(new Node(p.first, p.second)); } while (pq.size() > 1) { Node* left = pq.top(); pq.pop(); Node* right = pq.top(); pq.pop(); Node* parent = new Node('$', left->freq + right->freq); parent->left = left; parent->right = right; pq.push(parent); } return pq.top(); } // 遍历哈夫曼树,计算每个字符的编码 void calcHuffmanCode(Node* root, unordered_map<char, string>& code, string cur) { if (!root) return; if (root->ch != '$') { code[root->ch] = cur; } calcHuffmanCode(root->left, code, cur + "0"); calcHuffmanCode(root->right, code, cur + "1"); } // 将原始数据编码成哈夫曼编码 string encode(string s, unordered_map<char, string> code) { string res; for (char c : s) { res += code[c]; } return res; } // 将哈夫曼编码解码成原始数据 string decode(string s, Node* root) { string res; Node* cur = root; for (char c : s) { if (c == '0') { cur = cur->left; } else { cur = cur->right; } if (!cur->left && !cur->right) { res += cur->ch; cur = root; } } return res; } int main() { string s = "abacabad"; unordered_map<char, int> freq; for (char c : s) { freq[c]++; } Node* root = buildHuffmanTree(freq); unordered_map<char, string> code; calcHuffmanCode(root, code, ""); string encoded = encode(s, code); string decoded = decode(encoded, root); cout << "Original string: " << s << endl; cout << "Encoded string: " << encoded << endl; cout << "Decoded string: " << decoded << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值