zoukankan      html  css  js  c++  java
  • 二叉排序树(王道)

    #include <iostream>
    #include<cstdio>
    using namespace std;
    
    struct Nod
    e{
        Node* lchild;
        Node* rchild;
        int num;
    };
    
    struct Node Tree[50];
    int loc;
    Node *create(){//创建
        Tree[loc].lchild = Tree[loc].rchild = NULL;
        return &Tree[loc++];
    }
    
    void preOrder(Node* T){//前序遍历
        printf("%d",T->num);
        if(T->lchild != NULL)
            preOrder(T->lchild);
        if(T->rchild != NULL)
            preOrder(T->rchild);
    }
    
    void inOrder(Node* T){//中序遍历
        if(T->lchild != NULL)
            inOrder(T->lchild);
        printf("%d",T->num);
        if(T->rchild != NULL)
            inOrder(T->rchild);
    }
    
    void postOrder(Node* T){//后序遍历
        if(T->lchild != NULL)
            postOrder(T->lchild);
        if(T->rchild != NULL)
            postOrder(T->rchild);
        printf("%d",T->num);
    }
    
    Node *insertNode(Node *T,int x){//插入数字
        if(T == NULL){
            T = create();
            T->num = x;
        }
        else if(x < T->num)
            T->lchild = insertNode(T->lchild,x);
        else if(x > T->num)
            T->rchild = insertNode(T->rchild,x);
        return T;
    }
    
    int main()
    {
        int n;
        cin >> n;
        while(n--){
            int n2;
            cin >> n2;
            Node *T = NULL;
            loc = 0;
            for(int i=0;i<n2;i++){
                int x = 0;
                cin >> x;
                T = insertNode(T,x);
            }
            preOrder(T);
            cout << endl;
            inOrder(T);
            cout << endl;
            postOrder(T);
            cout << endl;
        }
        return 0;
    }
  • 相关阅读:
    Git 从入门到入坑
    单件模式(单例模式)
    装饰者模式
    观察者模式
    设计模式入门
    SpringBoot + Mybatis + Redis 整合入门项目
    Spring Boot 前后端交互及参数传递
    Spring Securtiy 认证流程(源码分析)
    Spring Boot 静态页面跳转
    第一条手机验证码
  • 原文地址:https://www.cnblogs.com/xym4869/p/8548567.html
Copyright © 2011-2022 走看看