public void CreateBstFromArray() { var array = Simple42; BstNode FillSubTree(int min, int max) { var distance = max - min; if (distance < 0) { return(null); } else if (distance == 0) { return(new BstNode(array[min])); } else { var mid = min + distance / 2; var root = new BstNode(array[mid]); root.Left = FillSubTree(min, mid - 1); root.Right = FillSubTree(mid + 1, max); return(root); } } var tree = FillSubTree(0, array.GetUpperBound(0)); Debug.WriteLine(tree.Value); }
BstNode <T> Add(BstNode <T> node, T item) { if (node == null) { ++Count; return(new BstNode <T> { Key = item }); } var d = compare(item, node.Key); if (d == 0) { return(node); } if (d < 0) { node.TypedLeft = Add(node.TypedLeft, item); } else { node.TypedRight = Add(node.TypedRight, item); } return(node); }
public static BstNode FindInOrderSuccessor(BstNode node, BstNode root) { BstNode result = null; if (node.Right == null) { BstNode current = root; while (true) { if (current.Value == node.Value) { break; } if (current.Value > node.Value) { result = current; current = current.Left; } else if (current.Value < node.Value) { current = current.Right; } } } else { result = node.Right; while (result.Left != null) { result = result.Left; } } return(result); }
public bool AddUnique(T value) { if (Root == null) { Root = new BstNode <T>(value); return(true); } BstNode <T> node, lastNode; if (Find(Root, value, out node, out lastNode)) { return(false); } if (value.CompareTo(lastNode.Value) < 0) { lastNode.Left = new BstNode <T>(value); } else { lastNode.Right = new BstNode <T>(value); } return(true); }
public void Add(T value) { if (Root == null) { Root = new BstNode <T>(value); return; } BstNode <T> node, lastNode; if (Find(Root, value, out node, out lastNode)) { node.Right = new BstNode <T>(value) { Right = node.Right }; return; } if (value.CompareTo(lastNode.Value) < 0) { lastNode.Left = new BstNode <T>(value); } else { lastNode.Right = new BstNode <T>(value); } }
// Suppose t != null. void RemoveTarget(BstNode <T> t) { if (t.TypedLeft == null || t.TypedRight == null) { var c = t.TypedLeft ?? t.TypedRight; if (t.TypedParent == null) { Root = c; } else if (t.TypedParent.TypedLeft == t) { t.TypedParent.TypedLeft = c; } else { t.TypedParent.TypedRight = c; } } else { var t2 = t.SearchNextNode() as BstNode <T>; t.Key = t2.Key; RemoveTarget(t2); } }
private static void Main(string[] args) { BSTOperations operations = new BSTOperations(); operations.Insert(15); operations.Insert(20); operations.Insert(10); operations.Insert(25); operations.Insert(17); operations.Insert(8); operations.Insert(12); operations.Insert(5); operations.Insert(30); operations.Insert(40); operations.Insert(55); operations.Insert(7); operations.Insert(28); operations.Insert(22); System.Console.Write("DFS: "); BSTOperations.TraverseTreeDFS(BSTOperations.root); System.Console.WriteLine(); BSTOperations.TraverseBFS(BSTOperations.root); //Delete BSTOperations.Delete(BSTOperations.root, 20); System.Console.Write("DFS: "); BSTOperations.TraverseTreeDFS(BSTOperations.root); System.Console.WriteLine(); BSTOperations.TraverseBFS(BSTOperations.root); System.Console.WriteLine("Height: " + BSTOperations.GetHeight(BSTOperations.root)); int itemToFind = 25; var value = BSTOperations.FindItem(BSTOperations.root, itemToFind); if (value == -1) { System.Console.WriteLine("Item not found"); } else { System.Console.WriteLine("Item Found"); } BstNode min = BSTOperations.FindMin(BSTOperations.root); if (min != null) { System.Console.WriteLine("Min: " + min.Data); } BstNode max = BSTOperations.FindMax(BSTOperations.root); if (max != null) { System.Console.WriteLine("Max: " + max.Data); } System.Console.WriteLine("Is Binary search tree? " + BSTOperations.IsBinarySearchTree(BSTOperations.root)); }
public void add(int value) { if (this.root == null) { this.root = new BstNode(value); return; } this.add(value, this.root); }
public static void PostOrderTraversal(BstNode root) { if (root == null) { return; } PostOrderTraversal(root.left); // process the left PostOrderTraversal(root.right); // process the right Console.WriteLine("PostOrderTraversal at node {0}", root.data); // process the root }
private void TraversalPostOrder(BstNode <T> node, Action <BstNode <T> > action) { if (node == null) { return; } TraversalPostOrder(node.Left, action); TraversalPostOrder(node.Right, action); action(node); }
private int Depth(BstNode <T> node) { if (node == null) { return(0); } var leftDepth = Depth(node.Left); var rightDepth = Depth(node.Right); return(leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1); }
public void drawNode(BstNode node, Transform targetTransform) { BstNodeUi nodeUi = ObjectPool.Instantiate(NodePrefab, targetTransform).GetComponent <BstNodeUi>(); nodeUi.valueText.text = node.mValue.ToString(); if (node.rightNode != null) { drawNode(node.rightNode, nodeUi.rightTransform); } if (node.leftNode != null) { drawNode(node.leftNode, nodeUi.leftTransform); } }
public void TestFindInOrderSuccessor() { BstNode root = new BstNode(10); root.Left = new BstNode(5); root.Right = new BstNode(30); root.Right.Left = new BstNode(22); root.Right.Right = new BstNode(35); BstNode result = Problem133.FindInOrderSuccessor(root.Left, root); Assert.AreEqual(10, result.Value); result = Problem133.FindInOrderSuccessor(root.Right.Left, root); Assert.AreEqual(30, result.Value); result = Problem133.FindInOrderSuccessor(root.Right, root); Assert.AreEqual(35, result.Value); result = Problem133.FindInOrderSuccessor(root.Right.Right, root); Assert.AreEqual(null, result); result = Problem133.FindInOrderSuccessor(root, root); Assert.AreEqual(22, result.Value); }
public void Insert(ref BstNode node) { if (firstNode == null) { firstNode = node; return; } else { bool addToLeft; BstNode targetNode = findInsertionPoint(node.mValue, out addToLeft); node.parentNode = targetNode; if (addToLeft) { targetNode.leftNode = node; } else { targetNode.rightNode = node; } } }
public BstNode findInsertionPoint(int Amount, out bool addToLeft) { bool found = false; addToLeft = false; BstNode node = firstNode; while (!found) { if (Amount > node.mValue) { if (node.rightNode == null) { found = true; addToLeft = false; continue; } else { node = node.rightNode; } } else { if (node.leftNode == null) { found = true; addToLeft = true; continue; } else { node = node.leftNode; } } } return(node); }
private void add(int value, BstNode node) { // check right if (value >= node.value) { if (node.right == null) { node.right = new BstNode(value); return; } this.add(value, node.right); } // check left else { if (node.left == null) { node.left = new BstNode(value); return; } this.add(value, node.left); } }
private bool Find(BstNode <T> current, T value, out BstNode <T> node, out BstNode <T> lastNode) { var cmp = value.CompareTo(current.Value); if (cmp == 0) { node = current; lastNode = null; return(true); } if ((cmp < 0 && current.Left == null) || (cmp > 0 && current.Right == null)) { node = null; lastNode = current; return(false); } return(cmp < 0 ? Find(current.Left, value, out node, out lastNode) : Find(current.Right, value, out node, out lastNode)); }
private static void ValidateTheTree(BstNode <int> node) { Utility.TreeValidation(node); }
public BstNode(BstNode parent, int Value) { mValue = Value; nodesCount = 1; parentNode = parent; }
public Bst() { this.root = null; }
public BinarySearchTree(int FirstValue) { firstNode = new BstNode(FirstValue); }
public void AddToTree(int Value) { BstNode node = new BstNode(Value); Insert(ref node); }
public BstNode(int value) { this.value = value; left = null; right = null; }