struct node{
char data;
node *lchild;
node *rchild;
};
node* newnode(char v){
node* Node=new node;
Node->data=v;
Node->lchild=Node->rchild=NULL;
return Node;
}
void search(node* root,char x,char newdata){
if(root==NULL){
return;
}
if(root->data==x){
root->data=newdata;
}
search(root->lchild,x,newdata);
search(root->rchild,x,newdata);
}
void insert(node* &root,char x){
if(root==NULL){
root=newnode(x);
return;
}
if(x<=root->data)
insert(root->lchild,x);
else
insert(root->rchild,x);
}
node* create(char data[],int n){
node* root=NULL;
for(int i=0;i<n;i++){
insert(root,data[i]);
}
return root;
}
void preorder(node* root){
if(root==NULL){
return;
}
printf("%c",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
void inorder(node* root){
if(root==NULL){
return;
}
inorder(root->lchild);
printf("%c",root->data);
inorder(root->rchild);
}
void posorder(node* root){
if(root==NULL){
return;
}
posorder(root->lchild);
posorder(root->rchild);
printf("%c",root->data);
}
int main(){
char s[1000];
gets(s);
node* t=new node;
t=create(s,strlen(s));
preorder(t);
printf("\n");
inorder(t);
printf("\n");
posorder(t);
printf("\n");
char in;
scanf("%c",&in);
insert(t,in);
preorder(t);
printf("\n");
inorder(t);
printf("\n");
posorder(t);
return 0;
}
二叉树基操
最新推荐文章于 2022-05-22 22:51:37 发布