btree.cpp代码如下:

#include <iostream>
using namespace std;

// 二叉树结点的定义
struct Node {
    char data;
    Node* left;
    Node* right;
};

// 创建二叉树
void createTree(Node* &root, char* str, int& index) {
    if (str[index] == '\0') {
        root = NULL;
        return;
    }
    if (str[index] == '(') {
        index++;
        if (str[index] == ')') {
            root = NULL;
            index++;
            return;
        }
        root = new Node;
        root->data = str[index];
        root->left = NULL;
        root->right = NULL;
        createTree(root->left, str, ++index);
        createTree(root->right, str, ++index);
    } else {
        index++;
        createTree(root, str, index);
    }
}

// 输出二叉树
void printTree(Node* root) {
    if (root == NULL) return;
    cout << root->data;
    if (root->left != NULL || root->right != NULL) {
        cout << "(";
        printTree(root->left);
        cout << ",";
        printTree(root->right);
        cout << ")";
    }
}

// 获取结点指针
Node* getNode(Node* root, char c) {
    if (root == NULL) return NULL;
    if (root->data == c) return root;
    Node* p = getNode(root->left, c);
    if (p != NULL) return p;
    return getNode(root->right, c);
}

// 获取二叉树高度
int getHeight(Node* root) {
    if (root == NULL) return 0;
    int leftHeight = getHeight(root->left);
    int rightHeight = getHeight(root->right);
    return max(leftHeight, rightHeight) + 1;
}

// 释放二叉树内存
void freeTree(Node* root) {
    if (root == NULL) return;
    freeTree(root->left);
    freeTree(root->right);
    delete root;
}

int main() {
    char str[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
    Node* root;
    int index = 0;
    createTree(root, str, index);

    cout << "二叉树b: ";
    printTree(root);
    cout << endl;

    Node* h = getNode(root, 'H');
    if (h == NULL) {
        cout << "未找到结点H" << endl;
    } else {
        cout << "结点H的左孩子值为:" << h->left->data << endl;
        cout << "结点H的右孩子值为:" << h->right->data << endl;
    }

    int height = getHeight(root);
    cout << "二叉树b的高度为:" << height << endl;

    freeTree(root);
    return 0;
}

exp1.cpp代码如下:

#include <iostream>
using namespace std;

// 二叉树结点的定义
struct Node {
    char data;
    Node* left;
    Node* right;
};

// 创建二叉树
void createTree(Node* &root, char* str, int& index) {
    if (str[index] == '\0') {
        root = NULL;
        return;
    }
    if (str[index] == '(') {
        index++;
        if (str[index] == ')') {
            root = NULL;
            index++;
            return;
        }
        root = new Node;
        root->data = str[index];
        root->left = NULL;
        root->right = NULL;
        createTree(root->left, str, ++index);
        createTree(root->right, str, ++index);
    } else {
        index++;
        createTree(root, str, index);
    }
}

// 输出二叉树
void printTree(Node* root) {
    if (root == NULL) return;
    cout << root->data;
    if (root->left != NULL || root->right != NULL) {
        cout << "(";
        printTree(root->left);
        cout << ",";
        printTree(root->right);
        cout << ")";
    }
}

// 获取结点指针
Node* getNode(Node* root, char c) {
    if (root == NULL) return NULL;
    if (root->data == c) return root;
    Node* p = getNode(root->left, c);
    if (p != NULL) return p;
    return getNode(root->right, c);
}

// 获取二叉树高度
int getHeight(Node* root) {
    if (root == NULL) return 0;
    int leftHeight = getHeight(root->left);
    int rightHeight = getHeight(root->right);
    return max(leftHeight, rightHeight) + 1;
}

// 释放二叉树内存
void freeTree(Node* root) {
    if (root == NULL) return;
    freeTree(root->left);
    freeTree(root->right);
    delete root;
}

int main() {
    char str[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
    Node* root;
    int index = 0;
    createTree(root, str, index);

    cout << "二叉树b: ";
    printTree(root);
    cout << endl;

    Node* h = getNode(root, 'H');
    if (h == NULL) {
        cout << "未找到结点H" << endl;
    } else {
        cout << "结点H的左孩子值为:" << h->left->data << endl;
        cout << "结点H的右孩子值为:" << h->right->data << endl;
    }

    int height = getHeight(root);
    cout << "二叉树b的高度为:" << height << endl;

    freeTree(root);
    return 0;
}
``

原文地址: https://www.cveoy.top/t/topic/fHBX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录