private void Insert(int data, Binary_Search_Tree node, Binary_Search_Tree parent) { if (node.Key == null || node.Key == data) { node.Key = data; node.kvo++; node.Parent = parent; return; } if (node.Key > data) { if (node.Left == null) { node.Left = new Binary_Search_Tree(); } Insert(data, node.Left, node); } else { if (node.Right == null) { node.Right = new Binary_Search_Tree(); } Insert(data, node.Right, node); } }
private void Insert(Binary_Search_Tree insertNode, Binary_Search_Tree targetNode, Binary_Search_Tree parent) { if (targetNode.Key == null || targetNode.Key == insertNode.Key) { targetNode.Key = insertNode.Key; targetNode.kvo++; targetNode.Left = insertNode.Left; targetNode.Right = insertNode.Right; targetNode.Parent = parent; return; } if (targetNode.Key > insertNode.Key) { if (targetNode.Left == null) { targetNode.Left = new Binary_Search_Tree(); } Insert(insertNode, targetNode.Left, targetNode); } else { if (targetNode.Right == null) { targetNode.Right = new Binary_Search_Tree(); } Insert(insertNode, targetNode.Right, targetNode); } }
public void Insert(int data) { if (Key == null || Key == data) { Key = data; kvo++; return; } if (Key > data) { if (Left == null) { Left = new Binary_Search_Tree(); } Insert(data, Left, this); } else { if (Right == null) { Right = new Binary_Search_Tree(); } Insert(data, Right, this); } }
} //поперечный обход древа private void Traverse_infix(Binary_Search_Tree node) { if (node.Left != null) { Traverse_infix(node.Left); } Console.Write("( Key: " + node.Key + " Count: " + node.kvo + " )\n"); if (node.Right != null) { Traverse_infix(node.Right); } }
private bool?DetecktPosition(Binary_Search_Tree node) { if (node.Parent == null) { return(null); } if (node.Parent.Right == node) { return(true); } else { return(false); } }
public void Remove(int data) { Binary_Search_Tree binaryTree = Find(data); if (binaryTree == null) { Console.WriteLine("Удаление не было произведено"); return; } if (binaryTree.kvo < 1) { kvo--; return; } Remove(binaryTree); }
private Binary_Search_Tree Find(int data, Binary_Search_Tree node) { if (node == null) { Console.WriteLine("Искомый элемент не найден"); return(null); } if (node.Key == data) { return(node); } if (node.Key > data) { return(Find(data, node.Left)); } return(Find(data, node.Right)); }
static void Main(string[] args) { string problem = ""; while (problem != "exit") { Console.WriteLine("Select a Chapter (1, 2, 3) or 'exit' to leave: "); problem = Console.ReadLine(); switch (problem) { case "1": Chapter_1_problems ch = new Chapter_1_problems(); break; case "2": Chapter_2_problems ch2 = new Chapter_2_problems(); break; case "3": Chapter_3_problems ch3 = new Chapter_3_problems(); break; case "test": Binary_Search_Tree tree = new Binary_Search_Tree(); string input = Console.ReadLine(); while (input != "back") { if (input == "search") { input = Console.ReadLine(); int int_i = Convert.ToInt32(input); Console.WriteLine(tree.search(int_i)); } else { int input_i = Convert.ToInt32(input); tree.insert(input_i); } input = Console.ReadLine(); } break; } } }
public void Remove(Binary_Search_Tree node) { if (node == null) { return; } //вычисление относительно позиции родителя текущего элемента (лево-право) bool?myPositionLR = DetecktPosition(node); //------------------------------------------------------------------------- if (myPositionLR != null) { if (node.Left == null && node.Right == null) { if (myPositionLR == true) { node.Parent.Right = null; } else { node.Parent.Left = null; } return; } if (node.Right == null) { if (myPositionLR == true) { node.Parent.Right = node.Left; } else { node.Parent.Left = node.Left; } node.Left.Parent = node.Parent; return; } if (node.Left == null) { if (myPositionLR == true) { node.Parent.Right = node.Right; } else { node.Parent.Left = node.Right; } node.Right.Parent = node.Parent; return; } //Если присутствуют оба дочерних узла //то правый ставим на место удаляемого //а левый вставляем Insert() в правый if (myPositionLR == false) { node.Parent.Left = node.Right; node.Right.Parent = node.Parent; Insert(node.Left, node.Right, node.Right); } if (myPositionLR == true) { node.Parent.Right = node.Right; node.Right.Parent = node.Parent; Insert(node.Left, node.Right, node.Right); } } if (myPositionLR == null)//возможно в начало надо, потестит с однім єлементом { var BuffLeft = node.Left; Binary_Search_Tree buffRR = node.Right.Right; Binary_Search_Tree buffRL = node.Right.Left; node.Key = node.Right.Key; node.kvo = node.Right.kvo; node.Left = buffRL; node.Right = buffRR; if (node.Left != null) { node.Left.Parent = node; } if (node.Right != null) { node.Right.Parent = node; } if (BuffLeft != null) { Insert(BuffLeft, node, node); } } }