Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

二叉查找樹全操作 C++ 實現:查詢、前驅後繼與結點刪除

本文是《算法導論》第 12 章偽代碼的 C++ 落地版本,在基礎插入之上补齐了查詢最值、查找、求前驅後繼與刪除結點等完整操作。

#include <iostream>
#include <vector>
using namespace std;

/*二叉查找樹結構*/
typedef struct BSTree
{
    int node_value;
    struct BSTree * left;
    struct BSTree * right;
    struct BSTree * parent;
} Tree;

Tree * root = NULL;

/*****構造二叉查找樹**********************************************/
void CreateBSTree(Tree * root, int node_value);
Tree * CreateBSTree(int * array_list, int array_length);
void Print(Tree* root);
/*****************************************************************/

int Minimum(Tree * p);
Tree * TreeMinimum(Tree * root);
int Maximum(Tree * p);
Tree * TreeMaximum(Tree * root);
Tree * FindNode(Tree * root, int node_value);
Tree * Successor(Tree * root);
Tree * PredeSuccessor(Tree * p);
bool DeleteTreeNode(Tree * root, int node_value);
bool DeleteTreeNode(Tree * n);
/*****************************************************************/

int main(int argc, char * argv[])
{
    //int list[] = {5,3,4,9,1,7,11};
    int list[] = {15, 5, 16, 3, 12, 20, 10, 13, 18, 23, 6, 7};
    root = CreateBSTree(list, 12);
    std::cout << "Cearte BSTree." << std::endl;
    //Print(root);
    //std::cout << Successor(FindNode(root, 4))->node_value;
    //DeleteTreeNode(root, 15);
    Tree * t = FindNode(root, 18);
    std::cout << PredeSuccessor(t)->node_value;
    getchar();
    return 0;
}

/*找出樹中的最小結點,p 為樹的根結點*/
int Minimum(Tree * p)
{
    Tree * t = TreeMinimum(p);
    if (t != NULL)
    {
        return t->node_value;
    }
    else
    {
        return -1;
    }
}

Tree * TreeMinimum(Tree * p)
{
    if (p->left == NULL)
    {
        return p;
    }
    TreeMinimum(p->left);
}

/*找出樹中的最大結點*/
int Maximum(Tree * p)
{
    Tree * t = TreeMaximum(root);
    if (t != NULL)
    {
        return t->node_value;
    }
    else
    {
        return -1;
    }
}

Tree * TreeMaximum(Tree * p)
{
    if (p->right == NULL)
    {
        return p;
    }
    TreeMaximum(p->right);
}

/*假定所有結點值都不相同,p 為樹的根結點,node_value 為要查找的值*/
Tree * FindNode(Tree * p, int node_value)
{
    if (p == NULL)
    {
        return NULL; /*遞歸返回標志*/
    }
    if (p->node_value == node_value)
    {
        return p;
    }
    if (p->node_value < node_value)
    {
        FindNode(p->right, node_value);
    }
    else
    {
        FindNode(p->left, node_value);
    }
}

/*找出一個結點的後繼結點*/
Tree * Successor(Tree * p)
{
    if (p == NULL)
    {
        return NULL;
    }
    if (p->right != NULL)
    {
        return TreeMinimum(p->right);
    }
    Tree * t = p->parent;
    while ((t != NULL) && (p == t->right))
    {
        p = t;
        t = t->parent;
    }
    return t;
}

/*找到一個結點的前驅結點,p 為結點的指針*/
Tree * PredeSuccessor(Tree * p)
{
    if (p == NULL)
    {
        return NULL;
    }
    else if (p->left != NULL)
    {/*左子樹不為空時,前驅即左子樹的最大值結點*/
        return TreeMaximum(p->left);
    }
    else
    {
        Tree * t = p->parent;
        while ((t != NULL) && (p == t->left))
        {/*注意 t 的方向,這一步與尋找後繼恰好相反*/
            p = t;
            t = t->parent;
        }
        return t;
    }
}

/*刪除一個結點,p 為樹根結點指針,node_value 為要刪除的值*/
bool DeleteTreeNode(Tree * p, int node_value)
{
    Tree * t = FindNode(p, node_value);
    if (t == NULL)
    {
        return false;
    }
    if ((t->left == NULL) && (t->right == NULL))
    {/*沒有子結點*/
        Tree* tmp = t;
        if (tmp->parent->left == tmp)
        {
            tmp->parent->left = NULL;
        }
        else
        {
            tmp->parent->right = NULL;
        }
        delete tmp;
        tmp = NULL;
    }
    else if ((t->left == NULL) || (t->right == NULL))
    {/*一個子結點*/
        Tree* tmp = t;
        if (tmp->parent->left == tmp)
        {
            tmp->parent->left = (tmp->left == NULL) ? tmp->right : tmp->left;
        }
        else
        {
            tmp->parent->right = (tmp->left == NULL) ? tmp->right : tmp->left;
        }
        delete tmp;
        tmp = NULL;
    }
    else
    {/*兩個子結點*/
        Tree* s = Successor(t);
        if (s == NULL)
        {
            return false;
        }
        t->node_value = s->node_value;
        DeleteTreeNode(s);
    }
}

/*刪除一個結點,n 為待刪結點的指針*/
bool DeleteTreeNode(Tree * n)
{
    if (n == NULL)
    {
        return NULL;
    }
    else if ((n->left == NULL) && (n->right == NULL))
    {/*沒有子結點*/
        Tree* tmp = n;
        if (tmp->parent->left == tmp)
        {
            tmp->parent->left = NULL;
        }
        else
        {
            tmp->parent->right = NULL;
        }
        delete tmp;
        tmp = NULL;
    }
    else if ((n->left == NULL) || (n->right == NULL))
    {/*一個子結點*/
        Tree* tmp = n;
        if (tmp->parent->left == tmp)
        {
            tmp->parent->left = (tmp->left == NULL) ? tmp->right : tmp->left;
        }
        else
        {
            tmp->parent->right = (tmp->left == NULL) ? tmp->right : tmp->left;
        }
        delete tmp;
        tmp = NULL;
    }
    else
    {/*兩個子結點*/
        Tree* s = Successor(n);
        if (s == NULL)
        {
            return false;
        }
        n->node_value = s->node_value;
        DeleteTreeNode(s);
    }
}

/*生成二叉查找樹*/
Tree * CreateBSTree(int * array_list, int array_length)
{
    if (array_length <= 0)
    {
        return false;
    }
    Tree * root = NULL;
    root = new BSTree();
    root->left = NULL;
    root->right = NULL;
    root->parent = NULL;
    root->node_value = array_list[0];
    for (int i = 1; i < array_length; i++)
    {
        CreateBSTree(root, array_list[i]);
    }
    return root;
}

void CreateBSTree(Tree * root, int node_value)
{
    if (root == NULL)
    {
        return;
    }
    if (root->node_value > node_value)
    {
        if (root->left == NULL)
        {
            Tree * node = new Tree();
            node->left = NULL;
            node->right = NULL;
            node->node_value = node_value;
            node->parent = root;
            root->left = node;
        }
        else
        {
            CreateBSTree(root->left, node_value);
        }
    }
    else
    {
        if (root->right == NULL)
        {
            Tree * node = new Tree();
            node->left = NULL;
            node->right = NULL;
            node->node_value = node_value;
            node->parent = root;
            root->right = node;
        }
        else
        {
            CreateBSTree(root->right, node_value);
        }
    }
}

/*中序排序輸出二叉查找樹*/
void Print(Tree* root)
{
    if (root == NULL)
    {
        return;
    }
    Print(root->left);
    std::cout << root->node_value << "\t";
    Print(root->right);
}

幾個關鍵設計

相比基礎版本,這裡的結點多了一個 parent 指針,是為了讓 Successor 與 PredeSuccessor 能沿著祖先鏈回溯——沒有它,求後繼時無從向上走。求後繼的邏輯是:有右子樹就取右子樹最小值,否則一路向上,直到離開某個「作為左孩子」的祖先。求前驅完全鏡像:有左子樹取左子樹最大值,否則向上找到第一個「作為右孩子」的祖先。

刪除分三種情況:無子結點直接摘除;僅一個子結點則讓子結點頂替自己的位置;有兩個子結點時,用後繼的值覆蓋自己,再轉去刪除那個後繼——後繼必然沒有左子樹,於是問題退化為前兩種情況。這是整份代碼最精妙的一步。

需要提醒的是,TreeMinimum 與 TreeMaximum 的遞歸分支缺少 return,FindNode 的遞歸調用也沒有把結果返回,這幾處在現代編譯器下會告警甚至導致邏輯錯誤,實戰中應補成 return TreeMinimum(p->left); 與 return FindNode(...);。

Copyright © Linux教程網 All Rights Reserved