📄 tree to an array.txt
字号:
How do you convert a tree into an array?
Discuss it!
The conversion is based on these rules
If i > 1, i/2 is the parent
If 2*i > n, then there is no left child, else 2*i is the left child.
If (2*i + 1) > n, then there is no right child, else (2*i + 1) is the right child.
Converting a tree to an array is very easy
Suppose we have a tree like this
A
B C
D E F G
The array representation would be
a[1] a[2] a[3] a[4] a[5] a[6] a[7]
A B C D E F G
That is, for every node at position i in the array, its left child will be stored at position (2*i) and right child at (2*i + 1). The root starts at position 1.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -