白书8.3 二叉树的表达

 

 代码如下:

# include <iostream>
using namespace std;

# define MAX 100001
# define NIL -1

struct Node{
    int left,right,parent;
};

Node a[MAX];

int sibling(int x){
    int i=a[x].parent;
    if(x==a[i].left)    return a[i].right;
    else    return a[i].left;
}

int degree(int n){
    int i=0;
    if(a[n].left!=NIL)    i+=1;
    if(a[n].right!=NIL)    i+=1;
    return i;
}

int d[MAX];
int h[MAX];

int depth(int n,int m){/*一个编号,一个计数器 */
    if(n==NIL)    return;
    d[n]=m;
    depth(a[n].left,m+1);
    depth(a[n].right,m+1);
}//写void是因为感觉要构造一个数组,把整棵树各个节点的深度都存放进去,要调用也简单 

int setheight(int u){
    int h1=0,h2=0;
    if(a[u].left!=NIL)    h1=setheight(a[u].left)+1;
    if(a[u].right!=NIL)    h2=setheight(a[u].right)+1;
    return h[u]={h1>h2?h1:h2};
}

void print(int n){
    cout<<"node "<<n<<":";
    cout<<"parent = "<<a[n].parent<<",";
    cout<<"sibling = "<<sibling(n)<<",";
    cout<<"degree = "<<degree(n)<<",";
    //cout<<"depth = "<<depth(n)<<",";
    cout<<"depth = "<<d[n]<<",";
    cout<<"height = "<<h[n]<<",";
}

int main()
{
    int n;
    cin>>n;
    int i;
    int v,l,r;//这里还要有一个v,可以让用户输入编号,因为用户的输入顺序不一定是按照i的自增
    for(i=0; i<n; i++)    a[i].parent=NIL;//回头补充:初始化双亲节点 
    for(i=0; i<n; i++){
        cin>>v>>l>>r;
        a[v].left=l;
        a[v].right=r;//这里输入完了,但我还想要确定双亲节点,因为输出结果要有双亲 
        if(a[v].left!=NIL)    a[l].parent=v;
        if(a[v].right!=NIL)    a[r].parent=v;
    }
    //好那现在这棵树就建好了,接下来看看输出要求,要输出兄弟节点的编号、度、深度、高度,以及是否为叶子、节点或者根
    int root;
    for(i=0; i<n; i++){
        if(a[i].parent==NIL)    root=i;//找出根节点 
    }//对了,还得初始化一下双亲节点,令双亲节点全部都为空先 
    
    depth(root,0);//感觉要先找到这个根节点在哪里,才能接着往下解决深度、高度的问题,首先就得求出这个根节点用户是写的是什么编号 
    setheight(root);
    for(i=0; i<n; i++)    print(i);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种树形结构,其中每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的链式存储是指使用指针来表示二叉树的存储方式。每个节点包含三个部分:数据域、左子节点指针和右子节点指针。如果一个节点没有左子节点或右子节点,则对应指针为空。 在C语言中,可以使用结构体来表示二叉树的节点,如下所示: ```c struct TreeNode { int val; // 数据域 struct TreeNode *left; // 左子节点指针 struct TreeNode *right; // 右子节点指针 }; ``` 二叉树的链式存储可以通过递归的方式来实现建立、遍历、计算深度、结点数、叶子数等基本操作。下面是一个示例代码,用于先序创建二叉树,并计算二叉树的高度和叶子个数: ```c #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 先序创建二叉树 struct TreeNode* createTree() { char c; scanf("%c", &c); if (c == '#') { return NULL; } struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = c; root->left = createTree(); root->right = createTree(); return root; } // 计算二叉树的高度 int getHeight(struct TreeNode *root) { if (root == NULL) { return 0; } int leftHeight = getHeight(root->left); int rightHeight = getHeight(root->right); return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; } // 计算二叉树的叶子个数 int getLeafCount(struct TreeNode *root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } return getLeafCount(root->left) + getLeafCount(root->right); } int main() { struct TreeNode *root = createTree(); printf("Height of the binary tree is: %d\n", getHeight(root)); printf("Number of leaves in the binary tree is: %d\n", getLeafCount(root)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值