满二叉树遍历

Posted litifeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了满二叉树遍历相关的知识,希望对你有一定的参考价值。

图形:

技术图片

程序:

#include<stdio.h>
#include<stdlib.h>
struct node{
        char data;
        struct node* left;
        struct node* right;
};
struct node* newNode(char data){
        struct node* node = (struct node*)malloc(sizeof(struct node));
        node->data=data;
        node->left=NULL;
        node->right=NULL;
        return node;
}
void printPostorder(struct node* node){
        if(node == NULL)
                return;
        printPostorder(node->left);
        printPostorder(node->right);
        printf("%c ",node->data);
}
void printInorder(struct node* node){
        if(node==NULL){
                return;
        }
        printInorder(node->left);
        printf("%c ",node->data);
        printInorder(node->right);
}
void printPreorder(struct node* node){
        if(node==NULL){
                return;
        }
        printf("%c ",node->data);
        printPreorder(node->left);
        printPreorder(node->right);
}
int main(){
        struct node *root=newNode(A);
        root->left=newNode(B);
        root->right=newNode(C);
        root->left->left=newNode(D);
        root->left->right=newNode(E);
        root->right->left=newNode(F);
        root->right->right=newNode(G);
        root->left->left->left=newNode(H);
        root->left->left->right=newNode(I);
        root->left->right->left=newNode(J);
        root->left->right->right=newNode(K);
        root->right->left->left=newNode(L);
        root->right->left->right=newNode(M);
        root->right->right->left=newNode(N);
        root->right->right->right=newNode(O);
        printf("
Preorder raversal of binary tree is 
");
        printPreorder(root);
        printf("
Inorder raversal of binary tree is 
");
        printInorder(root);
        printf("
Postorder raversal of binary tree is 
");
        printPostorder(root);
        return 0;
}

输出:

Preorder raversal of binary tree is
A B D H I E J K C F L M G N O
Inorder raversal of binary tree is
H D I B J E K A L F M C N G O
Postorder raversal of binary tree is
H I D J K E B L M F N O G C A

 

以上是关于满二叉树遍历的主要内容,如果未能解决你的问题,请参考以下文章

二叉树概念及遍历

满二叉树遍历

算法漫游指北(第十三篇):二叉树的基本概念满二叉树完全二叉树二叉树性质二叉搜索树二叉树定义二叉树的广度优先遍历

[数据结构]——二叉树(二叉树的分类和二叉树的遍历)

二叉树的实现遍历及面试题

二叉树入门知识——一篇了解二叉树