标签:return str 左右子树 name font size image 二叉查找树 main
输入n,然后n个树,建立二叉查找树。从小到大输出每个节点的左右子树,空输出#

#include<cstdio>
#include<iostream>
using namespace std;
typedef struct node{
int data;
struct node *lchild,*rchild;
}NODE;
void input(NODE *root,int value){
if(value==root->data){
return;
}
else if(value>root->data){
if(root->rchild==NULL){
root->rchild=new NODE;
root->rchild->data=value;
root->rchild->lchild=NULL;
root->rchild->rchild=NULL;
}
else{
input(root->rchild,value);
}
}
else{
if(root->lchild==NULL){
root->lchild=new NODE;
root->lchild->data=value;
root->lchild->lchild=NULL;
root->lchild->rchild=NULL;
}
else{
input(root->lchild,value);
}
}
}
int count=0,n;
void preorder(NODE *root){
if(root==NULL)return;
preorder(root->lchild);
if(count<n){
printf("%d(",root->data);
if(root->lchild==NULL){
printf("#");
}
else{
printf("%d",root->lchild->data);
}
if(root->rchild==NULL){
printf(", #)\n");
}
else{
printf(", %d)\n",root->rchild->data);
}
count++;
}
preorder(root->rchild);
}
int main(){
NODE *root=new NODE;
root->lchild=root->rchild=NULL;
int a;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a);
input(root,a);
}
preorder(root);
return 0;
}
标签:return str 左右子树 name font size image 二叉查找树 main
原文地址:http://www.cnblogs.com/Q1143316492/p/6260897.html