设为首页 加入收藏

TOP

C二叉树基础(一)
2017-03-30 14:17:08 】 浏览:485
Tags:基础
/*************************************************************************
	> File Name: btree.c
	> Author: XXDK
	> Email: v.manstein@qq.com 
	> Created Time: Fri 10 Mar 2017 12:19:59 AM PST
 ************************************************************************/

#include
  
   
#include"linkqueue.h"
//#include"linkstack.h"

typedef char data_t;
typedef struct btnode {
	data_t data;
	struct btnode* lchild;
	struct btnode* rchild;
}btree_t;

 void* assertx(void* p)
{
	if(p == NULL) {
		printf("argument error\n");
		return NULL;
	}
	
}

/**
 *	@data: 节点数据指针
 *	@num:  节点位置编号从根1开始
 *	@len:  待存入树节点中的数据的长度
 */
btree_t* create_btree(data_t* data, int num, int len)
{
	btree_t *root;
	// 1.
	root = (btree_t*)malloc(sizeof(btree_t));
	if(root == NULL) {
		printf("allocate memory error\n");
		return NULL;
	}
	// 2.
	root->data = data[num];
	root->lchild = NULL;
	root->rchild = NULL;
	// 3. 这里利用满二叉树的特性
	if(2*num <= len) {
		root->lchild = create_btree(data, 2*num, len);

	}
	if(2*num+1 <= len) {
		root->rchild = create_btree(data, 2*num+1, len);
	}

	return root;
}
// 层次遍历
int btree_travers_layer(btree_t* root)
{
	assertx(root);
	btree_t* temp;
	LinkQueue* queue = create_empty_linkqueue();

	// 1. root 入队列
	enter_linkqueue(queue, root);
	// 2. root 出队列,每出一个,拿到其孩子节点放入队尾
	// 直到队列为空,完成遍历
	while(!is_empty_linkqueue(queue)) {
		temp = delete_linkqueue(queue);
		printf("[%c] ", temp->data);
		if(temp->lchild != NULL) 
			enter_linkqueue(queue, temp->lchild);
		if(temp->rchild != NULL)
			enter_linkqueue(queue, temp->rchild);
	}
	printf("\n");
}
// 前序遍历
int btree_travers_preorder(btree_t* root)
{	
	if(root == NULL) {
		return -1;
	}
	printf("[%c] ", root->data);
	if(root->lchild != NULL) {
		btree_travers_preorder(root->lchild);
	}
	if(root->rchild != NULL) {
		btree_travers_preorder(root->rchild);
	}

	return 0;

}


// 中序遍历
int btree_travers_midorder(btree_t* root)
{	
	if(root == NULL) {
		return -1;
	}
	if(root->lchild != NULL) {
		btree_travers_midorder(root->lchild);
	}
	printf("[%c] ", root->data);
	if(root->rchild != NULL) {
		btree_travers_midorder(root->rchild);
	}

	return 0;

}
// 后序遍历
int btree_travers_postorder(btree_t* root)
{	
	if(root == NULL) {
		return -1;
	}
	if(root->lchild != NULL) {
		btree_travers_postorder(root->lchild);
	}
	if(root->rchild != NULL) {
		btree_travers_postorder(root->rchild);
	}
	printf("[%c] ", root->data);

	return 0;

}
/*****************************************************
 * 计算树的高度
 *	思路: 
 *	    用两个队列A,B。第一层节点入A队列,弹出A
 *	队列中的元素所有元素,每出一个,判读其左右孩子
 *	若果存在孩子,则孩子入B队列。层数累加。然后弹出
 *	所有B的元素,同样每出一个,判断左右孩子,存在则
 *	入A队列,层数累加。如此直到队列为空。
 *****************************************************/
unsigned int count_btree_height(btree_t* root)
{
	unsigned int count = 0;
	btree_t* temp;
	LinkQueue*	Aq = create_empty_linkqueue();
	LinkQueue*	Bq = create_empty_linkqueue();
	LinkQueue*  	Tq;

	if(root == NULL) {
		return 0;
	}

	enter_linkqueue(Aq, root);
	while(!is_empty_linkqueue(Aq)) {		// 判断也就是上一层的孩子构成的队列是否为空
		while(!is_empty_linkqueue(Aq)) {	// 出队直到整个队列
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言函数 atoi() 下一篇C语言写个无限循环

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目