二叉树的创建,遍历,查找算法及其程序实现(傻瓜版)

前一个月学习了一下二叉树,现在和大家分享一下。(高手勿看)

二叉树常被用于实现二叉查找树和二叉堆。值得注意的是,二叉树不是树的特殊情形。在图论中,二叉树是一个连通的无环图,并且每一个顶点的度不大于3。有根二叉树还要满足根结点的度不大于2。有了根结点后,每个顶点定义了唯一的根结点,和最多2个子结点。然而,没有足够的信息来区分左结点和右结点。

请看下面的程序:

//二叉树创建->前序中序后序遍历->查找
#include<stdio.h> 
#include<stdlib.h> 
typedef struct bitnode 
{ 
	char data; 
	struct bitnode *lchild,*rchild; 
}bitnode,*bitree;//二叉树节点类型和节点指针类型 
bitree create()//先序创建 
{ 
	bitree root=NULL; 
	char c; 
	scanf("%c",&c); 
	fflush(stdin); 
	if(c=='#')return NULL; 
	else 
	{ 
		root=(bitnode*)malloc(sizeof(bitnode)); 
		root->data=c; 
		root->lchild=create(); 
		root->rchild=create(); 
	} 
	return root; 
} 
void preorder(bitree root)//先序遍历 
{ 
	if(!root)return; 
	else 
	{ 
		putchar(root->data); //一个个输出根
		preorder(root->lchild); 
		preorder(root->rchild); 
	} 
} 
void inorder(bitree root)//中序遍历 
{ 
	if(!root)return; 
	else 
	{ 
		inorder(root->lchild); 
		putchar(root->data); 
		inorder(root->rchild); 
	} 
} 
void postorder(bitree root)//后序遍历 
{ 
	if(!root)return; 
	else 
	{ 
		postorder(root->lchild); 
		postorder(root->rchild); 
		putchar(root->data); 
	} 
} 

bitnode *Search(bitnode *head,char key)
{
	if(head!=NULL)
	{
		if(head->data == key)
		{
			printf("............找到............\n");
			printf("\n对应的数据域=%d,对应的地址=%d\n\n\n",head->data,head);
			return(head); /*已经找到*/
		}
		Search(head->lchild,key); 
		Search(head->rchild,key); 
	}
	return(NULL);
}
void main() 
{   char m;
	bitree root=NULL; 
	printf("输入先序序列:(每次输入一个字符回车)\n");
	root=create(); 

	printf("输入查找的结点:\n");
	m=getchar();
    Search(root,m);

	printf("前序遍历:\n"); 
	preorder(root); 
	printf("\n"); 

	printf("中序遍历:\n"); 
	inorder(root); 
	printf("\n"); 

	printf("后序遍历:\n"); 
	postorder(root); 
	printf("\n"); 
} 
/*输入序列为前序序列,#代表空 
例如二叉树为 
--------a 
-------/-\ 
------b---c 
-----/-\ 
----d---e 
输入abd##e##c##*/ 

这个程序实现二叉树的创建,遍历,查找还是比较简单的,很容易看懂,因为它没有用到栈的知识,而是类似于递归一样的解析,具体就不做详细的论述了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用C语言实现动态输入数据构造排序二叉树并转化为平衡二叉树,并实现查找算法的代码示例: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; // 创建新节点 Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } // 向二叉搜索树插入节点 Node* insertNode(Node* root, int data) { if (root == NULL) { return createNode(data); } if (data < root->data) { root->left = insertNode(root->left, data); } else if (data > root->data) { root->right = insertNode(root->right, data); } return root; } // 将二叉搜索树转化为有序数组 void inorderTraversal(Node* root, int* arr, int* index) { if (root != NULL) { inorderTraversal(root->left, arr, index); arr[*index] = root->data; (*index)++; inorderTraversal(root->right, arr, index); } } // 根据有序数组构建平衡二叉树 Node* buildBalancedBST(int* arr, int start, int end) { if (start > end) { return NULL; } int mid = (start + end) / 2; Node* root = createNode(arr[mid]); root->left = buildBalancedBST(arr, start, mid - 1); root->right = buildBalancedBST(arr, mid + 1, end); return root; } // 在平衡二叉树查找节点 Node* searchNode(Node* root, int key) { if (root == NULL || root->data == key) { return root; } if (key < root->data) { return searchNode(root->left, key); } else { return searchNode(root->right, key); } } // 释放二叉树节点的内存 void freeTree(Node* root) { if (root != NULL) { freeTree(root->left); freeTree(root->right); free(root); } } int main() { Node* root = NULL; int n, data; printf("请输入节点个数:"); scanf("%d", &n); printf("请输入节点值:\n"); for (int i = 0; i < n; i++) { scanf("%d", &data); root = insertNode(root, data); } int* arr = (int*)malloc(n * sizeof(int)); int index = 0; inorderTraversal(root, arr, &index); freeTree(root); root = buildBalancedBST(arr, 0, n - 1); printf("平衡二叉树构建完成!\n"); printf("请输入要查找的节点值:"); scanf("%d", &data); Node* result = searchNode(root, data); if (result != NULL) { printf("找到了!\n"); } else { printf("未找到!\n"); } freeTree(root); free(arr); return 0; } ``` 这段代码首先会动态获取用户输入的节点个数和节点值,然后根据输入的节点值构建一个排序二叉树。接着,通过中序遍历将排序二叉树转化为有序数组。然后,利用有序数组构建平衡二叉树。最后,用户可以输入要查找的节点值,程序将在平衡二叉树中进行查找,并输出查找结果。 注意:这只是一个简单的示例代码,未进行输入验证和错误处理,请根据实际需求进行完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值