iT邦幫忙

0

階層樹(Level-Order Tree)的插入

有一資料長這樣[1,2,3,4,5,6]

我用二元樹階層插入是長這樣,可透過比較node大小,來找到最尾端的node做新增

 Dim current As TNode = Root
                Dim parent As TNode = current
                While True
                    parent = current
                    If num < current.data Then
                        current = current.LNode
                        If current Is Nothing Then
                            parent.LNode = newNode
                            Exit While
                        End If
                    Else
                        current = current.RNode
                        If current Is Nothing Then
                            parent.RNode = newNode
                            Exit While
                        End If
                    End If



                End While

結構長這樣

1

2

3

4

5

6

現在我想用階層的方式加入,卻發現沒有一個判斷的方式讓我找到要在哪一個node新增節點
結構要長這樣:
1
/
2 3
/ \ /
4 5 6
請問有什麼類似的判斷方式嗎?

fillano iT邦超人 1 級 ‧ 2019-03-08 09:27:12 檢舉
二元樹每一層最大節點數量是固定的,而且可以算出來。
你是不是想要平衡樹 AVL-Tree。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答