二叉搜索树-c语言实现-妙用递归

二叉搜索树

1.性质:
所有比根节点的小的数据放入左孩子,所有比根节点大的数据放在右孩子处。

代码实现:
注:前序遍历二叉树[根-左-右]
中序遍历二叉树[左-根-右]
后序遍历二叉树[左-右-根]

typedef struct Node
{
	int data ;
	struct Node* lchild ;
	struct Node* rchild ;
}Node;

typedef struct BSTree
{
	Node* root ;
	int length ;
}BSTree;

Node* GetNewNode(int val)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	newNode->data = val ;
	newNode->lchild = NULL ;
	newNode->rchild = NULL ;
	return newNode;
}

BSTree* GetNewTree()
{
	BSTree* Tree = (BSTree*)malloc(sizeof(BSTree));
	Tree->length = 0  ;
	Tree->root = NULL ;

	return Tree;
}

//【1】插入元素
Node* Insert_Node(Node* root, int val , int* flag)//return root 都是用于返回上一层递归
{
	if (root == NULL) {
		root = GetNewNode(val);
		*flag = 1 ; //用于记录插入元素是否成功
		return root;
	}
	//判断如何插入,递归实现
	if (root->data == val) {
		return root; 
	}
	//将root的两个子节点作为下层的父节点进行递归
    if (root->data > val){
		root->lchild = Insert_Node(root->lchild, val, flag);
	} 
	else {
		root->rchild = Insert_Node(root->rchild, val, flag); 
	}
	return root ;
}

void Insert_Tree(BSTree* Tree, int val)
{
	int flag = 0 ;
	Tree->root = Insert_Node(Tree->root, val ,&flag );
	if (flag == 1){
		Tree->length++; 
	}
}


//【2】清空二叉树
void  Clear_Node(Node* root)
{
	if (root == NULL) return ;
	Clear_Node(root->lchild);
	Clear_Node(root->rchild);
	//出了root->rchild 说明遍历到最底层的节点,因此从下开始往上释放
	//递归--回溯【从下往上释放节点】
	free(root);
	return;
}

void Clear_Tree(BSTree* Tree)
{
	if (Tree->root == NULL) return ;
	Clear_Node(Tree->root);
	free(Tree);
}


//【3】遍历二叉树

//1.前序遍历二叉树-(根左右)
void pre_order_node(Node* root)
{
	if (root == NULL) return ;
	printf("%d ",root->data);
	pre_order_node(root->lchild);
	pre_order_node(root->rchild);
}

void pre_order_tree(BSTree* Tree)
{
	if (Tree == NULL) return;
	printf("pre_order: ");
	pre_order_node(Tree->root);
	printf("\n");
}

//2.中序遍历二叉树-(左)
void mid_order_node(Node* root)
{
	if (root == NULL) return ;
	mid_order_node(root->lchild);
	printf("%d ", root->data);
	mid_order_node(root->rchild);
}

void mid_order_tree(BSTree* Tree)
{
	if (Tree == NULL) return;
	printf("mid_order: ");
	mid_order_node(Tree->root);
	printf("\n");
}
//3.后序遍历二叉树
void post_order_node(Node* root)
{
	if (root == NULL) return;
	post_order_node(root->lchild);
	post_order_node(root->rchild);
	printf("%d ", root->data);
} 

void post_order_tree(BSTree* Tree)
{
	if (Tree == NULL) return;
	printf("post_orderr: ");
	post_order_node(Tree->root);
	printf("\n");
}


//【4】以广义表的形式输出二叉树
void output_node(Node* root)
{
	if (root == NULL) return ;
	printf("%d ",root->data);
	if (root->lchild == NULL && root->rchild == NULL) return;
	printf("(");
	output_node(root->lchild);
	printf(",");
	output_node(root->rchild);
	printf(")");
}

void output_Tree(BSTree* Tree)
{
	if (Tree == NULL) return;
	printf("Tree[%d]: " , Tree->length);
	output_node(Tree->root);
	printf("\n");
}

int main()
{
	srand((size_t)time(0));
	BSTree* Tree = GetNewTree();
#define MAX_OP 10 
	for (int i = 0; i < MAX_OP; i++)
	{
		Insert_Tree(Tree, rand() % 20);
	}
	output_Tree(Tree);

	pre_order_tree(Tree);
	mid_order_tree(Tree);
	post_order_tree(Tree);
#undef MAX_OP
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值