public void Create15NodeTree() { var tree = Bst.Create(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, new int[] { 8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15 }); Assert.AreEqual(8, tree.Value); Assert.AreEqual(4, tree.Left.Value); Assert.AreEqual(12, tree.Right.Value); Assert.AreEqual(2, tree.Left.Left.Value); Assert.AreEqual(1, tree.Left.Left.Left.Value); Assert.AreEqual(3, tree.Left.Left.Right.Value); Assert.AreEqual(6, tree.Left.Right.Value); Assert.AreEqual(5, tree.Left.Right.Left.Value); Assert.AreEqual(7, tree.Left.Right.Right.Value); Assert.AreEqual(10, tree.Right.Left.Value); Assert.AreEqual(9, tree.Right.Left.Left.Value); Assert.AreEqual(11, tree.Right.Left.Right.Value); Assert.AreEqual(14, tree.Right.Right.Value); Assert.AreEqual(13, tree.Right.Right.Left.Value); Assert.AreEqual(15, tree.Right.Right.Right.Value); }
Bst Deletion(Bst rootnode, int sayi) { if (rootnode == null) { return(rootnode); } if (sayi < rootnode.sayi) { rootnode.left = Deletion(rootnode.left, sayi); } else if (sayi > rootnode.sayi) { rootnode.right = Deletion(rootnode.right, sayi); } else { // bir child varsa if (rootnode.left == null) { return(rootnode.right); } else if (rootnode.right == null) { return(rootnode.left); } // iki child rootnode.sayi = minValue(rootnode.right); rootnode.right = Deletion(rootnode.right, rootnode.sayi); } return(rootnode); }
public int T2Sum() { var root = new TreeNodeIb(10) { right = new TreeNodeIb(20), left = new TreeNodeIb(9) }; var sum = 19; var sumIb = new Bst(root); var x1 = sumIb.Left(); var x2 = sumIb.Right(); while (x1 < x2) { if (x1 + x2 == sum) { return(1); } if (x1 + x2 < sum) { x1 = sumIb.Left(); } else { x2 = sumIb.Right(); } } return(0); }
public void StressTest() { Console.WriteLine("BSTTree Stress Test is running..."); var bstTree = new Bst <int>(); var rnd = new Random(); //Generate 20 random integer var ints = new int[9999999]; for (var i = 0; i < 9999999; i++) { ints[i] = rnd.Next(-2000000, 2000000); } foreach (var e in ints) { bstTree.Insert(e); } foreach (var e in ints) { var res = bstTree.TryFind(e); Assert.IsTrue(res.Found); Assert.AreEqual(res.Data, e); } foreach (var e in ints) { bstTree.Delete(e); } Console.WriteLine("BSTTree Stress Test has finished running..."); }
static void Main(string[] args) { Bst MyBst = new Bst(); MyBst.insert(15); MyBst.insert(13); MyBst.insert(19); MyBst.insert(9); MyBst.insert(14); MyBst.insert(17); MyBst.insert(21); MyBst.insert(8); MyBst.insert(11); MyBst.insert(16); MyBst.insert(18); MyBst.insert(20); MyBst.insert(23); MyBst.delete(19); MyBst.delete(18); MyBst.delete(17); MyBst.delete(16); MyBst.delete(20); MyBst.delete(21); MyBst.delete(23); }
public void VerticalSumOnlyRoot() { var result = Bst.VerticalSum(new Node(4)); Assert.AreEqual(result.Count, 1); Assert.AreEqual(result[0], 4); }
static void Main(string[] args) { Bst tree = new Bst(); tree.add(15); tree.add(5); tree.add(20); }
public void Create3NodeTree() { var tree = Bst.Create(new int[] { 1, 2, 3 }, new int[] { 2, 1, 3 }); Assert.AreEqual(2, tree.Value); Assert.AreEqual(1, tree.Left.Value); Assert.AreEqual(3, tree.Right.Value); }
public void CreateRootNodeOnlyTree() { var root = Bst.Create(new int[] { 1 }, new int[] { 1 }); Assert.IsNotNull(root); Assert.AreEqual(1, root.Value); Assert.IsNull(root.Left); Assert.IsNull(root.Right); }
public void Create5NodeTree() { var tree = Bst.Create(new int[] { 1, 2, 3, 4, 5 }, new int[] { 3, 2, 1, 4, 5 }); Assert.AreEqual(3, tree.Value); Assert.AreEqual(2, tree.Left.Value); Assert.AreEqual(1, tree.Left.Left.Value); Assert.AreEqual(4, tree.Right.Value); Assert.AreEqual(5, tree.Right.Right.Value); }
int minValue(Bst root) { int minv = root.sayi; while (root.left != null) { minv = root.left.sayi; root = root.left; } return(minv); }
public void DeletionTest() { Console.WriteLine("BSTTree Deletion Test is running..."); var bstTree = new Bst <int>(); var rnd = new Random(); //Generate 20 random integer var ints = new int[20000]; for (var i = 0; i < 20000; i++) { ints[i] = rnd.Next(-20000, 20000); } var counter = 0; foreach (var e in ints) { var res = bstTree.Insert(e); if (res) { counter++; } } var inOrderList = new List <int>(); foreach (var e in bstTree.InOrderTraverse()) { inOrderList.Add(e); } Assert.AreEqual(inOrderList.Count, counter); ValidateTheTree(bstTree.Root); //Now attempt to delete one node at a time //and verify tree validity after each deletion foreach (var e in ints) { var res = bstTree.Delete(e); if (res) { counter--; Assert.AreEqual(counter, bstTree.TreeSize); Assert.AreEqual(bstTree.InOrderTraverse().Count(), bstTree.TreeSize); Assert.IsFalse(bstTree.TryFind(e).Found); } if (bstTree.Root != null) { ValidateTheTree(bstTree.Root); } } Assert.IsNull(bstTree.Root); Assert.AreEqual(0, bstTree.TreeSize); Console.WriteLine("BSTTree Deletion Test has finished running..."); }
private void inOrder(Bst node) { if (node.left != null) { inOrder(node.left); } inlstbox.Items.Add(node.sayi); if (node.right != null) { inOrder(node.right); } }
public void PreOrder(Bst Node) { prelistbox.Items.Add(Node.sayi); if (Node.left != null) { PreOrder(Node.left); } if (Node.right != null) { PreOrder(Node.right); } }
private void PostOrder(Bst node) { if (node.left != null) { PostOrder(node.left); } if (node.right != null) { PostOrder(node.right); } postlstbox.Items.Add(node.sayi); }
public void VerticalSum7Nodes() { var tree = Bst.Create(new int[] { 1, 2, 3, 4, 5, 6, 7 }, new int[] { 4, 2, 1, 3, 6, 5, 7 }); var expectedResult = new SortedList <int, int>() { { -2, 1 }, { -1, 2 }, { 0, 12 }, { 1, 6 }, { 2, 7 }, }; var result = Bst.VerticalSum(tree); AssertEquals(expectedResult, result); }
public void FindTest() { Console.WriteLine("BSTTree Finding Test is running..."); var bstTree = new Bst <int>(); var rnd = new Random(); //Generate 20 random integer var ints = new int[20000]; for (var i = 0; i < 20000; i++) { ints[i] = rnd.Next(-20000, 20000); } var counter = 0; foreach (var e in ints) { var res = bstTree.Insert(e); if (res) { counter++; } } var inOrderList = new List <int>(); foreach (var e in bstTree.InOrderTraverse()) { inOrderList.Add(e); } Assert.AreEqual(inOrderList.Count, counter); ValidateTheTree(bstTree.Root); foreach (var e in ints) { var res = bstTree.TryFind(e); Assert.IsTrue(res.Found); Assert.AreEqual(res.Data, e); } //Generate some random number outside the range of //the tree and try to find. Expected false return. for (var i = 0; i < 100; i++) { var res = bstTree.TryFind(rnd.Next(21000, 50000)); Assert.IsFalse(res.Found); } Console.WriteLine("BSTTree Finding Test has finished running..."); }
public static void Client() { Tree temp; var root = temp = null; Console.WriteLine("Create Tree:"); var bst = new Bst(); #region create tree bst.InsertChild(ref root, 9); bst.InsertChild(ref root, 4); bst.InsertChild(ref root, 15); bst.InsertChild(ref root, 6); bst.InsertChild(ref root, 12); bst.InsertChild(ref root, 17); bst.InsertChild(ref root, 2); #endregion #region print Console.WriteLine("Pre Order Display"); // actual tree bst.Print_Preorder(root); Console.WriteLine("In Order Display"); bst.Print_Inorder(root); Console.WriteLine("Post Order Display"); bst.Print_Postorder(root); #endregion #region searching var val = 15; temp = bst.Search(ref root, val); if (temp != null && bst.IsFound) { Console.WriteLine("Searched node = {0}", val); } else { Console.WriteLine("Data not found in tree."); } #endregion #region delete bst.DeleteTree(ref root); #endregion }
public void CreateRightSkewedTree() { var tree = Bst.Create(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3, 4, 5 }); Assert.AreEqual(1, tree.Value); Assert.AreEqual(2, tree.Right.Value); Assert.AreEqual(3, tree.Right.Right.Value); Assert.AreEqual(4, tree.Right.Right.Right.Value); Assert.AreEqual(5, tree.Right.Right.Right.Right.Value); Assert.IsNull(tree.Left); Assert.IsNull(tree.Right.Left); Assert.IsNull(tree.Right.Right.Left); Assert.IsNull(tree.Right.Right.Right.Left); Assert.IsNull(tree.Right.Right.Right.Right.Right); }
static void Main(string[] args) { int i = 1; while (++i > 0 && i < 5) { Console.WriteLine(i); } Bst<int> bst = new Bst<int>(new BinNode<int>(15)); bst.InsertAsLC(bst.Root(), 10); bst.InsertAsRC(bst.Root(), 20); var aaa = bst.Search(21); bst.Insert(21); Console.ReadLine(); }
private void FindNode(int sayi, Bst node, ListBox x) { count++; if (node.sayi == 0) { x.Items.Add("boş"); } else if ((sayi < node.sayi) && (node.left != null)) { FindNode(sayi, node.left, x); } else if ((sayi > node.sayi) && (node.right != null)) { FindNode(sayi, node.right, x); } else if (sayi == node.sayi) { if (node.sayi == sayi) { x.Items.Add(sayi + " bulundu"); x.Items.Add("Depth : " + count); if (node.right != null && node.left != null) { x.Items.Add("children : " + node.left.sayi + " " + node.right.sayi); } else if (node.right == null && node.left != null) { x.Items.Add("child : " + node.left.sayi); } else if (node.right != null && node.left == null) { x.Items.Add("children : " + node.right.sayi); } else { x.Items.Add("Children : NULL"); } } else { sonuclstbox.Items.Add("Sayı bulunamadı"); } } }
static string Substract(string from, string value) { var bsd = new Bst <char>(); foreach (var ch in value) { bsd.AddUnique(ch); } var builder = new StringBuilder(); foreach (var ch in from) { if (!bsd.Contains(ch)) { builder.Append(ch); } } return(builder.ToString()); }
public void InsertionTest() { Console.WriteLine("BSTTree Insertion Test is running..."); var bstTree = new Bst <int>(); var rnd = new Random(); //Generate 20 random integer var ints = new int[20000]; for (var i = 0; i < 20000; i++) { ints[i] = rnd.Next(-20000, 20000); } var counter = 0; foreach (var e in ints) { var res = bstTree.Insert(e); if (res) { counter++; Assert.AreEqual(counter, bstTree.TreeSize); Assert.AreEqual(bstTree.InOrderTraverse().Count(), bstTree.TreeSize); ValidateTheTree(bstTree.Root); } } Assert.AreEqual(counter, bstTree.TreeSize); var inOrderList = new List <int>(); foreach (var e in bstTree.InOrderTraverse()) { inOrderList.Add(e); } Assert.AreEqual(inOrderList.Count, counter); ValidateTheTree(bstTree.Root); bstTree.ClearTheTree(); Assert.IsNull(bstTree.Root); Console.WriteLine("BSTTree Insertion Test has finished running..."); }
private static bool HasDublicates(int[] array) { if (array == null) { throw new ArgumentNullException(); } if (array.Length == 0) { return(false); } var bsd = new Bst <int>(); for (var i = 1; i < array.Length; i++) { if (!bsd.AddUnique(array[i])) { return(true); } } return(false); }
private void find(int sayi, Bst node) { if (node.sayi == 0) { label4.Text = "Boş ağaç"; } else if ((sayi < node.sayi) && (node.left != null)) { find(sayi, node.left); } else if ((sayi > node.sayi) && (node.right != null)) { find(sayi, node.right); } else if (sayi == node.sayi) { label4.Text = "1"; } else { label4.Text = "2"; } }
private void AddTree() { Bst yeninode = new Bst(Convert.ToInt32(EkleTxtbox.Text), null, null); if (Node.sayi == 0) { Node.sayi = Convert.ToInt32(EkleTxtbox.Text); } else { Bst current = Node; Bst parent; while (true) { parent = current; if (yeninode.sayi < current.sayi) { current = current.left; if (current == null) { parent.left = yeninode; break; } } else { current = current.right; if (current == null) { parent.right = yeninode; break; } } } } }
public void findParent(Bst node, int key, int parent) { if (node == null) { return; } if (node.sayi == key) { if (node.sayi == Node.sayi) { sonuclstbox.Items.Add("Root node"); } else { } } else { findParent(node.left, key, node.sayi); findParent(node.right, key, node.sayi); } }
public void PreOrderTraversal() { var bst = new Bst<string, string>(); bst.Put("f", "f"); bst.Put("b", "b"); bst.Put("g", "g"); bst.Put("a", "a"); bst.Put("d", "d"); bst.Put("i", "i"); bst.Put("c", "c"); bst.Put("e", "e"); bst.Put("h", "h"); var values = bst.PreOrderTraversal().ToList(); Assert.That(values.Count, Is.EqualTo(9)); Assert.That(values[0], Is.EqualTo("f")); Assert.That(values[1], Is.EqualTo("b")); Assert.That(values[2], Is.EqualTo("a")); Assert.That(values[3], Is.EqualTo("d")); Assert.That(values[4], Is.EqualTo("c")); Assert.That(values[5], Is.EqualTo("e")); Assert.That(values[6], Is.EqualTo("g")); Assert.That(values[7], Is.EqualTo("i")); Assert.That(values[8], Is.EqualTo("h")); }
public void PutThenGetGetValue() { var bst = new Bst<string, string>(); bst.Put("test", "v1"); var foundValue = bst.Get("test"); Assert.That(foundValue, Is.EqualTo("v1")); }
public void PutKeyTwiceGetNewValue() { var bst = new Bst<string, string>(); bst.Put("test", "v1"); bst.Put("test", "v2"); var foundValue = bst.Get("test"); Assert.That(foundValue, Is.EqualTo("v2")); }
public void PutDoesntThrow() { var bst = new Bst<string, string>(); Assert.That(() => bst.Put("test", "v1"), Throws.Nothing); }
public RunSplay() { _bst = new SplayTree <int>(); InitTree(); }
public RunBst() { bst = new Bst <int>(); InitBst(); }
public void GetNotAddedReturnsNull() { var bst = new Bst<string, string>(); var val = bst.Get("test"); Assert.That(val, Is.Null); }
public void CreateNullTree() { Assert.IsNull(Bst.Create(new int[] { }, new int[] { })); }
public Bst(int sayi, Bst left, Bst right) { this.sayi = sayi; this.left = left; this.right = right; }