bstree.c

来自「all traversals in binary search tree wit」· C语言 代码 · 共 92 行

C
92
字号
/*  Binary search tree*/
#include<stdio.h>
#include<stdlib.h>

struct tree
{
struct tree *left;
int data;
struct tree *right;
};
typedef struct tree btree;
void inorder(btree *);
void preorder(btree *);
void postorder(btree *);
void insert(btree **,int);
btree  *prt = NULL;
main()
{
	int i=0, nums, v1;
	btree *bt;
	bt=NULL;
	printf("specify the total number of items\n");
	scanf("%d",&nums);

	while(i++ < nums)
	{
		printf("enter  data\n");
		scanf("%d",&v1);

		insert(&bt,v1);
	}
	inorder(bt);
	printf("\n");
	preorder(bt);
	printf("\n");
	postorder(bt);
	getch();
}
void insert(btree **bt,int num)
{

	if(*bt == NULL)
	{
	 (*bt)=(btree *)malloc(sizeof(btree));
	 (*bt) -> left = NULL;
	 (*bt) -> right = NULL;
	 (*bt) -> data = num;
	}
	else
{
		if(num<(*bt)->data)
			insert(&((*bt)->left),num);
		else
			insert(&((*bt)->right),num);
	}
return;
}
void inorder(btree *bt)
{
	if(bt!=NULL)
	{
		inorder(bt->left);
		printf("%d-->",bt->data);
		inorder(bt->right);
	}

	else
	return;
}
void preorder(btree *bt)
{
	if(bt!=NULL)
	{
		printf("%d-->",bt->data);
		preorder(bt->left);
		preorder(bt->right);
	}
	else
	return;
}
void postorder(btree *bt)
{
	if(bt!=NULL)
	{
		postorder(bt->left);
		postorder(bt->right);
		printf("%d-->`",bt->data);
	}
	else
	return;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?