-
Notifications
You must be signed in to change notification settings - Fork 2
Binary Trees and BSTs
- Acyclic and connected.
- Rooted, directed and ordered.
Binary Tree T defined as
: T =
Tree Height
Empty tree has height of -1, so height
= max( height(
Tree Properties
-
Full : All nodes have either
0
orm
children. -
Perfect : Every leaf node is at the same level.
-
Complete : Perfect tree for every level except the leaves, where all leaves are pushed leftward.
Not every full tree is complete. Not every complete tree is full.
We use BSTs because can't run binary search on Linked List
- Height bound by n such that \(lg(n) \leq h \leq n\)
- Full, Complete and/or perfect
-
n
nodes in a Binary Tree means$n+1$ NULL pointers - BST has avg height of
$O(lg(n))$
Tree Analysis
- Best Case BST: Complete Tree
- Worst Case BST: Linked List Tree
- If inserting sorted list of elements, start at middle to avoid this outcome.
- All height-based BST operations have lower bound of
$\Omega$ (log(n)) time, and upper bound of$O(n)$ time - Min number of nodes in tree of height
$h$ :$log(n)$ - Max number of nodes in tree of height
$h$ :$n$
Traversals
-
Pre/Post/In Order Traversals
Always Left then Right. Only change where current node goes.
- Pre-Order: Self, L, R -- Stack (DFS)
- In-Order: L, Self, R
- Ordered list
- Post-Order: L, R, Self
-
Level-Order Traversal -- Queue (BFS)
- Enqueue root
- While queue isn't empty
- Dequeue and if element isn't NULL, print it and enqueue its children.
Traversals vs. Searches
A traversal visits each node exactly once. A search visits each node until find node being searched for.
An invalid in-order traversal is one that doesn't strictly ascend.
BFS : Visits all nodes on same level before going deeper.
DFS : Visits leaf nodes as quickly as possible.
Two Child Removal
- Find In Order Predecessor (rightmost element of left subtree)
- Swap IOP with node
- Delete removal node
Run Time Analysis
Operation | BST Avg. Case | BST Worst Case | Sorted Array | Sorted List |
---|---|---|---|---|
find() | O(log(n)) | O(n) | O(log(n)) *Binary Search | O(n) |
insert() | O(log(n)) | O(n) | O(n) | Given: O(1), Random: O(n) |
delete() | O(log(n)) | O(n) | O(n) | Given: O(1), Random: O(n) |
traverse() | O(n) | O(n) | O(n) | O(n) |