吐槽一下
唉数据结构,唉二叉树,唉C++类,好多都不会,不懂得class类的用法,不知道怎么写代码
先是复制课本上的代码,然后经过缝缝补补,发现课本上的代码竟然有字母打错的可能(痛骂编者
然后用ChatGPT不断缝缝补补(点赞ChatGPT,用来写代码真的很不错
这下知新作业终于是写完了,逆向已经一个也没动了
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <iostream> using namespace std;
template<typename DataType> struct BiNode { DataType data; BiNode<DataType>* lchild, * rchild; };
template<typename DataType> class BiTree { public: BiTree() { root = Creat(); } ~BiTree() { Release(root); } void PreOrder() { PreOrder(root); } void InOrder() { InOrder(root); } void PostOrder() { PostOrder(root); } void LevelOrder(); int NodeTree(BiNode<DataType>* bt) { return NodeTreeHelper(bt); } int TreeDepth() { return TreeDepthHelper(root); } BiNode<DataType>* root; private: BiNode<DataType>* Creat(); void Release(BiNode<DataType>* bt); void PreOrder(BiNode<DataType>* bt); void InOrder(BiNode<DataType>* bt); void PostOrder(BiNode<DataType>* bt); int NodeTreeHelper(BiNode<DataType>* bt); int TreeDepthHelper(BiNode<DataType>* bt);
};
template <typename DataType> void BiTree<DataType> ::PreOrder(BiNode<DataType>* bt) { if (bt == nullptr) return; else { cout << bt->data; PreOrder(bt->lchild); PreOrder(bt->rchild); } }
template <typename DataType> void BiTree<DataType> ::InOrder(BiNode<DataType>* bt) { if (bt == nullptr) return; else { InOrder(bt->lchild); cout << bt->data; InOrder(bt->rchild); } }
template <typename DataType> void BiTree<DataType> ::PostOrder(BiNode<DataType>* bt) { if (bt == nullptr) return; else { PostOrder(bt->lchild); PostOrder(bt->rchild); cout << bt->data; } }
template <typename DataType> void BiTree<DataType> ::LevelOrder() { BiNode<DataType>* Q[100], * q = nullptr; int front = -1, rear = -1; if (root == nullptr) return; Q[++rear] = root; while (front != rear) { q = Q[++front]; cout << q->data; if (q->lchild != nullptr) Q[++rear] = q->lchild; if (q->rchild != nullptr) Q[++rear] = q->rchild; } }
template <typename DataType> BiNode<DataType>* BiTree<DataType>::Creat() { BiNode<DataType>* bt; char ch; cin >> ch; if (ch == '#') { bt = nullptr; } else { bt = new BiNode<DataType>; bt->data = ch; bt->lchild = Creat(); bt->rchild = Creat(); } return bt; }
template<typename DataType> void BiTree<DataType>::Release(BiNode<DataType>* bt) { if (bt == nullptr) { return; } else { Release(bt->lchild); Release(bt->rchild); delete bt; } }
template <typename DataType> int BiTree<DataType>::NodeTreeHelper(BiNode<DataType>* bt) { if (bt == nullptr) { return 0; } if (bt->lchild == nullptr && bt->rchild == nullptr) { return 1; } return NodeTreeHelper(bt->lchild) + NodeTreeHelper(bt->rchild); }
template <typename DataType> int BiTree<DataType>::TreeDepthHelper(BiNode<DataType>* bt) { if (bt == nullptr) { return 0; } int leftDepth = TreeDepthHelper(bt->lchild); int rightDepth = TreeDepthHelper(bt->rchild); return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1); }
int main() { BiTree<char> t{}; cout << "前序遍历"; t.PreOrder(); cout << "\n中序遍历"; t.InOrder(); cout << "\n后序遍历"; t.PostOrder(); cout << "\n层序遍历"; t.LevelOrder(); cout << "\n叶子结点数:" << t.NodeTree(t.root); cout << "\n二叉树深度:" << t.TreeDepth();
return 0; }
|