Class: BinarySearch::RedBlackTree
- Inherits:
-
Object
- Object
- BinarySearch::RedBlackTree
- Defined in:
- lib/binary_search/red_black_tree.rb,
lib/binary_search/red_black_tree/node.rb
Overview
Implements a Red-Black Tree, a self-balancing binary search tree
A Red-Black Tree is a type of self-balancing binary search tree that maintains balance through the use of node colors (red and black) and a set of properties:
-
Every node is either red or black.
-
The root is black.
-
Every leaf (NIL) is black.
-
If a node is red, then both its children are black.
-
For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
These properties ensure that the tree remains approximately balanced during insertions and deletions, guaranteeing O(log n) time complexity for basic operations like search, insert, and delete.
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#root ⇒ Node?
readonly
The root node of the tree.
Instance Method Summary collapse
-
#find(key) ⇒ Node?
Finds a node with the given key.
-
#initialize ⇒ RedBlackTree
constructor
Initializes an empty Red-Black Tree.
-
#insert(key) ⇒ void
Inserts a new key into the tree.
-
#remove(key) ⇒ Node?
Removes a key from the tree.
-
#update(old_key, new_key) ⇒ Boolean?
Updates a key in the tree.
Constructor Details
#initialize ⇒ RedBlackTree
Initializes an empty Red-Black Tree
24 25 26 |
# File 'lib/binary_search/red_black_tree.rb', line 24 def initialize @root = nil end |
Instance Attribute Details
Instance Method Details
#find(key) ⇒ Node?
Finds a node with the given key
This method performs a standard binary search tree lookup.
109 110 111 112 113 114 115 116 117 |
# File 'lib/binary_search/red_black_tree.rb', line 109 def find(key) current = @root while current comparison = key <=> current.key return current if comparison == 0 current = comparison < 0 ? current.left : current.right end nil end |
#insert(key) ⇒ void
This method returns an undefined value.
Inserts a new key into the tree
The insertion process involves:
-
Performing a standard BST insertion
-
Coloring the new node red
-
Rebalancing the tree to maintain Red-Black properties
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/binary_search/red_black_tree.rb', line 37 def insert(key) new_node = Node.new(key) if @root.nil? @root = new_node @root.color = :black else current = @root parent = nil while current parent = current comparison = key <=> current.key case comparison when -1 current = current.left when 1 current = current.right else # For duplicates, we'll add to the right current = current.right end end new_node.parent = parent comparison = key <=> parent.key if comparison <= 0 parent.left = new_node else parent.right = new_node end fix_insert(new_node) end end |
#remove(key) ⇒ Node?
Removes a key from the tree
The removal process involves:
-
Finding the node to be removed
-
If the node has two children, replacing it with its successor
-
Removing the node (or its successor)
-
Rebalancing the tree if the removed node was black
79 80 81 82 83 84 |
# File 'lib/binary_search/red_black_tree.rb', line 79 def remove(key) node = find(key) return nil unless node remove_node(node) end |
#update(old_key, new_key) ⇒ Boolean?
Updates a key in the tree
This operation ensures that the tree structure remains valid after updating a key. It’s implemented as a removal of the old key followed by an insertion of the new key.
94 95 96 97 98 99 100 101 |
# File 'lib/binary_search/red_black_tree.rb', line 94 def update(old_key, new_key) node = find(old_key) return nil unless node return false if find(new_key) node.key = new_key true end |