原始树的子树

发布于 2024-12-19 12:35:12 字数 2903 浏览 1 评论 0原文

当我尝试执行以下操作时遇到问题: (1) 找到原树根的左子树中最大的info字段 (2)找到原树根的右子树中最小的info字段。

我的代码可以编译,但执行时会出现错误,并且我不清楚 maxleftsubtree() 和 minrightsubtree() 函数中发生了什么。任何建议将不胜感激。

我当前的代码:

#include <iostream>
#include <string>

using namespace std;

class Tnode {
    public:
        Tnode *left;
        string info;
        Tnode *right;
        Tnode(string info = "", Tnode* left = NULL, Tnode* right = NULL) :
            info(info), left(left), right(right) {}
};
class BST {
    public:
        BST() : theroot(NULL) {}
        void insert(string x);
        void inorderprint();
        void preorderprint();
        void postorderprint();
        void maxstring();
        void minstring();
        void maxleftsubtree();
        void minrightsubtree();
    private:
        void inorderprint(Tnode *p);
        void preorderprint(Tnode *p);
        void postorderprint(Tnode *p);
        void maxstring(Tnode *p);
        void minstring(Tnode *p);
        void maxleftsubtree(Tnode *p);
        void minrightsubtree(Tnode *p);
        void insertleft(Tnode *place, string newval);
        void insertright(Tnode *place, string newval);
        Tnode *theroot;
};

// add a new node (with x as info) to tree that has theroot as root
void BST::insert(string x)
{
    // if the tree is initially empty, put x at the root
    if (theroot==NULL) {
        theroot = new Tnode(x);
        return;
    }

    Tnode *p, *q;

    // otherwise, find where x belongs in the tree
    p = theroot;
    q = theroot;
    while ( q != NULL) {
        p = q;
        if (x < p-> info)
            q = p-> left;
        else
            q = p-> right;
    }

    // to get here, we found the correct place to store x,
    // as a child of node p        Q: is it left or right?

    if (x < p-> info)
        insertleft(p,x);
    else
        insertright(p,x);

    return;

}

//insert a new node (with info newval) as left child of place
void BST::insertleft(Tnode *place, string newval)
{
    Tnode *p = new Tnode(newval);

    place -> left = p;
    return;

}

//insert a new node (with info newval) as right child of place
void BST::insertright(Tnode *place, string newval)
{
    Tnode *p = new Tnode(newval);

    place -> right = p;
    return;

}
......................
...............
...............

//
//
void BST::maxleftsubtree()
{
    maxleftsubtree(theroot);
}

//
//
void BST::minrightsubtree()
{
    minrightsubtree(theroot);
}

.....................................
.................................
.........................

//
//
void BST::maxleftsubtree(Tnode *p)
{
    while (p -> left)
        p = p -> right;
    cout << p -> info << " \n";
    return;
}

//
//
void BST::minrightsubtree(Tnode *p)
{
    while (p -> right)
        p = p -> left;
    cout << p -> info << " \n";
    return;
}

I am having problems when I am attempting to :
(1) find the largest info field in the left sub-tree of the root of the original tree
(2) find the smallest info field in the right sub-tree of the root of the original tree.

My code compiles but then it has errors when it executes and I'm unclear of what is happening in my maxleftsubtree() and minrightsubtree() functions. Any suggestions would be appreciated.

My current code:

#include <iostream>
#include <string>

using namespace std;

class Tnode {
    public:
        Tnode *left;
        string info;
        Tnode *right;
        Tnode(string info = "", Tnode* left = NULL, Tnode* right = NULL) :
            info(info), left(left), right(right) {}
};
class BST {
    public:
        BST() : theroot(NULL) {}
        void insert(string x);
        void inorderprint();
        void preorderprint();
        void postorderprint();
        void maxstring();
        void minstring();
        void maxleftsubtree();
        void minrightsubtree();
    private:
        void inorderprint(Tnode *p);
        void preorderprint(Tnode *p);
        void postorderprint(Tnode *p);
        void maxstring(Tnode *p);
        void minstring(Tnode *p);
        void maxleftsubtree(Tnode *p);
        void minrightsubtree(Tnode *p);
        void insertleft(Tnode *place, string newval);
        void insertright(Tnode *place, string newval);
        Tnode *theroot;
};

// add a new node (with x as info) to tree that has theroot as root
void BST::insert(string x)
{
    // if the tree is initially empty, put x at the root
    if (theroot==NULL) {
        theroot = new Tnode(x);
        return;
    }

    Tnode *p, *q;

    // otherwise, find where x belongs in the tree
    p = theroot;
    q = theroot;
    while ( q != NULL) {
        p = q;
        if (x < p-> info)
            q = p-> left;
        else
            q = p-> right;
    }

    // to get here, we found the correct place to store x,
    // as a child of node p        Q: is it left or right?

    if (x < p-> info)
        insertleft(p,x);
    else
        insertright(p,x);

    return;

}

//insert a new node (with info newval) as left child of place
void BST::insertleft(Tnode *place, string newval)
{
    Tnode *p = new Tnode(newval);

    place -> left = p;
    return;

}

//insert a new node (with info newval) as right child of place
void BST::insertright(Tnode *place, string newval)
{
    Tnode *p = new Tnode(newval);

    place -> right = p;
    return;

}
......................
...............
...............

//
//
void BST::maxleftsubtree()
{
    maxleftsubtree(theroot);
}

//
//
void BST::minrightsubtree()
{
    minrightsubtree(theroot);
}

.....................................
.................................
.........................

//
//
void BST::maxleftsubtree(Tnode *p)
{
    while (p -> left)
        p = p -> right;
    cout << p -> info << " \n";
    return;
}

//
//
void BST::minrightsubtree(Tnode *p)
{
    while (p -> right)
        p = p -> left;
    cout << p -> info << " \n";
    return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-12-26 12:35:12

您的 maxleftsubtree(maxtrightsubtree) 函数存在错误。首先选择根的左(右)子树,沿着右(左)分支走到终点。这是修改后的版本:

void BST::maxleftsubtree(Tnode *p)
{
    Tnode* left = NULL;
    if (p != NULL) {
        left = p->left;
    }
    if (left != NULL) {
        while (left->right) 
            left = left -> right;
        cout << left -> info << " \n";
    }
    return;
}

There is an error in your maxleftsubtree(maxtrightsubtree) function. You should first select the left(right) subtree of the root, the walk along the right(left) branch to the end. Here is a modified version:

void BST::maxleftsubtree(Tnode *p)
{
    Tnode* left = NULL;
    if (p != NULL) {
        left = p->left;
    }
    if (left != NULL) {
        while (left->right) 
            left = left -> right;
        cout << left -> info << " \n";
    }
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文