Tree

Binary Tree only 0,1,2 Child(ren)
Traversal
Search
DFS(Depth First Search)
stack.push(root);
While(!empty){
current=stack.pop();
DFS(current->child1);
DFS(current->child2);
...
}
BFS(Breath First Search)
Tree Implementation
Parent-Point (Two Linear Array) Parent: O(1) Leftmost child: O(n) 트리끼리 합치기 어렵다.
| Parent | 0 | 1 | 1 | 2 | 2 | 2 |
|---|---|---|---|---|---|---|
| Label | a | b | c | d | e | f |

List of Children Linked list based Combine tree: O(1)
only change the parent of each root Separate array combine: O(n) *DIfficult to access Right sibling(need to search linked list) *Space complexity: children appear in both array

Left-Child / Right-Sibling Static imlement(array): each node has parent, L.C, R.S information *fixed space
Linked Implement(Linked List) Knuth Transform : convert into BINARY tree 1. Leftmost child →Left child (red) 2. Right Sibling→Right child (green) 3. Rotate 45 degrees

Convert any tree into BINARY TREE
*traversals
preorder in tree → preorder in BT
postorder → Inorder
Inorder → postorder
Binary tree with two different traversals ex) Inorder + postorder, Inorder+preorder //post+pre: impossible [Unique] inorder hdibeafcg postorder hidebfgca; last one is root [a] hdibe fcg hideb fgc; root is [b], [c], are children of [a] hdi e f g hide fg ; [f][g] are children of c, [d] [e] are children of [b] h i h i ; [h][i] are children of [d]

Binary Search Tree (BST) (ordered BT) BST properties:
Search: O(height); O(log n) when balanced, O(n) when linear list Sort: inorder traversal→자동으로 됨; O(n)
Degree 0 node(Leaf) :그냥 삭제
Degree 1 node(one child) Delete First, connect parent - child

Degree 2 node(two child) i) leftmost is leaf

//done
ii) leftmost is degree 1

Priority Queue Queue with Importance(priority) of KEY Insert: just insert Delete: Minimize+Delete →O(n)

//seems bit slow! →HEAP
Heap Tree-based Datastructure with HEAP property : priority(parent) < priority(Child) (min heap)
BINARY HEAP

Heapify 주어진 CBT를 Binary Heap으로 바꾸려면 그냥 모든 노드에 대해 check를 하면 되지만, 대신 nlogn이 걸리고 unique 하지 않다. →O(n)짜리 방법
