zoukankan      html  css  js  c++  java
  • 二叉搜索树的例子BST

    #ifndef ST_CLASS
    #define ST_CLASS
    
    #include <iostream>
    
    using namespace std;
    
    class ST
    {
        public:
            ST(int maxN)
            { head=0;}
            int search(int v)
            {
                return searchR(head, v);
            }
    
            int insert(int x)
            {
                insertR(head, x);
            }
    
            void show()
            {
                showR(head);
            }
    
        private:
            struct node
            {
                int item;
                node *l, *r;
                node (int x)
                {
                    item = x;
                    l = 0;
                    r = 0;
                }
            };
    
            typedef node *link;
            link head;
            int nullItem;
    
            int searchR(link h, int v)
            {
                if(h==0) return nullItem;
                int t = h->item;
                if(v == t) return h->item;
                if(v < t)
                    return searchR(h->l, v);
                else
                    return searchR(h->r, v);
            }
    
            void insertR(link& h, int x)
            {
                if(h==0)
                {
                    h = new node(x);
                    return ;
                }
                if(x < h->item)
                    insertR(h->l, x);
                else
                    insertR(h->r, x);
            }
    
            void showR(link h)
            {
                if(h==0) return;
                showR(h->l);
                cout << h->item << " ";
                showR(h->r);
            }
    
    };
    #endif
  • 相关阅读:
    软件测试课堂练习1
    安卓增删改查
    安卓数据库表
    安卓注册登录
    安卓购物清单
    安卓计算器
    第四周安卓作业
    第七周作业
    jsp第六周
    第四次jsp作业
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2767658.html
Copyright © 2011-2022 走看看