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;
    }

  • 相关阅读:
    Fix Installing .NET Framework 3.5 failed Error Code 0x800F0954 on Windows 10
    RHEL8安装五笔输入法
    Enable EPEL and Local Repository on RHEL8
    Why is Yum Replaced by DNF?
    检查Linux服务器是否被攻击的常用命令及方法
    IDEA 主题
    IDEA 如何显示一个类中所有的方法
    Appium 安装以及安装过程中遇到的问题
    Maven 如何发布 jar 包到 Nexus 私库
    java泛型的基本使用
  • 原文地址:https://www.cnblogs.com/LowBee/p/9023910.html
Copyright © 2011-2022 走看看