📄 binarytr.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node{
int n;
struct node * left;
struct node * right;
};
struct node * first;
void inorder(struct node * tree);
void insert(struct node * tree,int n);
main()
{
clrscr();
first=NULL;
for(;;)
{
char ch;
printf("enter your choice \n 1:to insert into the binary tree \n 2:inorder traverse tree,any key to exit \n");
ch=getchar();
if(ch=='1')
{
int i;
printf("enter the value to be entered ");
scanf("%d",&i);
insert(first,i);
}
else if(ch=='2')
inorder(first);
else if(ch=='3')
break;
}
return 0;
}
void insert(struct node * tree,int i)
{
struct node * ne;
if(tree==NULL)
{
ne=(struct node *) malloc(sizeof(struct node));
ne->n=i;
first=ne;
ne->left=NULL;
ne->right=NULL;
}
else if((i>(tree->n)))
{
if ((tree->right)!=NULL)
insert(tree->right,i);
else{
struct node *x;
tree->right=(struct node *) malloc(sizeof(struct node));
x=tree->right;
x->n=i;
x->right=NULL;
x->left=NULL;
}
}
else if(i<=(tree->n))
{
if((tree->left)!=NULL)
insert(tree->left,i);
else
{
struct node * x;
x=(struct node *) malloc(sizeof(struct node));
tree->left=x;
x->n=i;
x->left=NULL;
x->right=NULL;
}
}
return;
}
void inorder(struct node * tree)
{
if(tree==NULL)
return;
else
{
inorder(tree->left);
printf("%d",tree->n);
inorder(tree->right);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -