C语言树的简单实现和相关算法

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define maxSize 10
typedef struct BTNode
{
	char data;
	struct BTNode* lchild;
	struct BTNode* rchild;
}BTNode;

void init(BTNode**T)
{
	(*T) = (BTNode*)malloc(sizeof(BTNode));
	(*T)->lchild = NULL;
	(*T)->rchild = NULL;
}


//自己初始化一颗树,用来验证算法的正确性
void createT(BTNode** T)
{
	(*T)->data ='A';
    BTNode* l= (BTNode*)malloc(sizeof(BTNode));
	l->data = 'B';
	l->lchild = NULL;
	l->rchild = NULL;
	(*T)->lchild = l;

	BTNode* l2 = (BTNode*)malloc(sizeof(BTNode));
	l2->data = 'D';
	l2->lchild = NULL;
	l2->rchild = NULL;
	(*T)->lchild->lchild = l2;

	BTNode* l3 = (BTNode*)malloc(sizeof(BTNode));
	l3->data = 'X';
	l3->lchild = NULL;
	l3->rchild = NULL;
	(*T)->lchild->lchild->rchild = l3;

	BTNode* r = (BTNode*)malloc(sizeof(BTNode));
	r->data = 'C';
	r->lchild = NULL;
	r->rchild = NULL;
	(*T)->rchild = r;

	BTNode* r2 = (BTNode*)malloc(sizeof(BTNode));
	r2->data = 'E';
	r2->lchild = NULL;
	r2->rchild = NULL;
	(*T)->rchild->lchild = r2;

	BTNode* r3 = (BTNode*)malloc(sizeof(BTNode));
	r3->data = 'F';
	r3->lchild = NULL;
	r3->rchild = NULL;
	(*T)->rchild->rchild = r3;
}

//层次遍历
void level(BTNode**T)
{
	int front=0, rear=0;
	BTNode* que[maxSize];
	BTNode* q;
	if ((*T) != NULL)
	{
		rear = (rear + 1) % maxSize;  //这里假设队列空间足够
		que[rear] = (*T);
		while (front != rear)
		{
			front = (front + 1) % maxSize;
			q = que[front];
			printf("%c",q->data);
			if (q->lchild!=NULL)
			{
				rear = (rear + 1) % maxSize;  //这里假设队列空间足够
				que[rear] = q->lchild;
			}
			if (q->rchild != NULL)
			{
				rear = (rear + 1) % maxSize;  //这里假设队列空间足够
				que[rear] = q->rchild;
			}
		}
	}
}

//先序遍历
void preorder(BTNode** T)
{
	if ((*T) != NULL)
	{
		printf("%c ",(*T)->data);
		preorder(&(*T)->lchild);
		preorder(&(*T)->rchild);
	}
}

//中序遍历
void inorder(BTNode** T)
{
	if ((*T) != NULL)
	{		
		inorder(&(*T)->lchild);
		printf("%c ", (*T)->data);
		inorder(&(*T)->rchild);
	}
}

//后序遍历
void postorder(BTNode** T)
{
	if ((*T) != NULL)
	{
		postorder(&(*T)->lchild);
		postorder(&(*T)->rchild);
		printf("%c ", (*T)->data);
	}
}

//返回一颗树深度
int getDepth(BTNode** T)
{
	int LD, RD;
	if ((*T) == NULL)
		return 0;
	else
	{
		LD = getDepth(&(*T)->lchild);
		RD = getDepth(&(*T)->rchild);
		return (LD > RD ?LD:RD) + 1;
	}
}

//返回一棵树宽度(思路:层次遍历所有结点,为它们打上层数编号,最后在循环找出宽度)
int getWidth(BTNode** T)
{
	typedef struct St
	{
		int Lno;
		BTNode* T;
	}St;

	int front = 0, rear = 0;
	int Lno = 0,max=0;
	int i, j,n;
	BTNode* q;
	St que[maxSize];
	if ((*T) == NULL)
		return 0;
	else
	{
		++rear;
		que[rear].Lno = 1;
		que[rear].T = (*T);
		while (front != rear)
		{
			++front;
			q = que[front].T;
			Lno = que[front].Lno;
			if (q->lchild != NULL)
			{
				++rear;
				que[rear].Lno = Lno+1;
				que[rear].T = q->lchild;
			}
			if (q->rchild != NULL)
			{
				++rear;
				que[rear].Lno = Lno + 1;
				que[rear].T = q->rchild;
			}
		}
		for (i = 1; i <= Lno; ++i)
		{
			n = 0;
			for (j = 1; j <= rear; ++j)
			{
				if (que[j].Lno == i)
					++n;
			}
			if (max < n)
			  max = n;
		}
		return max;
	}
}

//找到树中值为key的结点位置用q指向它,若找不到q指向NULL
void search(BTNode*T,BTNode**q,char key)  
{
	if (T != NULL)
	{
		if (T->data == key)
			(*q) = T;
		else
		{
			//请注意此处的q,和main函数中调用时q
			search(T->lchild,q,key);
			if ((*q) == NULL)
			search(T->rchild,q,key);
		}
	}
}
int main()
{
	BTNode* T;
	init(&T);
	createT(&T);
	printf("层次遍历:");
	level(&T);
	printf("\n");
	printf("先序遍历:");
	preorder(&T);
	printf("\n");
	printf("中序遍历:");
	inorder(&T);
	printf("\n");
	printf("后序遍历:");
	postorder(&T);
	printf("\n");
	printf("树高度:%d\n",getDepth(&T));
	printf("树宽度:%d\n",getWidth(&T));

	char c = 'X', c2 = 'M';
	BTNode* x = NULL;
	search(T, &x, c);
	if (x!=NULL)
	{
		printf("找到%c\n",x->data);
	}
	else
	{
		printf("未找到%c\n",c);
	}
	x = NULL;
	search(T, &x, c2);
	if (x != NULL)
	{
		printf("找到%c\n", x->data);
	}
	else
	{
		printf("未找到%c\n",c2);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用C语言实现哈夫曼的建立算法的一个简单示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 // 定义哈夫曼的结构体 typedef struct { int weight; // 权值 int parent; // 父节点 int leftchild; // 左孩子节点 int rightchild;// 右孩子节点 }HuffmanNode, *HuffmanTree; // 构建哈夫曼 void createHuffmanTree(HuffmanTree *HT, int n, int *w) { // 初始化哈夫曼节点数组 int m = 2 * n - 1; *HT = (HuffmanTree)malloc((m+1) * sizeof(HuffmanNode)); for (int i = 1; i <= m; i++) { (*HT)[i].weight = 0; (*HT)[i].parent = 0; (*HT)[i].leftchild = 0; (*HT)[i].rightchild = 0; } // 输入权值 for (int i = 1; i <= n; i++) { (*HT)[i].weight = w[i-1]; } // 构建哈夫曼 for (int i = n+1; i <= m; i++) { int s1, s2; int min1 = min2 = MAX_SIZE; for (int j = 1; j < i; j++) { if ((*HT)[j].parent == 0) { if ((*HT)[j].weight < min1) { min2 = min1; s2 = s1; min1 = (*HT)[j].weight; s1 = j; } else if ((*HT)[j].weight < min2) { min2 = (*HT)[j].weight; s2 = j; } } } (*HT)[s1].parent = i; (*HT)[s2].parent = i; (*HT)[i].leftchild = s1; (*HT)[i].rightchild = s2; (*HT)[i].weight = (*HT)[s1].weight + (*HT)[s2].weight; } } int main() { HuffmanTree HT; int n, w[MAX_SIZE]; printf("请输入权值个数n:"); scanf("%d", &n); printf("请输入权值:"); for (int i = 0; i < n; i++) { scanf("%d", &w[i]); } createHuffmanTree(&HT, n, w); printf("哈夫曼的结构如下:\n"); printf("weight parent leftchild rightchild\n"); for (int i = 1; i <= 2*n-1; i++) { printf("%-7d %-7d %-10d %-10d\n", HT[i].weight, HT[i].parent, HT[i].leftchild, HT[i].rightchild); } return 0; } ``` 以上是一个简单的哈夫曼建立算法的示例,其中 `createHuffmanTree` 函数实现了哈夫曼的构建过程,主函数通过输入权值个数和权值数组来调用此函数,并输出构建好的哈夫曼的结构。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值