zoukankan      html  css  js  c++  java
  • 二叉排序树

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #define clc(a, b) memset(a, b, sizeof(a))
    #define LL long long
    using namespace std;
    
    const int inf = 0x3f;
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e6+5;
    
    struct Node
    {
        int data, flag;
        Node *lchild, *rchild;
    }node, tree;
    
    bool Search(Node *T, int key)
    {
        Node *p = T;
        while(p)
        {
            if(p->data == key) return true;
            if(key < p->data)
            {
                printf("E");
                p = p->lchild;
            }
            else
            {
                printf("W");
                p = p->rchild;
            }
        }
        return false;
    }
    Node* Insert(Node *T, int key)
    {
        Node *f = T, *p = T;
        while(p)
        {
            if(p->data == key) return T;
            f = p;
            p = (key < p->data) ? p->lchild : p->rchild;
        }
        p = (Node*)malloc(sizeof(Node));
        p->data = key;
        p->lchild = p->rchild = NULL;
        if(T == NULL)
        {
            T = p;
        }
        else if(key < f->data)
        {
            f->lchild = p;
        }
        else
            f->rchild = p;
        return T;
    }
    void Des(Node *T)
    {
        if(T)
        {
            if(T->lchild)
                Des(T->lchild);
            if(T->rchild)
                Des(T->rchild);
            free(T);
            T = NULL;
        }
    }
    int main()
    {
        int tt, n, q, x;
        scanf("%d", &tt);
        Node *tree = NULL;
        while(tt--)
        {
            tree = NULL;
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
            {
                scanf("%d", &x);
                tree = Insert(tree, x);
            }
            scanf("%d", &q);
            while(q--)
            {
                scanf("%d", &x);
                Search(tree, x);
                puts("");
            }
            //Des(tree);
        }
        return 0;
    }
  • 相关阅读:
    Calendar来进行一个时间段内每天的判断
    关于PreparedStatement的setDate的了解
    git学习--创建标签
    git学习--bug分支
    hibernate(1)
    JavaScript(三)---- 控制流程语句
    JavaScript(二)---- 变量、数据类型和运算符
    JavaScript(一)---- 概述
    css(四)-- 盒子模型和定位
    css(三)-- 常用属性
  • 原文地址:https://www.cnblogs.com/wmxl/p/4804603.html
Copyright © 2011-2022 走看看