二叉树学习

来源:互联网 发布:手机安装ubuntu arm 编辑:程序博客网 时间:2024/05/22 20:28

二叉树的遍历:

前序:根 左 右(前根)

中序:左根右(中根)

后序: 左右根(后根)


二叉查找树:

在二叉树之中涉及到一个递归概念(当递归有返回值的时候我们必须带回的值)


#include <iostream>
using namespace std;

typedef int T;


class BinaryTree
{    
public:

    struct Node
    {
        T m_Data;
        Node* m_Left;
        Node* m_Right;
        Node(const T& d):m_Data(d), m_Left(), m_Right()
        {

        }
        Node(const T& d, Node* l, Node* r):m_Data(d), m_Right(r), m_Left(l)
        {

        }
    };
    typedef Node* Tree;
    ~BinaryTree()
    {
        Clear();
    }

    //////////////////////////////////////////////////////////////////////////
    void Clear()
    {
        Clear(m_Root);
    }

    void Insert(const T& data)
    {
        Insert(m_Root, new Node(data));
        ++m_Size;
    }

    Tree& Find(const T& data)
    {
        return Find(m_Root, data);
    }

     void Travel() const
     {
         Travel(m_Root);
         cout << endl;
     }

    bool Empty() const
    {
        return m_Root == NULL;
    }
    
    int Size() const
    {
        return m_Size;
    }

    bool  Remove(const T& d)
    {
        Tree& t = Find(d);//希望原始变量必须使用引用
        if ( NULL == t)
        {
            return false;
        }
        Node* p = t;
        if (t->m_Left)
        {
            Insert(t->m_Right, t->m_Left);
        }
        t = t->m_Right;
        delete p;
        --m_Size;
        return true;
    }

    void update(const T& oldData, const T& NewData)
    {
          if (Remove(oldData))
          {
              Insert(NewData);
          }
    }
    /////////////////////////////////////////////////////////////////////////

    BinaryTree():m_Root(), m_Size()
    {

    }
private:
    void Insert(Tree& t, Node* p)
    {
        if (NULL == t)
        {
            t = p;
        }
        else if (t->m_Data > p->m_Data)
        {
            Insert(t->m_Left, p);
        }
        else
        {
            Insert(t->m_Right, p);
        }
    }



    Tree& Find(Tree& t, const T& d)      //返回以d为根的子树的根指针
    {
        if (t == NULL)
        {
            return t;                
        }
        else if (t->m_Data == d)
        {
            return t;

        }
        else if (t->m_Data > d)
        {
            return Find(t->m_Left, d);       //递归函数的返回值不为空,必须返回递归的值
        }
        else
        {
            return Find(t->m_Right, d);
        }

    }

    void Travel(Tree t)     const
    {
        if (t != NULL)
        {
            Travel(t->m_Left);
            cout << t-> m_Data << "   ";
            Travel(t->m_Right);
        }
    }

    void Clear(Tree& t)
    {
        if (t != NULL)
        {
            Clear(t->m_Left);
            Clear(t->m_Right);
            delete t;
            t = NULL;
        }
    }

    int Hight(Tree t)
    {
        if (t == NULL)
        {
            return 0;
        }
        int Left = 0;
        Left = Hight(t->m_Left);
        int Right = 0;
        Right = Hight(t->m_Right);
        return 1+ max(Left, Right);
    }

    Node* m_Root;
    int m_Size;
};

int main()
{
    int a[]    = {10, 1 ,12, 1, 5, 1,6, 14, 29, 2, 54,  6};
    BinaryTree BT;
    for (int i = 0; i < 12; ++i)
    {
        BT.Insert(a[i]);
    }
    BT.Travel();
    BT.Remove(10);
    BT.Travel();
    BT.update(12, 100);
    BT.Travel();
    system("pause");
    
    return 0;
}

原创粉丝点击