数据结构-二叉树常用操作集合

二叉树常用操作集合

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

// 定义一个二叉树
struct TreeNode{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

// 先序遍历 先访问根节点 再先序遍历左子树 再先序遍历右子树
void preOrder(TreeNode* root){
    if(!root) return;
    cout << root->val << " ";
    preOrder(root->left);
    preOrder(root->right);
}

// 中序遍历,先序遍历左子树,再访问根节点 再中序遍历右子树
void inOrder(TreeNode* root){
    if(!root) return;
    inOrder(root->left);
    cout << root->val << " ";
    inOrder(root->right);
}

// 后续遍历 后续遍历左节点 后续遍历右节点 再访问根节点
void postOrder(TreeNode* root){
    if(!root) return;
    postOrder(root->left);
    postOrder(root->right);
    cout << root->val << " ";
}

// 计算二叉树的深度
int calTreeDepth(TreeNode* root){
    if(root == NULL) return 0;
    else{   
        int m = calTreeDepth(root->left);
        int n = calTreeDepth(root->right);
        if(m > n) return m + 1;
        else return n + 1;
    }
}

// 统计二叉树中节点的个数
int treeNodeCount(TreeNode* root){
    if(root == NULL) return 0;
    else return treeNodeCount(root->left) + treeNodeCount(root->right)+1;
}

// 统计二叉树中叶子节点的个数
int leafNodeCount(TreeNode* root){
    if(root == NULL) return 0;
    // 如果该节点的左右子节点均为空,那么该节点就是叶子节点
    if(!root->left && !root->right){
        return 1;
    }else{
        return leafNodeCount(root->left) + leafNodeCount(root->right);
    }
}

// 统计二叉树的度的个数
int duCount(TreeNode* root){
    if(root == NULL) return 0;
    if((!root->left)&&(root->right) || (root->left)&&(root->right)){
        return duCount(root->left) + duCount(root->right) + 1;
    }
    else{
        return duCount(root->left) + duCount(root->right);
    }
}

// 二叉树中从每个叶子结点到根结点的路径
void printAllPath(TreeNode* root, char path[], int pathLen){
    int i;
    if(root != NULL){
        path[pathLen] = root->val;
        if(root->left == NULL && root->right == NULL);{
            for(int i = pathLen; i >= 0; i--){
                cout << path[i] << " ";
            }
            cout << endl;
        }
    }else{
        printAllPath(root->left, path, pathLen + 1);
        printAllPath(root->right, path, pathLen + 1);
    }
}

// 使用递归进行左右结点转换
void exchangeTreeNode(TreeNode* root){
    TreeNode* temp;
    if(root != NULL){
        temp = root->left;
        root->left = root->right;
        root->right = temp;
        exchangeTreeNode(root->left);
        exchangeTreeNode(root->right);
    }
}

// 二叉树的双序遍历
void doubleOrderTraverse(TreeNode* root){
    if(root != NULL){
        cout << root->val;
        doubleOrderTraverse(root->left);
        cout << root->val;
        doubleOrderTraverse(root->right);
    }
}

// 利用队列迭代遍历二叉树
void queueOrderTraverse(TreeNode* root){
    if(root == NULL) return;
    queue <TreeNode*> q;
    q.push(root);

    while(!q.empty()){
        TreeNode* curr = q.front(); //拷贝队列首个结点
        q.pop();
        cout << curr->val << " ";
        if(curr->left != NULL){
            q.push(curr->right);
        }
        if(curr->right != NULL){
            q.push(curr->right);
        }
    }
}

// 利用栈迭代遍历二叉树
void stackTraverse(TreeNode* root){
    if(root == NULL) return;
    stack<TreeNode*> stc;
    stc.push(root);
    TreeNode* curr = root;
    while(!stc.empty()){
        curr = stc.top();
        stc.pop();
        cout << curr->val << " ";
        if(curr->left){
            stc.push(curr->left);
        }
        if(curr->right){
            stc.push(curr->right);
        }
    }
}

// 初始化二叉树
TreeNode* treeInit(){
    TreeNode* root = new TreeNode(2);
    root->left = new TreeNode(1);
    root->left->left = new TreeNode(0);
    root->left->right = new TreeNode(4);

    root->right = new TreeNode(3);
    root->right->left = new TreeNode(2);
    root->right->right = new TreeNode(5);
    return root;
}

int main(){
    TreeNode* root=  treeInit();
    cout << "preOrder traversal: ";
    preOrder(root); // 先序遍历
    cout << endl;

    cout << "inOrder traversal: ";
    inOrder(root);
    cout << endl;

    cout << "postOrder traversal: ";
    postOrder(root);
    cout << endl;

    cout << "Queue Traverse TreeNode: ";
    queueOrderTraverse(root);
    cout << endl;

    cout << "Stack Traverse TreeNode: ";
    stackTraverse(root);
    cout << endl;


    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值