树2.0

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)
#define minum -1000
#define maxum 1000
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.levelorder    层次遍历\n");
    printf("4.preorder      先序遍历\n");
    printf("5.inorder       中序遍历\n");
    printf("6.postorder     后序遍历\n");
    printf("7.preorderbystack\n");
    printf("8.insertinorder\n");
    printf("9.findmax\n");
    printf("10.findmin\n");
    printf("11.produceatree\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);
}
tree* insertinorder(tree* Root,int data)
{
    if(!Root)
    {
        tree* leaf=(tree*)malloc(sizeof(tree));
        leaf->left=NULL;
        leaf->right=NULL;
        leaf->value=data;
        Root=leaf;
    }else if(data<=Root->value)
        Root->left=insertinorder(Root->left,data);
    else
        Root->right=insertinorder(Root->right,data);
    return Root;
}
int findmax(tree* Root)//假设节点值均为非负整数
{
    int leftmax,rightmax,totalmax;
    if(!Root)
        return minum;
    leftmax=findmax(Root->left);
    rightmax=findmax(Root->right);
    totalmax=max(max(leftmax,rightmax),Root->value);
    return totalmax;
}
int findmin(tree* Root)
{
    int leftmin,rightmin,totalmin;
    if(!Root)
        return maxum;
    leftmin=findmin(Root->left);
    rightmin=findmin(Root->right);
    totalmin=min(min(leftmin,rightmin),Root->value);
    return totalmin;
}
tree* produceatree(tree* Root,int n,int range)
{
    srand(time(NULL));
    for(int i=0;i<n;i++)
    {
        int number=rand()%range;
        Root=insertatrandom(Root,number);
    }
    return Root;
}
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:");
                levelorder(Root);
                printf("\n");
                break;
            }
            case 4:{
                printf("the tree is:");
                preorder(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;
            }
            case 8:{
                printf("please input value:");
                scanf("%d",&data);
                Root=insertinorder(Root,data);
                break;
            }
            case 9:{
                printf("max is:%d\n",findmax(Root));
                break;
            }
            case 10:{
                printf("min is:%d\n",findmin(Root));
                break;
            }
            case 11:{
                int n,range;
                printf("please input the size and the range of the tree:");
                scanf("%d%d",&n,&range);
                Root=produceatree(Root,n,range);
                break;
            }
            default:
                flag=0;
                break;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值