二叉树

二叉树就是每个节点至多有两个子节点的树。

关于树结构,我们在上一篇《数据结构与算法之有根树的表达》中已经讲过了。所以这里就简单讲讲二叉树怎么表达。

二叉树的表达相对比较容易,只要定义好每个节点的父节点、左子节点、右子节点即可。这可以很方便地用结构体来实现。

计算二叉树的高

怎么计算二叉树的高呢?根据树高的定义,我们知道,每个节点的高是max(左子节点的高+1,右子节点的高+1)。只需要递归地进行计算,就能算出节点的高。

这样的算法需要把每个节点都遍历一次,因此复杂度为O(N)

题目

Your task is to write a program which reads a rooted binary tree T and prints the following information for each node u of T:

node ID of u
parent of u
sibling of u
the number of children of u
depth of u
height of u
node type (root, internal node or leaf)
If two nodes have the same parent, they are siblings. Here, if u and v have the same parent, we say u is a sibling of v (vice versa).

The height of a node in a tree is the number of edges on the longest simple downward path from the node to a leaf.

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input
The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1.

Output
Print the information of each node in the following format:

node id: parent = p, sibling = s, degree = deg, depth = dep, height = h, type

p is ID of its parent. If the node does not have a parent, print -1.

s is ID of its sibling. If the node does not have a sibling, print -1.

deg, dep and h are the number of children, depth and height of the node respectively.

type is a type of nodes represented by a string (root, internal node or leaf. If the root can be considered as a leaf or an internal node, print root.

Please follow the format presented in a sample output below.

Constraints
1 ≤ n ≤ 25
Sample Input 1
9
0 1 4
1 2 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1
Sample Output 1
node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root
node 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node
node 2: parent = 1, sibling = 3, degree = 0, depth = 2, height = 0, leaf
node 3: parent = 1, sibling = 2, degree = 0, depth = 2, height = 0, leaf
node 4: parent = 0, sibling = 1, degree = 2, depth = 1, height = 2, internal node
node 5: parent = 4, sibling = 8, degree = 2, depth = 2, height = 1, internal node
node 6: parent = 5, sibling = 7, degree = 0, depth = 3, height = 0, leaf
node 7: parent = 5, sibling = 6, degree = 0, depth = 3, height = 0, leaf
node 8: parent = 4, sibling = 5, degree = 0, depth = 2, height = 0, leaf

代码实现

#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 25
#define NIL -1

struct Node
{
	int parent;
	int left;
	int right;
	int h;
	int depth;
	int degree;
	int sibling;
};



Node tree[MAX];
int root;
int n;
int max_depth=0;

int searching(int this_node, int dp)
{
    tree[this_node].depth = dp;
    int h1=0,h2=0, height=0;
    max_depth = max(max_depth, dp);
    if(tree[this_node].left!=NIL)
        h1 = searching(tree[this_node].left, dp+1);
    if(tree[this_node].right!=NIL)
        h2 = searching(tree[this_node].right, dp+1);
    if(tree[this_node].left==NIL&&tree[this_node].right==NIL)
    {
        tree[this_node].h = 0;
        return 0;
    }

    height = max(h1+1, h2+1);
    tree[this_node].h = height;
    return height;


}

void print()
{
    for(int i=0;i<n;i++)
    {
        printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, ", i, tree[i].parent, tree[i].sibling, tree[i].degree,tree[i].depth, tree[i].h);

        if(tree[i].parent==NIL)
            cout<<"root";
        else if(tree[i].degree==0)
            cout<<"leaf";
        else
            cout<<"internal node";
        if(i!=n-1) cout<<endl;
    }
}


int main()
{

	for(int i=0;i<MAX;i++)
    {
        tree[i].parent = NIL;
        tree[i].left = NIL;
        tree[i].right = NIL;
        tree[i].sibling = NIL;
        tree[i].degree = 2;
        tree[i].h = 0;
        tree[i].depth = 0;
    }


    cin>>n;
    for(int i=0;i<n;i++)
    {
        int current_node, left_node, right_node;
        cin>>current_node>>left_node>>right_node;
        tree[current_node].left = left_node;
        tree[current_node].right = right_node;
        tree[left_node].parent = current_node;
        tree[right_node].parent = current_node;
        tree[tree[current_node].left].sibling = tree[current_node].right;
        tree[tree[current_node].right].sibling = tree[current_node].left;
        if(left_node == NIL) tree[current_node].degree--;
        if(right_node == NIL) tree[current_node].degree--;
    }

    for(int i=0;i<n;i++)
    {
        if(tree[i].parent==NIL)
        {
            root = i;
            break;
        }
    }


    searching(root,0);
    print();

}

欢迎关注我的公众号“灯珑”,让我们一起了解更多的事物~

你也可能喜欢

发表评论