> For the complete documentation index, see [llms.txt](https://garychang.gitbook.io/data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garychang.gitbook.io/data-structure/lecture2-tree-and-binary-tree/lecture2.7-tree-and-binary-tree-conversion.md).

# 2.7 - Tree and Binary Tree Conversion

樹與二元樹相互轉換

### 1. 將樹化成二元樹

1. 建立sibling間的平行links
2. 刪除父點、子點間的links，只保留最左子點和父點的links
3. 整個樹順時鐘轉45度，即
   1. 最左子點當父點（左子點）
   2. 次右節點當右子點

### 2. 將二元樹化成樹

1. 逆時針轉45度，即將右子點全部上拉成次右兄弟
2. 補上父點與子點的links
3. 將sibling平行links刪除

### 3. Forest化成Binary Tree

1. 將forest中的每棵樹化成二元樹
2. 將每科二元樹的Root視為sibling，平行串連
3. 將這些Roots的次右兄弟轉成右子點

### 4. Binary Tree化成Forest

1. 將Root的右子點的所有links上拉成為Root的siblings
2. 刪掉Root的siblings間的平行links
3. 將每棵二元樹化成樹
