C++:二叉树

 

代码

class binaryTree
{

public:
    struct binaryNode
    {
        int data;
        binaryNode* preNode;
        binaryNode* left;
        binaryNode* right;
        binaryNode() :preNode(nullptr),left(nullptr), right(nullptr){}
    };

    binaryNode * root;

public:

    binaryTree()
    {
        cnt = 0;
        root = nullptr;
    }
    void addData(int t_data)
    {
        if (cnt)
        {
            if (findData(t_data) != nullptr)
                cout << "已经有这个节点了" << endl;
            else
            {
                binaryNode* ptr = root;
                while (true)
                {
                    if (t_data < ptr->data)
                    {
                        if (ptr->left != nullptr)
                            ptr = ptr->left;
                        else
                        {
                            binaryNode* new_ = new binaryNode;
                            new_->data = t_data;
                            ptr->left = new_;
                            new_->preNode = ptr;
                            ++cnt;
                            return;
                        }
                    }
                    else
                    {
                        if (ptr->right != nullptr)
                            ptr = ptr->right;
                        else
                        {
                            binaryNode* new_ = new binaryNode;
                            new_->data = t_data;
                            ptr->right = new_;
                            new_->preNode = ptr;
                            ++cnt;
                            return;
                        }
                    }
                }
            }
        }
        else
        {
            root = new binaryNode;
            root->data = t_data;
            ++cnt;
        }
    }
    bool deleteData(int t_key) // 提升左边
    {
        binaryNode * ptr = findData(t_key);
        if (ptr == nullptr)
        {
            cout << "没有这个值" << endl;
            return false;
        }
        else
        {
            binaryNode * t_ptr = ptr;
            t_ptr = ptr->left;
            if (t_ptr == nullptr)
            {
                if (ptr->preNode != nullptr)
                {
                    if (ptr->data < ptr->preNode->data)
                        ptr->preNode->left = ptr->right;
                    else
                        ptr->preNode->right = ptr->right;
                }
                ptr->right->preNode = ptr->preNode;
                return true;
            }
            if (t_ptr->right == nullptr)
            {
                t_ptr->preNode = ptr->preNode;
                if (t_ptr->preNode != nullptr)
                {
                    if (t_ptr->data < t_ptr->preNode->data)
                        t_ptr->preNode->left = t_ptr;
                    else
                        t_ptr->preNode->right = t_ptr;
                }
                return true;
            }

            while (t_ptr->right!=nullptr)
            {
                t_ptr = t_ptr->right;
            }
            if (t_ptr->left != nullptr)
                t_ptr->preNode->right = t_ptr->left;
            else
            {
                t_ptr->preNode->right = nullptr;

                t_ptr->right = ptr->right;
                t_ptr->left = ptr->left;
                t_ptr->preNode = ptr->preNode;
                if (t_ptr->preNode != nullptr)
                {
                    if (t_ptr->data < t_ptr->preNode->data)
                        t_ptr->preNode->left = t_ptr;
                    else
                        t_ptr->preNode->right = t_ptr;
                }
            }
            return true;
        }
    }
    binaryNode* findData(int t_key)
    {
        binaryNode* ptr = root;
        while (true)
        {
            if (ptr == nullptr)
                return nullptr;
            if (ptr->data == t_key)
                return ptr;
            else if (ptr->data > t_key)
            {
                ptr = ptr->left;
            }
            else
                ptr = ptr->right;
        }
    }
    void preoder(const binaryNode * ptr) // 前序遍历 
    {
        if (ptr != nullptr)
        {
            cout << ptr->data << endl;
            preoder(ptr->left);
            preoder(ptr->right);
        }
    }
    void inorder(const binaryNode * ptr) // 中序遍历
    {
        if (ptr != nullptr)
        {
            inorder(ptr->left);
            cout << ptr->data << endl;
            inorder(ptr->right);
        }
    }
    void postorder(const binaryNode * ptr) // 后序遍历
    {
        if (ptr != nullptr)
        {
            postorder(ptr->left);
            postorder(ptr->right);
            cout << ptr->data << endl;
        }
    }
private:
    size_t cnt;
};

 

posted @ 2017-04-09 23:02  whlook  阅读(220)  评论(0)    收藏  举报