二叉搜索树的例子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
原文地址:https://www.cnblogs.com/wouldguan/p/2767658.html