ZBLOG

实现二叉树类的设计,要求具有的功能:1)根据二叉树括号表示法根据字符串str生成对应的二叉链存储结构b,二叉树括号表示串中只有4类字符:‘(‘、‘)’,‘,’和A-Z字母,如"A(B(D(,G)),C(E,F))",一般在构造函数中实现;2)销毁二叉链存储结构Release():销毁二叉链b并释放空间,一般在析构函数中实现;3)查找结点Find(x):在二叉树b中寻找data域值为x的结点,并返回指向该结点的指针;4)求高度Height():求二叉树b的高度;5)(选做)设置结点的其他数据...

以下是一个基于题目要求的二叉树类的设计和实现示例:

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

template<typename T>
struct BinTreeNode {
    T data;
    T data2;
    BinTreeNode<T> *leftChild, *rightChild;
};

template<typename T>
class BinaryTree {
private:
    BinTreeNode<T>* root;

public:
    BinaryTree(const string& str) {
        root = nullptr;
        stack<BinTreeNode<T>*> s; // 辅助栈用于构建二叉树
        int i = 0;
        while (i < str.size()) {
            if (str[i] == '(') {
                i++;
            } else if (str[i] == ')') {
                s.pop();
                i++;
            } else if (str[i] == ',') {
                i++;
            } else { // 字母节点
                BinTreeNode<T>* newNode = new BinTreeNode<T>;
                newNode->data = str[i];
                newNode->leftChild = newNode->rightChild = nullptr;
                
                if (!root) { // 如果根节点为空,当前节点即为根节点
                    root = newNode;
                } else { // 否则,将当前节点连接到父节点上
                    if (!s.top()->leftChild)
                        s.top()->leftChild = newNode;
                    else
                        s.top()->rightChild = newNode;
                }
                
                s.push(newNode);
                i++;
            }
        }
    }

    ~BinaryTree() {
        Release(root);
    }

    void Release(BinTreeNode<T>* node) {
        if (!node)
            return;

        Release(node->leftChild);
        Release(node->rightChild);
        
        delete node;
    }

    BinTreeNode<T>* FindNode(const T& x) {
        return Find(root, x);
    }

    BinTreeNode<T>* Find(BinTreeNode<T>* node, const T& x) {
        if (!node)
            return nullptr;
        
        if (node->data == x)
            return node;

        BinTreeNode<T>* leftResult = Find(node->leftChild, x);
        BinTreeNode<T>* rightResult = Find(node->rightChild, x);

        return leftResult ? leftResult : rightResult;
    }

    int Height() {
        return GetHeight(root);
    }

    int GetHeight(BinTreeNode<T>* node) {
        if (!node)
            return 0;

        int leftHeight = GetHeight(node->leftChild);
        int rightHeight = GetHeight(node->rightChild);

        return max(leftHeight, rightHeight) + 1;
    }

    void SetNode(const T& x, const T& y) {
        SetData2(root, x, y);
    }

    void SetData2(BinTreeNode<T>* node, const T& x, const T& y) {
        if (!node)
            return;

        if (node->data == x)
            node->data2 = y;

        SetData2(node->leftChild, x, y);
        SetData2(node->rightChild, x, y);
    }

    void Disp() {
       PrintTree(root);
       cout << endl;
   }
   
   void PrintTree(BinTreeNode<T> *root){
      if(root==nullptr){
         cout<<"";
      }else{
         cout<<"<"<<root->data<<","<<root->data2<<">";
         cout<<"(";
         PrintTree(root->leftChild);
         cout<<",";
         PrintTree(root->rightChild);
         cout<<")";
      }
   }
};

int main()
{  
    BinaryTree<char> bt("A(B(D(,G)),C(E,F))");
    BinTreeNode<char>* p;
 
    cout << "b的高度: " << bt.Height() << endl;
    
    p = bt.FindNode('F');
    if (p != nullptr)
        cout << "b中存在F结点" << endl;
    else
        cout << "b中不存在F结点" << endl;

    bt.SetNode('A', 1);
    bt.SetNode('B', 1);
    bt.SetNode('D', 3);

    bt.Disp();
   
   return 0;
}

请注意,以上示例代码是基于题目要求提供的功能设计的,可能不包含所有异常处理和完整的边界情况处理。在实际使用时,请根据需要进行适当修改和优化。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9560

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?