开发者

Adjacency matrix of binary tree of depth 4 in C

How would the adjacency matrix of binary tree of depth 4 in C look like? The depth of a node is defined as its distance from the root.

I know a is at depth zero e is at depth 2

             a
          /     \ 
         b       c
       / \      /  \ 
      d  e      f   g
    / \ / \    / \ / \ 
   h  i j k   l  m n  o



  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
  a b c d e f g h i j k l m n o
a   1 1 0 0 0 0 0 0 0 0 0 0 0 0
b 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0
c 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0
d 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0
e 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
f 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0
g 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1
h 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
i 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 
j 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
k 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
l 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
m 0 0 0 开发者_运维百科0 0 1 0 0 0 0 0 0 0 0 0
n 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
o 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0


Just an observation. Holds true in general.

If you have a complete binary tree, by which I mean all internal nodes have two children, and all leaves at same depth. And if you number them starting from 1 i.e. in your case

a = 1; b = 2; c = 3 ....

For any node x -> i It's children will be 2*i and 2*i + 1 And it's parent will be floor(i/2)

In your case, you can just hard-code it since you have only depth = 4

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜