C语言实现二叉树排序

一、代码

  • #include<stdio.h>
    #include<stdlib.h>
    #define N 10
    int a[N]={3,2,5,8,4,7,6,9,1,10};
    
    typedef struct tree
    {
    	int data;
    	struct tree *lchild;
    	struct tree *rchild;
    }BitTree;
    
    void Sort_Tree(BitTree *bt,int key)  //在二叉排序树中插入查找关键字key;
    {
    	BitTree *parent;
    	BitTree *p=(BitTree *)malloc(sizeof(BitTree));
    	p->data=key;               //保存结点数据;
    	p->lchild=p->rchild=NULL;  //左右子树置空;
    	BitTree *head=bt;
    	while(head)                //查找关键字所在的位置;
    	{
    		parent=head;
    		if(key<head->data)     //如果关键字小于结点的数据;
    			head=head->lchild; //在左子树上查找;
    		else                   //若关键字大于结点的数据
    			head=head->rchild; //再右子树上查找;
    	}
    	//判断添加到左子树还是右子树;
    	if(key<parent->data)  //小于父结点;
    		parent->lchild=p; //添加到左子树;
    	else                  //大于父结点;
    		parent->rchild=p; //添加到右子树;
    }
    
    //中序遍历输出二叉排序树;
    void InOrder(BitTree *bt)  
    {
    	if(bt)  //树不为空,则执行如下操作;
    	{
    		InOrder(bt->lchild);   //中序遍历左子树;
    		printf("%d ",bt->data);  //输出结点数据;
    		InOrder(bt->rchild);  //中序遍历右子树;
    	}
    }
    
    int main(void)
    {
    	int i;
    	BitTree *bt;  //保存二叉排序树根结点;
    	printf("原数据:\n");
    	for(i=0;i<N;i++)
    		printf("%d ",a[i]);
    	printf("\n\n");
    	
    	//初始化根结点;
    	bt=(BitTree *)malloc(sizeof(BitTree));
    	bt->data=a[0];
    	bt->lchild=bt->rchild=NULL;
    	//将其他数组数据进行排序;
    	for(i=1;i<N;i++)
    		Sort_Tree(bt,a[i]);
    
    	printf("遍历二叉排序树:\n");
    	InOrder(bt);
    	printf("\n");
    	return 0;
    }

     

二、结果 

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现排序二叉树的代码,其中包括插入节点、查找节点和删除节点的操作: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 插入节点 TreeNode* insert(TreeNode* root, int val) { if (root == NULL) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } if (val < root->val) { root->left = insert(root->left, val); } else { root->right = insert(root->right, val); } return root; } // 查找节点 TreeNode* search(TreeNode* root, int val) { if (root == NULL || root->val == val) { return root; } if (val < root->val) { return search(root->left, val); } else { return search(root->right, val); } } // 删除节点 TreeNode* delete(TreeNode* root, int val) { if (root == NULL) { return root; } if (val < root->val) { root->left = delete(root->left, val); } else if (val > root->val) { root->right = delete(root->right, val); } else { if (root->left == NULL) { TreeNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { TreeNode* temp = root->left; free(root); return temp; } TreeNode* temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = delete(root->right, temp->val); } return root; } int main() { TreeNode* root = NULL; root = insert(root, 5); root = insert(root, 3); root = insert(root, 7); root = insert(root, 1); root = insert(root, 9); TreeNode* node = search(root, 7); if (node != NULL) { printf("Found node: %d\n", node->val); } else { printf("Node not found.\n"); } root = delete(root, 5); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值