zoukankan      html  css  js  c++  java
  • 完全二叉树模板

    #include<iostream>
    using namespace std;
    template <class T>
    class CompleteBinaryTree{ //完全二叉树,所有结点从0~n-1编号
        //对于结点i,它的左右子树分别位于2i+1和2i+2,它的父结点为[(i-1)/2]
        //除根节点外,所有奇数下标结点为其父节点左子树,偶数下标为右子树
        //对于奇数2k-1,其右兄弟为2k;对于偶数2k其左兄弟为2k-1
    private:
        T *Node;
        int maxSize;
        int Size;
    public:
        CompleteBinaryTree(int s)
        {
            maxSize=s;
            Size=0;
            Node=new T[maxSize];
        }
        void clearTree()
        {
            delete Node;
            Size=0;
            Node=new T[maxSize];
        }
        int Find(const T value)
        {
            for(int i=0;i<Size;i++)
            {
                if(Node[i]==value)
                    return i;
            }
            cout << "non-exist" <<endl;
            return 0;
        }
        T left(int i,int &p) //返回i结点的左子树和左子树值
        {
            if(i>=0&&(2*i+1)<Size)
            {
                p=2*i+1;
                return Node[2*i+1];
            }
            cout <<"illegal position"<<endl;
            return NULL;
        }
        T left(int i)
        {
            if(i>=0&&(2*i+1)<Size) return Node[2*i+1];
            cout <<"illegal position"<<endl;
            return NULL;
        }
        T right(int i,int &p) //返回i结点的右子树和右子树值
        {
            if(i>=0&&(2*i+2)<Size)
            {
                p=2*i+2;
                return Node[2*i+2];
            }
            cout <<"illegal position"<<endl;
            return NULL;
        }
        T right(int i)
        {
            if(i>=0&&(2*i+2)<Size)return Node[2*i+2];
            cout <<"illegal position"<<endl;
            return NULL;
        }
        T parent(int i,int &p)
        {
            if(i>0&&i<Size)
            {
                p=(i-1)/2;
                return Node[p];
            }
            cout <<"illegal position"<<endl;
            return NULL;
        }
        T parent(int i)
        {
            if(i>0&&i<Size)
                return Node[(i-1)/2];
            cout <<"illegal position"<<endl;
            return NULL;
        }
        bool insert(const T value) //将value插入末尾
        {
            if((Size+1)<=maxSize)
            {
                Node[Size++]=value;
                return true;
            }
            return false;
        }
        bool insert(const T value,int i) //将value插入下标为i的结点,原结点放在末尾
        {
            if(i<0||i>Size)return false;
            if((Size+1)<=maxSize)
            {
                Node[Size]=Node[i];
                Node[i]=value;
                Size++;
                return true;
            }
            return false;
        }
        void display() //广搜遍历
        {
            cout << "Size:" << Size <<endl;
            cout << "Max size:" << maxSize <<endl;
            for(int i=0;i<Size;i++)
            {
                cout << Node[i] << " ";
            }
            cout <<endl;
        }
        const T operator[](int x)
        {
            if(x>=0&&x<Size)
                return Node[x];
            cout <<"illegal position"<<endl;
            return NULL;
        }
    };
    int main()
    {
        CompleteBinaryTree<int> t(100);
        int a;
        for(int i=0;i<10;i++)t.insert(i);
        cout << "f"<<t.Find(5)<<endl;
        t.display();
        cout << t.left(-1)<<endl;
        cout << t[1] <<endl;
        cout << t.right(2) <<endl;
        return 0;
    }

  • 相关阅读:
    基于脚本的nodemanager管理器
    SSH 等效性问题 总提示输入密码问题
    增量检查点【概念】
    【ORA错误大全】 ORA-19527
    DataGuard 配置须知
    rhel5.4+oracle 10g rac
    microg,google新推的一个计划
    [转]Android ListView 与 RecyclerView 对比浅析—缓存机制
    android studio的Beta, Canary, Dev, Stable四种Channel版本介绍、分析与选择
    android studio增量更新
  • 原文地址:https://www.cnblogs.com/LowBee/p/9023910.html
Copyright © 2011-2022 走看看