Gullu - Red Black Trees Flashcards
ADT
abstract data type. Represents a collection of records. Records in an ADT are position oriented rather than value oriented.
ADT Operations
Create: Create an empty table
Insert: item based on search key or position
Delete
Retrieve
Traverse
Implementations of RBT
Which operations are required
The frequency with which the operators will be used
Linear Implementations of RBT
Array or Pointer-Based (Array vs Linked List)
Appropriateness of this implementation depends on which operations will be used and how frequently.
Invert and Traverse in no particular order (RBT)
Operations needed: Insert item in list, list/traverse items there
Insert is efficient: Array based (Add at the end and increment counter) and Pointer Based (Just replace head or tail). Operations are O(1). Ex. Appending to log file.
Traverse in Sorted Order (RBT)
Retrieves, inserts and deletes occur very infrequently. We can load the list and sort it when it changes. Arrays do not offer any advantage over pointer-based solutions. Only consideration is whether the size of the list is known beforehand or not.
Ex. Print catalogue for every visitor
Traverse in sorted order and retrieve
Infrequent inserts and deletes. Sort after insert or delete. Array based, using binary search to retrieve by key effectively. Pointer based, so we must scan to find the middle which is inefficient.
Ex. A lookup table
Traverse in sorted order, retrieve, insert, delete
Inserting and deleting: Find position of new item, insertion will cause a shift up of operation and deleting will cause a shift down. Pointer based solution is impractical cause of retrieve.
Binary Trees
way to index records, if tree is balanced, we have effective traversals, inserts and deletes.
Binary Tree useful operations
Find Max/Min, Find, Delete
Rotations in an AVL Tree
- Minimum overhead due to low height
- For any node in the tree, the height of left and right subtrees can differ by at most one. The height of an empty tree is -1.
- After inserts/deletes, the shape is monitored to see if it conforms to AVL conditions
- Height of the tree repaired using rotation operations.
Simple AVL implementation
Recursively go down to find insertion point, then go up to find height info and rebalance.
Properties of Red-Black Trees
- Nodes are coloured Red or Black
- The root is black
- Children of a red node must be black
- Every path from root to null leaves has the same number of black nodes
- Null leaves are black.
Shortest path RBT
only black nodes
Longest path RBT
alternating black and red nodes