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)(选做)设置结点的其他数据...

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

  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. template<typename T>
  5. struct BinTreeNode {
  6. T data;
  7. T data2;
  8. BinTreeNode<T> *leftChild, *rightChild;
  9. };
  10. template<typename T>
  11. class BinaryTree {
  12. private:
  13. BinTreeNode<T>* root;
  14. public:
  15. BinaryTree(const string& str) {
  16. root = nullptr;
  17. stack<BinTreeNode<T>*> s; // 辅助栈用于构建二叉树
  18. int i = 0;
  19. while (i < str.size()) {
  20. if (str[i] == '(') {
  21. i++;
  22. } else if (str[i] == ')') {
  23. s.pop();
  24. i++;
  25. } else if (str[i] == ',') {
  26. i++;
  27. } else { // 字母节点
  28. BinTreeNode<T>* newNode = new BinTreeNode<T>;
  29. newNode->data = str[i];
  30. newNode->leftChild = newNode->rightChild = nullptr;
  31. if (!root) { // 如果根节点为空,当前节点即为根节点
  32. root = newNode;
  33. } else { // 否则,将当前节点连接到父节点上
  34. if (!s.top()->leftChild)
  35. s.top()->leftChild = newNode;
  36. else
  37. s.top()->rightChild = newNode;
  38. }
  39. s.push(newNode);
  40. i++;
  41. }
  42. }
  43. }
  44. ~BinaryTree() {
  45. Release(root);
  46. }
  47. void Release(BinTreeNode<T>* node) {
  48. if (!node)
  49. return;
  50. Release(node->leftChild);
  51. Release(node->rightChild);
  52. delete node;
  53. }
  54. BinTreeNode<T>* FindNode(const T& x) {
  55. return Find(root, x);
  56. }
  57. BinTreeNode<T>* Find(BinTreeNode<T>* node, const T& x) {
  58. if (!node)
  59. return nullptr;
  60. if (node->data == x)
  61. return node;
  62. BinTreeNode<T>* leftResult = Find(node->leftChild, x);
  63. BinTreeNode<T>* rightResult = Find(node->rightChild, x);
  64. return leftResult ? leftResult : rightResult;
  65. }
  66. int Height() {
  67. return GetHeight(root);
  68. }
  69. int GetHeight(BinTreeNode<T>* node) {
  70. if (!node)
  71. return 0;
  72. int leftHeight = GetHeight(node->leftChild);
  73. int rightHeight = GetHeight(node->rightChild);
  74. return max(leftHeight, rightHeight) + 1;
  75. }
  76. void SetNode(const T& x, const T& y) {
  77. SetData2(root, x, y);
  78. }
  79. void SetData2(BinTreeNode<T>* node, const T& x, const T& y) {
  80. if (!node)
  81. return;
  82. if (node->data == x)
  83. node->data2 = y;
  84. SetData2(node->leftChild, x, y);
  85. SetData2(node->rightChild, x, y);
  86. }
  87. void Disp() {
  88. PrintTree(root);
  89. cout << endl;
  90. }
  91. void PrintTree(BinTreeNode<T> *root){
  92. if(root==nullptr){
  93. cout<<"";
  94. }else{
  95. cout<<"<"<<root->data<<","<<root->data2<<">";
  96. cout<<"(";
  97. PrintTree(root->leftChild);
  98. cout<<",";
  99. PrintTree(root->rightChild);
  100. cout<<")";
  101. }
  102. }
  103. };
  104. int main()
  105. {
  106. BinaryTree<char> bt("A(B(D(,G)),C(E,F))");
  107. BinTreeNode<char>* p;
  108. cout << "b的高度: " << bt.Height() << endl;
  109. p = bt.FindNode('F');
  110. if (p != nullptr)
  111. cout << "b中存在F结点" << endl;
  112. else
  113. cout << "b中不存在F结点" << endl;
  114. bt.SetNode('A', 1);
  115. bt.SetNode('B', 1);
  116. bt.SetNode('D', 3);
  117. bt.Disp();
  118. return 0;
  119. }

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


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?