首页 > 其他 > 详细

binary search tree——二叉搜索树

时间:2014-02-20 11:15:27      阅读:310      评论:0      收藏:0      [点我收藏+]

Binary tree

In computer science, a binary tree is a tree data structure in which each node has at most two children (referred to as the left child and the right child). In a binary tree, the degree of each node can be at most two. Binary trees are used to implement binary search trees and binary heaps, and are used for efficientsearching and sorting. A binary tree is a special case of a K-ary tree, where k is 2.

bubuko.com,布布扣



代码实现:

/*******************************************************************************
Code writer: EOF
Date : 2014.02.19
code purpose:
		example binary search tree 		

e-mail:jasonleaster@gmail.com
********************************************************************************/
#include"stdio.h"
#include"stdlib.h"
#define ARRAYSIZE 10

struct node
{
	int data;
	struct node* leftchild;	
	struct node* rightchild;
};//node initialization

int insert_node(struct node** pp_node,int number);
int print_node(struct node* p_node);
int free_tree(struct node* p_node);


struct node* root = NULL;

int main(int argc, char* argv[])
{
	int temp = 0;
	int array[ARRAYSIZE] = {23,32,4,56,33,98,24,88,45,78};
	
	for(temp = 0;temp < ARRAYSIZE;temp++)
	{
		if(insert_node(&root,array[temp]) != 0)
		{
			break;
		}
	}
	
	print_node(root);

	return 0;
}

int insert_node(struct node** pp_node,int number)
{
	if((*pp_node) == NULL)
	{
		(*pp_node) = (struct node*)malloc(sizeof(struct node));
		if(*pp_node == NULL)
		{
			printf("memory allocation failed\nprocess end!\n");
			return 1;
		}
		(*pp_node)->data = number;
		(*pp_node)->leftchild = NULL;
		(*pp_node)->rightchild = NULL;
		
		return 0;
	}
	else //use recursion to build the binary tree
	{
		if((*pp_node)->data < number)
		{
			insert_node(&((*pp_node)->rightchild),number);
		}
		else
		{
			insert_node(&((*pp_node)->leftchild),number);
		}
		
	}
}

int print_node(struct node* p_node)// use recursion to print out the data in the binary tree
{
	// print the left side of the binary tree
	if(p_node->leftchild != NULL)
	{
		print_node(p_node->leftchild);
	}
	printf("%d\n",p_node->data);

	// print the right side of the binary tree
	if(p_node->rightchild != NULL)
	{
		print_node(p_node->rightchild);
	}
	//printf("%d\n",p_node->data); we don‘t need this 
}

int free_tree(struct node* p_node)// use recursion to free the memory we allocated
{
	if(p_node->leftchild != NULL)
	{
		free_tree(p_node->leftchild);
		free_tree(p_node->rightchild);
		free(p_node);
	}
	
	if(p_node->rightchild != NULL)
	{
		free_tree(p_node->rightchild);
	}

}

学习感想:

1.知道二叉树很长时间了,但是一直没有实现,多次失败,感觉懂了,但是就是实现不了。这次搞定了。有些事就是要死磕,一次不行再来一次。

2.递归真的很重要!很多算法的实现都是用递归,而我往往有点对递归没好感,是自己不熟悉的原因吧,用的少。

3.我始终认为,任何事情,只有真的做到了,才叫学会,所以我死磕。


loving you, tree...................




binary search tree——二叉搜索树

原文:http://blog.csdn.net/cinmyheart/article/details/19495197

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!