📄 main.c
字号:
#include <stdio.h>
#include <stdlib.h>
/*===========================================*/
//定义结构体
/*===========================================*/
typedef struct Ltree{
int data;
struct Ltree *left, *right;
}Tree;
/*===========================================*/
//插入节点
/*===========================================*/
void Insert_Tree(Tree **root, Tree *temp)
{
if((*root)==NULL) /* 把空节点插入新节点 */
(*root)=temp;
else if(temp->data==(*root)->data) /* 值相同的者返回 */
return;
else if(temp->data > (*root)->data) /* data值大 递归right */
Insert_Tree((&(*root)->right),temp);
else if(temp->data < (*root)->data)
Insert_Tree((&(*root)->left),temp); /* data值小 递归left */
}
/*===========================================*/
//建立二叉排序树
/*===========================================*/
void Create_Tree(Tree **root)
{
int scan_x;
Tree *s;
printf("输入二叉排序树元素(输入[-1]结束)\n输入元素:");
scanf("%d",&scan_x);
s=(Tree *)malloc(sizeof(Tree));
if(s==NULL)
printf("申请空间失败\n");
else
{
while(scan_x!=-1)
{
s->data=scan_x;
//置空左右子树为Insert_Tree()插入准备
s->left=s->right=NULL;
Insert_Tree(&(*root),s);
printf("输入元素:");
scanf("%d",&scan_x);
s=(Tree *)malloc(sizeof(Tree));
if(s==NULL)
{
printf("申请空间失败\n");
break;
}
}
}
}
/*===========================================*/
//中序遍历输出
/*===========================================*/
void PerOut_Tree(Tree *root)
{
if(root!=NULL)
{
PerOut_Tree(root->left);
printf("%d\t",root->data);
PerOut_Tree(root->right);
}
}
main()
{
Tree *Root=NULL;
Create_Tree(&Root);
PerOut_Tree(Root);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -