码迷,mamicode.com
首页 > 其他好文 > 详细

满二叉树遍历

时间:2019-04-06 17:17:28      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:har   data   nod   new   ima   class   ntp   malloc   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("\nPreorder raversal of binary tree is \n");
        printPreorder(root);
        printf("\nInorder raversal of binary tree is \n");
        printInorder(root);
        printf("\nPostorder raversal of binary tree is \n");
        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

 

满二叉树遍历

标签:har   data   nod   new   ima   class   ntp   malloc   com   

原文地址:https://www.cnblogs.com/litifeng/p/10662120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!