#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Tree tree;
typedef struct Queue queue;
typedef struct Stack stack;
struct Tree
{
int value;
struct Tree* left;
struct Tree* right;
};
struct treenode
{
struct Tree* node;
struct treenode* next;
};
struct Queue
{
struct treenode* front;
struct treenode* rear;
};
struct Stack
{
struct Tree* node;
struct Stack* next;
};
void push(stack** s,tree* leaf)
{
stack* newnode=new stack;
newnode->next=*s;
newnode->node=leaf;
*s=newnode;
}
tree* pop(stack** s)
{
stack* temp=*s;
*s=(*s)->next;//位置不能放错
tree* leaf=temp->node;
delete(temp);
return leaf;
}
void enqueue(queue* q,tree* leaf)
{
struct treenode* newnode=new struct treenode;
newnode->node=leaf;
newnode->next=NULL;
if(q->rear)
q->rear->next=newnode;
q->rear=newnode;
if(!q->front)
q->front=newnode;
}
tree* dequeue(queue* q)
{
tree* leaf=q->front->node;
treenode* temp=q->front;
q->front=q->front->next;
//free(temp);???为什么free(temp)放这里不行
return leaf;
}
void menu(void)
{
printf("menu:\n");
printf("1.initialize\n");
printf("2.insertatrandom\n");
printf("3.preorder\n");
printf("4.levelorder\n");
printf("5.inorder\n");
printf("6.postorder\n");
printf("7.preorderbystack\n");
}
int Choice(void)
{
int choice;
printf("please input your choice:");
scanf("%d",&choice);
return choice;
}
tree* initialize(void)
{
return NULL;
}
tree* insertatrandom(tree* Root,int data)
{
if(!Root)
{
Root=(tree*)malloc(sizeof(tree));
Root->value=data;
Root->left=Root->right=NULL;
}else
{
if(!Root->left)
Root->left=insertatrandom(Root->left,data);
else
Root->right=insertatrandom(Root->right,data);
/*tree* root=Root;
if(!root->left)
root->left=insertatrandom(root->left,data);
else
root->right=insertatrandom(root->right,data);
return root;*/
}
return Root;
}
void preorder(tree* Root)
{
if(Root)
{
printf("%d ",Root->value);
preorder(Root->left);
preorder(Root->right);
}
/*tree* root=Root;
if(root)
{
printf("%d ",root->value);
preorder(root->left);
preorder(root->right);
}*/
}
int isempty(queue* q)
{
return (q->front==NULL);
}
void levelorder(tree* Root)
{
tree* temp=Root;
queue* q=new queue;
q->front=NULL;
q->rear=NULL;
enqueue(q,temp);//辅助队列
while(!isempty(q))
{
temp=dequeue(q);
printf("%d ",temp->value);
if(temp->left!=NULL)
enqueue(q,temp->left);
if(temp->right!=NULL)
enqueue(q,temp->right);
}
delete(q);
}
void inorder(tree* Root)
{
if(Root)
{
inorder(Root->left);
printf("%d ",Root->value);
inorder(Root->right);
}
}
void postorder(tree* Root)
{
if(Root)
{
postorder(Root->left);
postorder(Root->right);
printf("%d ",Root->value);
}
}
void preorderbystack(tree* Root)
{
stack *s=new stack;
s=NULL;
tree* root=Root;
while(1)
{
while(root)
{
printf("%d ",root->value);
push(&s,root);
root=root->left;
}
if(!s)
break;
root=pop(&s);
root=root->right;
}
delete(s);
}
int main()
{
int flag=1,data;
tree *Root,*leaf;
menu();
while(flag)
{
switch(Choice())
{
case 1:
Root=initialize();
printf("initialize ok\n");
break;
case 2:{
printf("please input value:");
scanf("%d",&data);
Root=insertatrandom(Root,data);
break;
}
case 3:{
printf("the tree is:");
preorder(Root);
printf("\n");
break;
}
case 4:{
printf("the tree is:");
levelorder(Root);
printf("\n");
break;
}
case 5:{
printf("the tree is:");
inorder(Root);
printf("\n");
break;
}
case 6:{
printf("the tree is:");
postorder(Root);
printf("\n");
break;
}
case 7:{
printf("the tree is:");
preorderbystack(Root);
printf("\n");
break;
}
default:
flag=0;
break;
}
}
return 0;
}
树1.0(遍历,插入)
最新推荐文章于 2024-09-04 18:39:13 发布