📄 jianshu.cpp
字号:
// jianshu.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
struct ltree
{
int data;
struct ltree *llink;
struct ltree *rlink;
};
typedef struct ltree treenode;
typedef treenode* lnode;
lnode createtree(int n)
{
lnode root=NULL;
for (int i=0;i<n;i++)
{
lnode newnode,currentnode,parentnode;
newnode=(lnode)malloc(sizeof(treenode));
printf("input number:\n");
scanf("%d",&newnode->data);
newnode->llink=NULL;
newnode->rlink=NULL;
currentnode=root;
if (currentnode==NULL)root=newnode;
else
{
while (currentnode!=NULL)
{
parentnode=currentnode;
if (newnode->data<currentnode->data)
currentnode=currentnode->llink;
else
currentnode=currentnode->rlink;
}
if (newnode->data<parentnode->data)
parentnode->llink=newnode;
else
parentnode->rlink=newnode;
}
}
return root;
}
void displaytree(lnode root)
{
lnode p;
p=root->llink;
printf("左子树:\n");
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->llink;
}
p=root->rlink;
printf("右子树:\n");
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->rlink;
}
}
int main(int argc, char* argv[])
{
int n;
lnode root;
printf("input n:\n");
scanf("%d",&n);
root=createtree(n);
displaytree(root);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -