二叉树遍历相关算法

/*************************************************************
 * file: binary_tree_traverse.c
 * brief:二叉树遍历的相关算法
 * yejing@2015.2.11    1.0      creat
 * yejing@2015.2.14    1.1      added二叉树层次遍历
 * yejing@2015.2.15    1.2      added已知前序中序重建二叉树
 * yejing@2015.2.16    1.3      added已知二叉树前序中序求后序
 *************************************************************/
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 
 typedef _node_t{
	struct _node_t* left;
	struct _node_t* right;
	int value;
 }node_t;

/**********************二叉树的递归遍历**********************/ 
 static void recursive_preorder(node_t* root){
	if(!root)
		return;
	printf("%d ", root->value);
	
	if(root->left)
		recursive_preorder(root->left);
	if(root->right)
		recursive_preorder(root->right);
		
	return;
 }
 
 static void recursive_inorder(node_t* root){
	if(!root)
		return;
		
	if(root->left)
		recursive_preorder(root->left);
	
	printf("%d ", root->value);
	
	if(root->right)
		recursive_preorder(root->right);
	
 }
 
 static void recursive_postorder(node_t* root){
	if(!root)
		return;
	
	if(root->left)
		recursive_preorder(root->left);
	if(root->right)
		recursive_preorder(root->right);
		
	printf("%d ", root->value);
		
	return;
 }
 
 
 /*********************二叉树的非递归遍历*********************/
#define MAX_SIZE    1024 
 typdef struct _assist_stack_t{
	node_t* array[MAX_SIZE];
	int    count;
 }assist_stack_t;
 
 static void stack_push(assist_stack_t ast_stck, node_t node){
	if(ast_stck.count >= MAX_SIZE)
		return;
	ast_stck.array[ast_stck.count++] = node;
	return;
 }
 
 static node_t stack_pop(assist_stack_t ast_stck){
	if(ast_stck.count <= 0)
		return NULL;
	
	return ast_stck.array[--ast_stck.count];
 }
 
 static int stack_getsize(assist_stack_t ast_stck){
	return ast_stck.count;
 }
 
 
 static void loop_preorder(node_t* root){
	if(!root)	
		return;
	
	node_t* tmp = root;
	assist_stack_t ast_stck;
	memset((char*)&ast_stck, 0, sizeof(ast_stck));
	ast_stck.count = 0;
	stack_push(ast_stck, tmp);
	printf("\033[1;31;40m  %s[start]\033[0m \n", __func__);
	
	while(!tmp || stack_getsize(ast_stck)){
		while(!tmp){
			push(ast_stck, tmp);
			printf("%d ", tmp->value);
			tmp = tmp->lchild;
		}
		if(stack_getsize(ast_stck))
			tmp = stack_pop(ast_stck)
			if(tmp)
				tmp = tmp->rchild;
		
	}
	printf("\033[1;31;40m  %s[end]\033[0m \n", __func__);
	return;
	
 }
 
 static void loop_inorder(node_t* root){
	if(!root)	
		return;
	
	node_t* tmp = root;
	assist_stack_t ast_stck;
	memset((char*)&ast_stck, 0, sizeof(ast_stck));
	ast_stck.count = 0;
	stack_push(ast_stck, tmp);
	printf("\033[1;31;40m  %s[start]\033[0m \n", __func__);
	
	while(!tmp || stack_getsize(ast_stck)){
		while(!tmp){
			push(ast_stck, tmp);
			tmp = tmp->lchild;
		}
		if(stack_getsize(ast_stck))
			tmp = stack_pop(ast_stck)
			printf("%d ", tmp->value);
			if(tmp)
				tmp = tmp->rchild;
		
	}
	printf("\033[1;31;40m  %s[end]\033[0m \n", __func__);
	return;
 }
 
 static void loop_postorder(node_t* root){
 
 
 }
 
/***************二叉树的层次遍历(借助辅助队列)****************/
typedef struct _list_t{
	node_t* node;
	struct _list_t* next;
}list_t;

typedef _queue_t{
	list_t* head;
	list_t* tail;
}queue_t;

static void enqueue(queue_t* queue, node_t* node){
	if(!node || !queue)
		return;
	list_t* tmp = queue->head;
	
}
static list_t* dequeue(queue_t* queue){

}

static int is_queue_empty(queue_t* queue){
	
}

static void binary_tree_level_traverse(node_t* root){
	if(!root)
		return;
	queue_t queue;
	queue->head = queue->tail = NULL;
	list_t* tmp = (list*)malloc(sizeof(list_t));
	enqueue(queue, tmp);
	
	while(!is_queue_empty(queue)){
		tmp = dequeue(queue);
		printf("%d ", tmp->value);
		if(!tmp){
			if(!tmp->lchild)
				enqueue(queue, tmp->lchild);
			if(!tmp->rchild)
				enqueue(queue, tmp->rchild);
		}
	}
	printf("\n");
	
	return;
}

/*******************已知前序中序重建二叉树********************/
/*
brief:通过前序中序递归重建二叉树,通过后序中序求前序类似,只不过后序的根节点在最后。
params:
	preOrder 前序遍历向量
	inOrder  中序遍历向量
	treeLen  树节点总数
return:
	重建树的跟节点
*/
static void binary_tree_rebuild(int* preOrder, int* inOrder, int treeLen, node_t** root){
	if(!preOrder || !inOrder || treeLen < 1 || !*root)
		return;
		
	node_t* tmp = (node_t*)malloc(sizeof(node_t));
	if(!tmp)
		return;
	
	memset((char*)tmp, 0, sizeof(node_t));
	//前序遍历第一个元素是根
	tmp->value = *preOrder;
	*root = tmp;
	
	//递归退出条件,到最后一个节点
	if(treelen == 1) 
		return;
	
	//定位中序中的根,在根之前的为左子,之后的为右子
	int* cursor = inOrder;
	int cursor_len = 0;
	while(*cursor != *preOrder){
		if(!cursor)
			return;
		//左子长度
		++cursor_len; 
		++cursor;
	}
	
	int left_len = cursor_len;
	//总长减去根减去左子长度为右子长度
	int right_len = treeLen - cursor_len - 1;
	
	//递归重建左子树
	if(left_len)
		binary_tree_rebuild(preOrder + 1, inOrder, left_len, &((*root)->left));
	
	//递归重建右子树
	if(right_len)
		binary_tree_rebuild(preOrder + left_len + 1, inOrder + left_len + 1, &((*root)->left));
	
}

/*******************已知二叉树前序中序求后序********************/

//重建二叉树,然后后序遍历即可
void get_post_by_in_and_pre(int* preOrder, int* inOrder, int treeLen){
	node_t* root = NULL;
	binary_tree_rebuild(preOrder, inOrder, treeLen, &root);
	if(!root)
		recursive_postorder(root);
		
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值