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

Function is_balance

src/tree/binary/rb2.rs:358–373  ·  view source on GitHub ↗

does every path from the root to a leaf have the given number of black links?

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

Source from the content-addressed store, hash-verified

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 {
359 fn counter<K, V>(h: Option<NonNull<Node<K, V>>>, mut black: usize) -> bool {
360 if h.is_none() {
361 0 == black
362 } else {
363 let h = NodeQuery::new(h);
364 if !h.is_red() {
365 black -= 1;
366 }
367 counter(h.left().node, black) && counter(h.right().node, black)
368 }
369 }
370
371 let black = calc_blacks(root);
372 counter(root, black)
373}
374
375/// Assuming that h is red and both h.left and h.left.left
376/// are black, make h.left or one of its children red.

Callers

nothing calls this directly

Calls 2

calc_blacksFunction · 0.85
counterFunction · 0.85

Tested by

no test coverage detected