二叉树各种遍历的实现(递归、非递归、层次、高度和节点数目)

以下代码将实现下列函数:

(1)建树

(2)递归前序遍历

(3)递归中序遍历

(4)递归后序遍历

(5)非递归前序遍历

(6)非递归中序遍历

(7)非递归后序遍历

(8)层次遍历

(9)树的高度

(10)节点数目

#include<stdio.h>
#include<stack>
#include<stdlib.h>
#include<deque>

using namespace std;

struct BinaryTree
{
    char c;
    BinaryTree *left;
    BinaryTree *right;
};

void creatTree(BinaryTree **root)
{
    char c;
    scanf("\n%c", &c);

    if(c == '#')
        *root = NULL;
    else 
    {
    *root = (BinaryTree*)malloc(sizeof(BinaryTree));
    (*root)->c = c;
    creatTree(&((*root)->left));
    creatTree(&((*root)->right));
    }
    
}

void preOrder(BinaryTree *root)
{
    if(root == NULL)
        return;

    printf("%c ", root->c);
    preOrder(root->left);
    preOrder(root->right);

}

void inOrder(BinaryTree *root)
{
    if(root == NULL)
        return;

    inOrder(root->left);
    printf("%c ", root->c);
    inOrder(root->right);
}

void postOrder(BinaryTree *root)
{
    if(root == NULL)
        return;

    postOrder(root->left);
    postOrder(root->right);
    printf("%c ", root->c);
}

void preOrder_nonRecursive(BinaryTree *root)
{
    if(root == NULL)
        return;

    stack<BinaryTree*> s;
    BinaryTree *curr = root;

    while(curr != NULL || !s.empty())
    {
       while(curr != NULL)
       {
           printf("%c ", curr->c);
           s.push(curr);
           curr = curr->left;
       }

       if(!s.empty())
       {
           curr = s.top();
           s.pop();
           curr = curr->right;
       }
    }
}

void inOrder_nonRecursive(BinaryTree *root)
{
    if(root == NULL)
        return;

    stack<BinaryTree*> s;
    BinaryTree *curr = root;

    while(curr != NULL || !s.empty())
    {
       while(curr != NULL)
       {
    
           s.push(curr);
           curr = curr->left;
       }

       if(!s.empty())
       {
           curr = s.top();
           printf("%c ", curr->c);
           s.pop();
           curr = curr->right;
       }
    }
}
void postOrder_nonRecursive(BinaryTree *root)
{
    if(root == NULL)
        return;

    stack<BinaryTree*> s;
    BinaryTree *curr = root;
    BinaryTree *preVisited = NULL;

    while(curr != NULL || !s.empty())
    {
       while(curr != NULL)
       {
           s.push(curr);
           curr = curr->left;
       }

       curr = s.top();
       if(curr->right == NULL || curr->right == preVisited)
       {
           printf("%c ", curr->c);
           preVisited = curr;
           s.pop();
           curr = NULL;
       }
       else
       {
           curr = curr->right;
       }
    }
}

void levelOrder(BinaryTree *root)
{
    if(root == NULL)
        return;

    deque<BinaryTree *> s;
    s.push_back(root);

    while(s.size())
    {
        BinaryTree *node = s.front();
        s.pop_front();
        printf("%c ", node->c);

        if(node->left)
            s.push_back(node->left);
        if(node->right)
            s.push_back(node->right);
    }
}

int depth(BinaryTree *root)
{
    if(root == NULL)
        return 0;

    int left = depth(root->left);
    int right = depth(root->right);

    return 1 + (left > right ? left : right);
}

int count(BinaryTree *root)
{
    if(root == NULL)
        return 0;

    return 1 + count(root->left) + count(root->right);
}
int main()
{
    BinaryTree *root = NULL;
    creatTree(&root);

    preOrder(root);
    printf("\n");
    inOrder(root);
    printf("\n");
    postOrder(root);
    printf("\n");
    preOrder_nonRecursive(root);
    printf("\n");
    inOrder_nonRecursive(root);
    printf("\n");
    postOrder_nonRecursive(root);
    printf("\n");
    levelOrder(root);
    printf("\n");
    printf("%d\n", depth(root));
    printf("%d\n", count(root));
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值