Gullu - Red Black Trees Flashcards

1
Q

ADT

A

abstract data type. Represents a collection of records. Records in an ADT are position oriented rather than value oriented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ADT Operations

A

Create: Create an empty table
Insert: item based on search key or position
Delete
Retrieve
Traverse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Implementations of RBT

A

Which operations are required
The frequency with which the operators will be used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Linear Implementations of RBT

A

Array or Pointer-Based (Array vs Linked List)
Appropriateness of this implementation depends on which operations will be used and how frequently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Invert and Traverse in no particular order (RBT)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Traverse in Sorted Order (RBT)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Traverse in sorted order and retrieve

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Traverse in sorted order, retrieve, insert, delete

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Binary Trees

A

way to index records, if tree is balanced, we have effective traversals, inserts and deletes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Binary Tree useful operations

A

Find Max/Min, Find, Delete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Rotations in an AVL Tree

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Simple AVL implementation

A

Recursively go down to find insertion point, then go up to find height info and rebalance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Properties of Red-Black Trees

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Shortest path RBT

A

only black nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Longest path RBT

A

alternating black and red nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Height of an RBT

A

height with n internal nodes is O(log2n)

17
Q

How many leaves in a binary tree

A

n+1 leaves if Binary tree contains N internal nodes

18
Q

General 2-3-4 tree with height h, we have bounds

A
  • Minimum leaves is 2^h and max is 4^h
  • N+1 leaves like merged RBT
  • 2^h < n+1
  • If we log both sides; h<log2(n+1)
  • Height of merged RBT represents shortest possible path length by property 4. Longest possible path is twice shortest.
  • For RBT having height h, we get h<2(log2(n+1))
19
Q

RBT Operations Time Complexity

A

O(log(2)n)

20
Q

Insert Strategy for RBT

A
  • Use normal BST to find insert location
  • Choice to insert red or black node
  • New black node violates property 4, red might violate property 3
  • We add red node
21
Q

Our insert strategy for RBTs

A
  • Always insert red node x
  • If this is the root, colour it black and we are done
  • If parent P is black, we are done
  • If parent is red, and uncle is black:
    • If relative to ground parent G, X is outside: Do a LL and recolour old G root as red and new node to black
      - If relative to ground parent G, X is inside: Do a LR and recolour as above.
  • If both parents and uncle are red, the scheme won’t work and we have to recursively fix both violations.
22
Q

Comparing AVL and RBTs

A

AVL Tree height is <= 1.44log2n
RBT height is <= 2log2n
AVLs are thus faster for searching

AVL Trees typically need a pass down to find location of a new item and another pass up to rebalance the tree

RBTs appear to need the same as above, but with the requirement of recursion. We can solve this with a top down insertion.

23
Q

Deletion in RBT

A
  • Red deletion is always fine
  • Black deletion may cause a problem, may need to fix on the way down
24
Q

Important Points RBT

A
  • AVL offers faster lookups
  • AVL only needs to store balancing info of each node
  • RBT only needs one extra info per node (colour)
  • Insertions and deletions in RBT are faster as fewer operations are needed