求二叉树问题

1,写出这棵树的度()2.这棵树的深度是多少()3.节点E的双亲是(),兄弟节点是(),孩子节点时()4。求二叉树的先序,中序,后序遍历顺序

1. 这棵树的度是2.
   注:节点A有两个分支,表示节点A的度是2,这是最大的度,所以这棵树的度是2.
2. 这棵树的深度是4.
   注:根节点A的深度是1,节点B,C的深度是2,节点D,E,F的深度是3,
      节点H,I,J的深度是4. 这棵树的最大深度是4,所以这棵树的深度是4.
3. 节点E的双亲是B,兄弟节点是D,孩子节点是I.

4. 先序遍历序列: A B D H E I C F J
   中序遍历序列: H D B E I A F J C
   后序遍历序列: H D I E B J F C A

二叉树示意图:
           A
       /      \
      B        C
     / \      /
    D   E    F
   /     \    \
  H       I    J


//C语言测试程序
#include "stdio.h"
#include "stdlib.h"
struct tree
{
    char data;
    struct tree *left;
    struct tree *right;
};
typedef struct tree treenode;
typedef treenode *btree;

btree createbtree(char *data,int pos,int maxPos) //递归创建法
{
    btree newnode;

    if(data[pos]==0 || pos>maxPos)
    {
        return NULL;
    }
    else
    {
        newnode=(btree)malloc(sizeof(treenode));
        newnode->data=data[pos];
        newnode->left=createbtree(data,2*pos,maxPos);
        newnode->right=createbtree(data,2*pos+1,maxPos);
        return newnode;
    }
}

void inorder(btree ptr)
{
    if(ptr!=NULL)
    {
        inorder(ptr->left);
        printf("%C ",ptr->data);
        inorder(ptr->right);
    }
}

void preorder(btree ptr)
{
    if(ptr!=NULL)
    {
        printf("%C ",ptr->data);
        preorder(ptr->left);
        preorder(ptr->right);
    }
}

void postorder(btree ptr)
{
    if(ptr!=NULL)
    {
        postorder(ptr->left);
        postorder(ptr->right);
        printf("%C ",ptr->data);
    }
}

int main()
{
    btree root=NULL;
    int i;

    char data[16]={0,'A','B','C','D','E','F',0,'H',0,0,'I',0,'J',0,0};
    root=createbtree(data,1,15);
    printf("二叉树的顺序存储内容: ");
    for(i=1;i<16;i++)
    {
        if(data[i]==0)
        {
            printf("^ ");
        }
        else
        {
            printf("%c ",data[i]);
        }
    }

    printf("\n先序遍历序列: ");
    preorder(root);
    printf("\n中序遍历序列: ");
    inorder(root);
    printf("\n后序遍历序列: ");
    postorder(root);

    printf("\n");
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答