protected int min_value(binary_node <int> node) { while (node.left != null) { node = node.left; } return(node.data); }
protected binary_node <int> right_right(binary_node <int> i) { binary_node <int> temp_parent = i.right; temp_parent.right = i.right.right; i.right = temp_parent.left; temp_parent.left = i; return(temp_parent); }
private void check_conditions(binary_node <int> temp) { List <binary_node <int> > path_node = path(temp); binary_node <int> parent = path_node[path_node.Count - 2]; if (parent.Colors == black) { return; } binary_node <int> grand_parent = path_node[path_node.Count - 3]; if (parent.Colors == red) { binary_node <int> .Colour side_color = black; binary_node <int> side = new binary_node <int>(0); (side_color, side) = sibling_color(parent, grand_parent); if (side_color == black) { binary_node <int> temp_child = do_rotations(grand_parent, temp); binary_node <int> temp_parent = new binary_node <int>(0); if (grand_parent == root) { root = temp_child; } else { temp_parent = path_node[path_node.Count - 4]; if (temp_parent.data < temp_child.data) { temp_parent.right = temp_child; } else { temp_parent.left = temp_child; } } temp_child.Colors = black; if (temp_child.left.data == grand_parent.data) { temp_child.left.Colors = red; } else { temp_child.right.Colors = red; } root.Colors = black; } else { side.Colors = black; grand_parent.Colors = red; parent.Colors = black; root.Colors = black; color_check(); } } }
public int height(binary_node <T> node) { if (node == null) { return(0); } else { return(Mathf.Max(height(node.left), height(node.right)) + 1); } }
private void inorder_traversal(binary_node <T> node) { if (node != null) { inorder_traversal(node.left); data += node.data; data += " "; inorder_traversal(node.right); } else { return; } }
private void preorder_traversal(binary_node <T> node) { if (node != null) { data += node.data; data += " "; data += node.Colors; data += " "; preorder_traversal(node.left); preorder_traversal(node.right); } else { return; } }
void extra_remove() { Queue <binary_node <int> > qu = new Queue <binary_node <int> >(); qu.Enqueue(root); List <binary_node <int> > arr = new List <binary_node <int> >(); while (qu.Count > 0) { binary_node <int> temp = qu.Dequeue(); if (temp != null) { qu.Enqueue(temp.left); qu.Enqueue(temp.right); arr.Add(temp); } } foreach (binary_node <int> temp in arr) { correct_bf(path(temp)); } }
private (binary_node <int> .Colour, binary_node <int>) sibling_color(binary_node <int> temp, binary_node <int> parent) { binary_node <int> side; //print(parent.data + " " + temp.data); if (parent.left == temp) { side = parent.right; } else { side = parent.left; } if (side == null) { return(black, null); } else { return(side.Colors, side); } }
protected List <binary_node <int> > path(binary_node <int> node) { List <binary_node <int> > arr = new List <binary_node <int> >(); binary_node <int> main_node = root; while (main_node != null) { if (node.data == main_node.data) { arr.Add(main_node); return(arr); } if (node.data < main_node.data) { arr.Add(main_node); main_node = main_node.left; } else { arr.Add(main_node); main_node = main_node.right; } } return(arr); }
void Splaying(binary_node <int> node) { if (node == root) { return; } List <binary_node <int> > path_node = path(node); binary_node <int> temp = new binary_node <int>(0); binary_node <int> temp2 = new binary_node <int>(0); binary_node <int> gp = new binary_node <int>(0); while (root != node) { if (root.right == node) { root = right_right(root); } else if (root.left == node) { root = left_left(root); } else { if (path_node.Count < 4) { temp = root; gp = temp; } else { temp = path_node[path_node.Count - 4]; gp = path_node[path_node.Count - 3]; } if (gp.data > node.data) { if (gp.left.left == node) { temp2 = left_left(gp); temp2 = left_left(temp2); } else if (gp.left.right == node) { temp2 = left_right(gp); } } else { if (gp.right.right == node) { temp2 = right_right(gp); temp2 = right_right(temp2); } else if (gp.right.left == node) { temp2 = right_left(gp); } } if (temp == root && path_node.Count <= 3) { root = node; } else { if (temp.data < node.data) { temp.right = temp2; } else if (temp.data > node.data) { temp.left = temp2; } path_node = path(node); } } } }
private void remove_conditions(List <binary_node <int> > temp_path, binary_node <int> main_node) { if (main_node == root) { main_node.Colors = black; return; } binary_node <int> .Colour side_color = black; binary_node <int> parent = temp_path[temp_path.Count - 2]; binary_node <int> grand_parent; binary_node <int> side = new binary_node <int>(0); if (parent == root) { grand_parent = null; } else { grand_parent = temp_path[temp_path.Count - 3]; } (side_color, side) = sibling_color(main_node, temp_path[temp_path.Count - 2]); bool first_cond = false; if (side == null) { first_cond = true; } else if (side_color == black) { if ((side.left == null) && (side.right == null)) { first_cond = true; } else if (side.left != null) { if ((side.right == null) && (side.left.Colors == black)) { first_cond = true; } } if (side.right != null) { if ((side.left == null) && (side.right.Colors == black)) { first_cond = true; } else if ((side.right.Colors == black) && (side.left.Colors == black)) { first_cond = true; } } } if (first_cond) { if (parent.Colors == red) { main_node.Colors = black; parent.Colors = black; side.Colors = red; return; } else { side.Colors = red; main_node.Colors = black; remove_conditions(path(parent), parent); } } else if (side_color == red) { binary_node <int> temp; parent.Colors = red; side.Colors = black; if (parent.left == main_node) { temp = right_right(parent); } else { temp = left_left(parent); } if (parent == root) { temp = root; } else if (grand_parent.data > temp.data) { grand_parent.left = temp; } else { grand_parent.right = temp; } main_node.Colors = black; root.Colors = black; remove_conditions(path(main_node), main_node); } else if (side_color == black) { side.Colors = red; binary_node <int> away; binary_node <int> close; binary_node <int> temp; if (parent.left == main_node) { close = side.left; away = side.right; } else { close = side.right; away = side.left; } if (away == null) { away = new binary_tree <int> .binary_node <int>(0); } if (close == null) { close = new binary_tree <int> .binary_node <int>(0); } if (away.Colors == black && close.Colors == red) { close.Colors = black; side.Colors = red; if (parent.left == side) { temp = right_right(side); } else { temp = right_right(side); } if (parent == root) { temp = root; } else if (parent.data > temp.data) { parent.left = temp; } else { parent.right = temp; } root.Colors = black; remove_conditions(path(main_node), main_node); return; } side.Colors = parent.Colors; parent.Colors = black; if (parent.left == side) { temp = left_left(parent); } else { temp = right_right(parent); } if (parent == root) { root = temp; } else if (grand_parent.data > temp.data) { grand_parent.left = temp; } else { grand_parent.right = temp; } away.Colors = black; root.Colors = black; } }
private void remove_conditions(List <binary_node <int> > temp_path) { binary_node <int> main_node = temp_path[temp_path.Count - 1]; if (main_node == root && height(main_node) == 1) { root = null; return; } else if (main_node.Colors == red && height(main_node) == 1) { binary_node <int> base_node = temp_path[temp_path.Count - 2]; int temp_data = main_node.data; if (base_node.data > temp_data) { base_node.left = null; } else { base_node.right = null; } main_node = null; return; } else { int temp_data = 0; binary_node <int> temp_node = null; if (height(main_node) > 1 && main_node.right != null) { temp_data = min_value(main_node.right); temp_node = main_node.right; } if (temp_node == null && main_node.left != null) { temp_data = max_value(main_node.left); temp_node = main_node.left; } if (height(main_node) == 1) { temp_node = main_node; temp_data = main_node.data; } if (height(main_node) > 1) { remove_conditions(path(new binary_node <int>(temp_data))); main_node.data = temp_data; return; } remove_conditions(path(temp_node), temp_node); List <binary_node <int> > temp_path_remove = path(new binary_node <int>(temp_data)); binary_node <int> base_node; if (temp_path_remove.Count < 2) { root = temp_path_remove[0]; base_node = root; } else { base_node = temp_path_remove[temp_path_remove.Count - 2]; } if (base_node.data > temp_data) { base_node.left = null; } else { base_node.right = null; } return; } }
public binary_tree(binary_node <T> root_node) { root = root_node; }
public binary_tree(T root_data) { root = new binary_node <T>(root_data); root.Colors = binary_node <T> .Colour.black; }
public void set_right(T data) { this.right = new binary_node <T>(data); }
public void set_left(T data) { this.left = new binary_node <T>(data); }