MCPcopy Create free account
hub / github.com/douchuan/algorithm / balance

Function balance

src/tree/binary/rb2.rs:343–355  ·  view source on GitHub ↗

restore red-black tree invariant

(h: Option<NonNull<Node<K, V>>>)

Source from the content-addressed store, hash-verified

341
342/// restore red-black tree invariant
343fn balance<K, V>(h: Option<NonNull<Node<K, V>>>) -> Option<NonNull<Node<K, V>>> {
344 let mut h = NodeQuery::new(h);
345 if h.right().is_red() && !h.left().is_red() {
346 h.node = rotate_left(h.node);
347 }
348 if h.left().is_red() && h.left().left().is_red() {
349 h.node = rotate_right(h.node);
350 }
351 if h.left().is_red() && h.right().is_red() {
352 flip_colors(h.node);
353 }
354 h.node
355}
356
357/// does every path from the root to a leaf have the given number of black links?
358fn is_balance<K, V>(root: Option<NonNull<Node<K, V>>>) -> bool {

Callers 4

putFunction · 0.85
del_minFunction · 0.85
del_maxFunction · 0.85
deleteFunction · 0.85

Calls 6

flip_colorsFunction · 0.85
is_redMethod · 0.80
rotate_leftFunction · 0.70
rotate_rightFunction · 0.70
rightMethod · 0.45
leftMethod · 0.45

Tested by

no test coverage detected