二叉树的基本操作 C++代码实现

本文介绍了如何使用C++实现二叉树的基本操作,包括前序、中序、后序递归与非递归遍历,求二叉树深度、叶子节点数量,以及从给定的两种序列创建二叉树的方法。
摘要由CSDN通过智能技术生成

首先定义节点

typedef struct BTree
{
    int    value;
    struct BTree *lchild;
    struct BTree *rchild;
}BTree;

前序递归建立二叉树

/*
**num 前序序列
**index 下标
*/
BTree *CreateBTree(BTree *node,int *num,int& index)
{
    if(num[index] == 0)
        return NULL;
    else
    {
        node = new BTree;
        node -> value = num[index];
        node -> lchild = CreateBTree(node->lchild,num,++index);
        node -> rchild = CreateBTree(node->rchild,num,++index);
    }
    return node;
}

几种遍历

递归--前序遍历
void preOrder(BTree * root)
{
    if(root == NULL)
        return;
    cout << root -> value << " ";  //先输出树的根节点的值
    preOrder(root -> lchild);       //递归 左子树
    preOrder(root -> rchild);       //递归 右子树   
}
非递归--前序遍历
void preOrder_dxm(BTree * root)
{
    stack<BTree*> S;
    BTree *p = root;
    while(p != NULL || !S.empty())
    {
        while(p != NULL)
        {
            cout << p -> value << " ";
            S.push(p);
            p = p -> lchild;
        }
        if(!S.empty())
        {
            S.pop();
            if(S.empty())
                return ;
            p = S.top();
            S.pop();
            p = p -> rchild;
        }
    }
}
递归--中序遍历
void inOrder(BTree * root)
{
    if(root == NULL)
        return;
    inOrder(root -> lchild);
    cout << root -> value << " ";
    inOrder(root -> rchild);
}
非递归--中序遍历
void inOrder_dxm(BTree * r
  • 22
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是二叉树基本操作C++代码,包括创建二叉树、先序遍历、中序遍历、后序遍历和层次遍历。 ```c++ #include<iostream> #include<queue> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) :val(x), left(NULL), right(NULL) {} }; //创建二叉树 void createTree(TreeNode*& root) { int x; cin >> x; if (x == -1) { root = NULL; } else { root = new TreeNode(x); createTree(root->left); createTree(root->right); } } //先序遍历 void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } //中序遍历 void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } //后序遍历 void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } //层次遍历 void levelOrder(TreeNode* root) { if (root == NULL) { return; } queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* temp = q.front(); q.pop(); cout << temp->val << " "; if (temp->left) { q.push(temp->left); } if (temp->right) { q.push(temp->right); } } } int main() { TreeNode* root; createTree(root); cout << "先序遍历:"; preOrder(root); cout << endl; cout << "中序遍历:"; inOrder(root); cout << endl; cout << "后序遍历:"; postOrder(root); cout << endl; cout << "层次遍历:"; levelOrder(root); cout << endl; return 0; } ``` 注意:以上代码中采用了递归的方式实现树的遍历,这种方式使用较为简单,但在遇到大规模的树时可能会引起堆栈溢出,因此在实际应用中需要注意树的大小。对于大规模的树,可以采用非递归方式实现遍历。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值