二叉查找树转换为有序双向链表---微软

/*************************************************************
 * file:transfer_binary_tree_to_double_link_list.c
 * brief:通过只修改指针将二叉查找树转换为双向有序链表
 * yejing@2015.2.3    1.0      creat
 *************************************************************/
 #include <stdio.h>
 #include <stdlib.h>
 #inlcude <string.h>
 #include <assert.h>
 
 
 typedef enum{
	LEFT_CHILD = 2,
	RIGHT_CHILD,
	ROOT
 }LEFT_OR_RIGHT;
 
 typedef struct _node_t{
	int value;
	int left_or_right;
	struct _node_t* left;
	struct _node_t* right;
 }node_t;
 
static node_t* process_a_node(node_t* node){
	node* left_tmp  = NULL;
	node* right_tmp = NULL;
	if(node->left){
		node->left->left_or_right = LEFT_CHILD;
		left_tmp = process_a_node(node->left);
	}
	if(node->right){
		node->left->left_or_right = RIGHT_CHILD;
		right_tmp = process_a_node(node->right);
	}
		
	
	if(left_tmp && right_tmp){
		//左子树用右儿子上指,右儿子用左子树上指
		left_tmp->right = node;
		right_tmp->left = node;
		if(node->left_or_right == LEFT_CHILD)
			return right_tmp;
		else if(node->left_or_right == RIGHT_CHILD)
			return left_tmp;
		else
			return node;
	}
	
	if(left_tmp){
		left_tmp->right = node;
		if(node->left_or_right == LEFT_CHILD)
			return node;
		else if(node->left_or_right == RIGHT_CHILD)
			return left_tmp;
		else
			return node;		
	}
	
	if(right_tmp){
		right_tmp->left = node;
		if(node->left_or_right == LEFT_CHILD)
			return right_tmp;
		else if(node->left_or_right == RIGHT_CHILD)
			return node;
		else
			return node;
	
	}
	
	return node;
}

static node_t* create_binary_search_tree_node(int value){
	node_t* root = (node_t*)malloc(sizeof(node_t));
	if(!root)
		assert(0);
	root->value = value;
	root->left  = NULL;
	root->right = NULL;
	
	return root;
}

static search_appriproate_position(node_t* root, int value){
	if(!root)
		assert(0);
	node_t* tmp = root;
	do{
		if(value < tmp->value){
			if(tmp->left)
				tmp = tmp->left;
			else
				return tmp;
		}
		else{
			if(tmp->right)
				tmp = tmp-right;
			else
				return tmp;
		}
	}while(1);
	return NULL;
}

static node_t* add_node_to_tree(node_t* root, int value){
	node_t* tmp = create_binary_search_tree_node(value);
	if(!tmp)
		return root;
		
	node_t* pos = search_appriproate_position(root, value);
	if(pos){
		if(value < pos->value)
			pos->left = tmp;
		else
			pos->right = tmp;
	}
	
	return root;
}

static show_list_doule_direction(node_t* root){
	if(!root)
		return;
		
	node_t* tmp = root;
	node_t* head, tail;
	//从根节点往左走,找到头结点
	do{
		if(tmp->left)
			tmp = tmp->left;
		else
			break;
	}while(1);
	head = tmp;
	
	//从根节点往右走,找到尾结点
	tmp = root;
	do{
		if(tmp->right)
			tmp = tmp->right;
		else
			break;
	}while(1);
	tail = tmp;
	
	printf("\033[1;31;40m show list from head: \033[0m");
	while(head){
		printf("%d ", head->value);
		tmp = head->right;
	}
	
	printf("\033[1;31;40m show list from head: \033[0m");
	while(tail){
		printf("%d ", tail->value);
		tail = tail->left;
	}
	
	return;
}

int main(int argc, char* argv[]){
	node_t* root = create_binary_search_tree_node(8);
	if(!root)
		assert(0);
		
	add_node_to_tree(root, 6);
	add_node_to_tree(root, 10);
	add_node_to_tree(root, 5);
	add_node_to_tree(root, 7);
	add_node_to_tree(root, 9);
	add_node_to_tree(root, 11);
	process_a_node(root);

	show_list_doule_direction(root);
	
	return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值