树一二叉搜索树

#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct BiNode {
    int data;
    struct BiNode *left, *right;
} BiNode, *BiTree;

// 初始化树
int InitBiTree(BiTree &T) {
    T = NULL;  // 初始化为空树
    return 1;
}

// 插入节点
int InsertBiNode(BiTree &T, int val) {
    if (T == NULL) {
        T = new BiNode;
        T->data = val;
        T->left = NULL;
        T->right = NULL;
        return 1;
    } else if (val < T->data) {
        return InsertBiNode(T->left, val);
    } else if (val > T->data) {
        return InsertBiNode(T->right, val);
    } else {
        cout << "值已存在,插入失败" << endl;
        return 0;
    }
}

// 删除节点
BiTree DeleteBiNode(BiTree T, int val) {
    if (T == NULL) {
        return T;  // 树为空,直接返回
    }

    if (val < T->data) {
        T->left = DeleteBiNode(T->left, val);
    } else if (val > T->data) {
        T->right = DeleteBiNode(T->right, val);
    } else {
        // 找到要删除的节点
        if (T->left == NULL) {
            // 只有右子树或没有子树
            BiTree temp = T->right;
            delete T;
            return temp;
        } else if (T->right == NULL) {
            // 只有左子树
            BiTree temp = T->left;
            delete T;
            return temp;
        }

        // 有两个子节点的情况
        BiTree temp = T->right;
        while (temp->left != NULL) {
            temp = temp->left;
        }
        T->data = temp->data;  // 用最小节点的值替换当前节点的值
        T->right = DeleteBiNode(T->right, temp->data);  // 删除右子树中的最小节点
    }
    return T;
}

// 前序遍历
void PreOrder(BiTree T) {
    if (T != NULL) {
        cout << T->data << "  ";
        PreOrder(T->left);
        PreOrder(T->right);
    }
}

// 中序遍历
void MidOrder(BiTree T) {
    if (T != NULL) {
        MidOrder(T->left);
        cout << T->data << "  ";
        MidOrder(T->right);
    }
}

// 中序遍历-非递归算法
void MidOrderWhile(BiTree T) {
    stack<BiTree> S;
    BiTree p = T;
    while (p != NULL || !S.empty()) {
        if (p != NULL) {
            S.push(p);
            p = p->left;
        } else {
            p = S.top();
            S.pop();
            cout << p->data << "  ";
            p = p->right;
        }
    }
}

// 后序遍历
void FinOrder(BiTree T) {
    if (T != NULL) {
        FinOrder(T->left);
        FinOrder(T->right);
        cout << T->data << "  ";
    }
}

// 层次遍历
void LevelOrder(BiTree T) {
    if (T == NULL) return;  // 如果树为空,直接返回

    queue<BiTree> Q;
    Q.push(T);

    while (!Q.empty()) {
        BiTree p = Q.front();
        cout << p->data << "  ";
        Q.pop();

        if (p->left != NULL) {
            Q.push(p->left);
        }
        if (p->right != NULL) {
            Q.push(p->right);
        }
    }
}

// 先序遍历 构建二叉树
void PreCreateBiTree(BiTree &T) {
    int a;
    cin >> a;
    if (a == 0) {
        T = NULL;
    } else {
        T = new BiNode;
        T->data = a;
        PreCreateBiTree(T->left);
        PreCreateBiTree(T->right);
    }
}

// 中序遍历 构建二叉树
void MidCreateBiTree(BiTree &T, vector<char>& nodes, int& index) {
    if (index >= nodes.size() || nodes[index] == '#') {
        T = NULL;
    } else {
        T = new BiNode;
        T->data = nodes[index] - '0';  // 将字符转换为整数
        index++;
        MidCreateBiTree(T->left, nodes, index);
        index++;
        MidCreateBiTree(T->right, nodes, index);
    }
}

// 先序复制二叉树
int CopyBiTree(BiTree &T, BiTree &NewT) {
    if (T == NULL) {
        NewT = NULL;
        return 0;
    } else {
        NewT = new BiNode;
        NewT->data = T->data;
        CopyBiTree(T->left, NewT->left);
        CopyBiTree(T->right, NewT->right);
    }
    return 1;
}

// 计算二叉树的深度
int GetTreeDepth(BiTree T) {
    if (T == NULL) {
        return 0;  // 空树深度为0
    } else {
        int leftDepth = GetTreeDepth(T->left);   // 计算左子树的深度
        int rightDepth = GetTreeDepth(T->right); // 计算右子树的深度
        return max(leftDepth, rightDepth) + 1;   // 返回左右子树深度的最大值加1
    }
}

// 计算二叉树的节点总数
int GetNodeCount(BiTree T) {
    if (T == NULL) {
        return 0;
    } else {
        int leftCount = GetNodeCount(T->left);
        int rightCount = GetNodeCount(T->right);
        return leftCount + rightCount + 1;
    }
}

// 计算二叉树的叶子节点数
int GetLeafNodeCount(BiTree T) {
    if (T == NULL) {
        return 0;  // 空树没有叶子节点
    } else if (T->left == NULL && T->right == NULL) {
        return 1;  // 当前节点是叶子节点
    } else {
        // 递归计算左子树和右子树的叶子节点数并相加
        return GetLeafNodeCount(T->left) + GetLeafNodeCount(T->right);
    }
}

int main() {
    BiTree T;
    InitBiTree(T);

    // 插入一些节点
    InsertBiNode(T, 60);
    InsertBiNode(T, 55);
    InsertBiNode(T, 45);
    InsertBiNode(T, 65);
    InsertBiNode(T, 80);
    InsertBiNode(T, 89);

    // 前序遍历
    cout << "前序遍历: ";
    PreOrder(T);
    cout << endl;

    // 中序遍历
    cout << "中序遍历: ";
    MidOrder(T);
    cout << endl;

    // 后序遍历
    cout << "后序遍历: ";
    FinOrder(T);
    cout << endl;

    // 中序遍历-非递归算法
    cout << "中序遍历-非递归算法: ";
    MidOrderWhile(T);
    cout << endl;

    // 层次遍历
    cout << "层次遍历: ";
    LevelOrder(T);
    cout << endl;

    // 计算二叉树的深度
    int depth = GetTreeDepth(T);
    cout << "二叉树的深度为: " << depth << endl;

    // 计算二叉树的节点总数
    int nodeCount = GetNodeCount(T);
    cout << "二叉树的节点总数为: " << nodeCount << endl;

    //计算二叉树的叶子节点数
    int LeafnodeCount = GetLeafNodeCount(T);
    cout<<"二叉树的叶子节点总数为:"<<LeafnodeCount<<endl;

    cout << "------------------------" << endl;
    BiTree T2;
    InitBiTree(T2);
    cout << "请输入二叉树的节点值,0表示空节点(例如:1 2 0 0 3 0 0):" << endl;
    PreCreateBiTree(T2);
    cout << endl;

    // 前序遍历
    cout << "前序遍历: ";
    PreOrder(T2);
    cout << endl;

    // 中序遍历
    cout << "中序遍历: ";
    MidOrder(T2);
    cout << endl;

    // 后序遍历
    cout << "后序遍历: ";
    FinOrder(T2);
    cout << endl;

    // 中序遍历-非递归算法
    cout << "中序遍历-非递归算法: ";
    MidOrderWhile(T2);
    cout << endl;

    // 层次遍历
    cout << "层次遍历: ";
    LevelOrder(T2);
    cout << endl;

    BiTree T3;
    InitBiTree(T3);
    vector<char> nodes = {'#', '1', '2', '#', '#', '3', '#', '#'};
    int index = 0;
    MidCreateBiTree(T3, nodes, index);

    // 前序遍历
    cout << "前序遍历: ";
    PreOrder(T3);
    cout << endl;

    // 层次遍历
    cout << "层次遍历: ";
    LevelOrder(T3);
    cout << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值