本文共 3768 字,大约阅读时间需要 12 分钟。
???????????????????????????????????????????????????????????????
?????????????????????????????????????????????????
typedef struct BiNode { char data; struct BiNode* lchild; struct BiNode* rchild;} BiNode;BiTree* createBiTree(BiNode* bt) { char c; cin >> c; if (c == '#') { bt = NULL; } else { bt = new BiNode; bt->data = c; bt->lchild = createBiTree(bt->lchild); bt->rchild = createBiTree(bt->rchild); } return bt;} ????????????????????C++?????????????????????
class BiTree {private: struct Node { char data; Node* left; Node* right; }; Node* root;public: BiTree() { root = create(root); } void create(Node*& node) { char c; cin >> c; if (c == '#') { node = NULL; } else { node = new Node; node->data = c; create(node->left); create(node->right); } }}; ???????????????????????????????????
void preOrder(BiTree* bt) { if (bt == NULL) { return; } else { cout << bt->root->data << " "; preOrder(bt->root->left); preOrder(bt->root->right); }} ????????????????
void levelOrder(BiTree* root) { if (root == NULL) { return; } queue q; q.enqueue(root->root); while (!q.empty()) { Node* current = q.front(); q.pop(); cout << current->data << " "; if (current->left != NULL) { q.enqueue(current->left); } if (current->right != NULL) { q.enqueue(current->right); } }} ????????????????????
int getTreeDepth(BiTree* T) { if (T == NULL) { return 0; } else { int leftDepth = getTreeDepth(T->root->left); int rightDepth = getTreeDepth(T->root->right); return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1); }} ??????????????
void copyTree(BiTree* T, BiTree*& NewT) { if (T == NULL) { NewT = NULL; return; } else { NewT = new BiTree; NewT->root->data = T->root->data; copyTree(T->root->left, NewT->root->left); copyTree(T->root->right, NewT->root->right); }} ?????????????
int nodeCount(BiTree* T) { if (T == NULL) { return 0; } else { return nodeCount(T->root->left) + nodeCount(T->root->right) + 1; }} ??????????????????
int getLeafNumber(BiTree* root) { if (root == NULL) { return 0; } else if (root->root->left == NULL && root->root->right == NULL) { return 1; } else { return getLeafNumber(root->root->left) + getLeafNumber(root->root->right); }} ?????????????
int getDegreeOneNode(BiTree* T) { if (T == NULL) { return 0; } if ((T->root->left == NULL && T->root->right != NULL) || (T->root->left != NULL && T->root->right == NULL)) { return 1 + getDegreeOneNode(T->root->left) + getDegreeOneNode(T->root->right); } else { return getDegreeOneNode(T->root->left) + getDegreeOneNode(T->root->right); }} ??????????
int getDegreeTwoNode(BiTree* T) { if (T == NULL) { return 0; } if (T->root->left != NULL && T->root->right != NULL) { return 1 + getDegreeTwoNode(T->root->left) + getDegreeTwoNode(T->root->right); } else { return getDegreeTwoNode(T->root->left) + getDegreeTwoNode(T->root->right); }} ?????????
void releaseTree(BiTree* T) { if (T != NULL) { releaseTree(T->root->left); releaseTree(T->root->right); delete T; }} ???????????????????????????????????
转载地址:http://ttyg.baihongyu.com/