public int PointAddTesttTest(Point point, IBinaryTree <Point> tree) { tree.Add(point); var f = tree.Find(point); return(f); }
public int BookAddTestTest(Book.Logic.Book book, IBinaryTree <Book.Logic.Book> tree) { var f = tree.Find(book); tree.Add(book); return(f); }
/// <summary> /// Get depth of the tree. /// </summary> /// <typeparam name="T">The type of value the node contains.</typeparam> /// <param name="tree">A tree to calculte depth.</param> /// <returns>The depth of the tree.</returns> private static int GetDepthBinary <T>(IBinaryTree <T> tree) { int GetDepthInternal(IBinaryTree <T> root, int depth) { int result = depth; int tempDepth; if (root.LeftChild != null) { tempDepth = GetDepthInternal(root.LeftChild, depth + 1); if (tempDepth > result) { result = tempDepth; } } if (root.RightChild != null) { tempDepth = GetDepthInternal(root.RightChild, depth + 1); if (tempDepth > result) { result = tempDepth; } } return(result); } return(GetDepthInternal(tree, 1)); }
/// <summary> /// Verifies that a binary tree is no taller than necessary to store the data if it were optimally balanced. /// </summary> /// <param name="node">The root node.</param> /// <param name="count">The number of nodes in the tree. May be <c>null</c> if <see cref="IBinaryTree.Count"/> is functional.</param> internal static void VerifyHeightIsWithinTolerance(this IBinaryTree node, int?count = null) { // http://en.wikipedia.org/wiki/AVL_tree double heightMustBeLessThan = Math.Log(2, s_GoldenRatio) * Math.Log(Math.Sqrt(5) * ((count ?? node.Count) + 2), 2) - 2; Assert.True(node.Height < heightMustBeLessThan); }
public IBinaryTree FindPath(IBinaryTree tree) { SimpleTreeNode[,] nodes = tree.GetNodes(); int lines = nodes.GetLength(0); _preprocessedNodes = new SimpleTreeNode[lines, lines]; _largestValues = new SimpleTreeNode[lines, lines]; bool topIsEven = nodes[0, 0].Value % 2 == 0; bool lineCountIsEven = lines % 2 == 0; bool currentLineIsEven = topIsEven; for (int i = 0; i < lines; i++) { for (int j = 0; j <= i; j++) { _largestValues[i, j] = new SimpleTreeNode(nodes[i, j]); _preprocessedNodes[i, j] = new SimpleTreeNode() { Value = (nodes[i, j].Value % 2 == 0) == currentLineIsEven ? nodes[i, j].Value : (int?)null }; } currentLineIsEven = !currentLineIsEven; } for (int i = lines - 2; i >= 0; i--) { for (int j = 0; j <= i; j++) { int?left = _preprocessedNodes[i + 1, j].Value; int?rigth = _preprocessedNodes[i + 1, j + 1].Value; if (!left.HasValue) { _largestValues[i, j].Value += _largestValues[i + 1, j + 1].Value; nodes[i, j].PathDirection = 1; } else if (!rigth.HasValue) { _largestValues[i, j].Value += _largestValues[i + 1, j].Value; } else { if (_largestValues[i + 1, j].Value.Value > _largestValues[i + 1, j + 1].Value.Value) { _largestValues[i, j].Value += _largestValues[i + 1, j].Value; } else { _largestValues[i, j].Value += _largestValues[i + 1, j + 1].Value; nodes[i, j].PathDirection = 1; } } } } return(tree); }
public void DrawBinaryTree(IBinaryTree <TVal> tree, int offsetLeft, int offsetHigh) { if ((this.drawParas == null) || (tree == null) || (tree.Count == 0)) { return; } this.curTree = tree; this.offsetX = offsetLeft; this.offsetY = offsetHigh; try { this.drawParas.Graphic.Clear(this.drawParas.GraphicBackColor); Point[][] position = this.GetNodePosition(tree.Depth); Node <TVal> root = tree.Root; this.DrawTree(root, 0, 0, this.drawParas.Graphic, position, offsetLeft, offsetHigh); } catch (Exception ee) { ee = ee; } }
public void Reset() { _curr = null; _right = false; _begin = false; _stack.Clear(); }
public BinaryTreeDrawer() { this.drawParas = null; this.curTree = null; this.offsetX = 0; this.offsetY = 0; }
public void SetTree(E rootData, IBinaryTree <E> leftTree, IBinaryTree <E> rightTree) { CheckInitialization(); // TODO: Implement ME! throw new NotImplementedException(); }
public static string Print(IBinaryTree tree) { StringBuilder sb = new StringBuilder(1024); Stack<IBinaryTreeNode> nodes = new Stack<IBinaryTreeNode>(64); nodes.Push(tree.Root); while (nodes.Count > 0) { IBinaryTreeNode node = nodes.Pop(); if (node == NullBinaryTreeNode.Instance) { sb[sb.Length - 1] = ')'; sb.Append(" "); } else if (node == null) { sb.Append("N "); } else { sb.Append("(" + (node.Color == 1 ? "R" : "") + node.Value + " "); nodes.Push(NullBinaryTreeNode.Instance); nodes.Push(node.RightChild); nodes.Push(node.LeftChild); } } return sb.ToString(); }
/// <summary> /// Verifies that a binary tree is balanced according to AVL rules. /// </summary> /// <param name="node">The root node of the binary tree.</param> internal static void VerifyBalanced(this IBinaryTree node) { if (node.Left != null) { VerifyBalanced(node.Left); } if (node.Right != null) { VerifyBalanced(node.Right); } if (node.Right != null && node.Left != null) { Assert.InRange(node.Left.Height - node.Right.Height, -1, 1); } else if (node.Right != null) { Assert.InRange(node.Right.Height, 0, 1); } else if (node.Left != null) { Assert.InRange(node.Left.Height, 0, 1); } }
public static IBinaryTree <T> Create <T>( IBinaryTree <T> left, T item, IBinaryTree <T> right) { return(new Node <T>(left, item, right)); }
public void SecondFunctorLaw(IBinaryTree <int> tree) { string g(int i) => i.ToString(); bool f(string s) => s.Length % 2 == 0; Assert.Equal(tree.Select(g).Select(f), tree.Select(i => f(g(i)))); }
public void BinaryTree_01_GetRoot_1_OnEmptyTree() { // Arrange IBinaryTree <int> tree = DSBuilder.CreateBinaryTreeEmpty(); // Act & Assert Assert.Throws(typeof(BinarySearchTreeEmptyException), () => tree.GetRoot()); }
static BinaryTree() { theEmpty = new EmptyBinaryTree(); theHashCode = typeof(BinaryTree <V>).AssemblyQualifiedName.GetHashCode(); unchecked { theHashCode += typeof(V).AssemblyQualifiedName.GetHashCode(); } }
/// <summary> /// Constructs a BinaryTreeNode /// </summary> /// <param name="item"></param> public BinaryTreeNode(IBinaryTree <T> owner, T item) { Require.That(item != null, $"{nameof(item)} must not be null."); Require.ArgumentNotNull(owner, nameof(owner)); this.Value = item; this.Owner = owner; }
public BinaryTree(T value, IBinaryTree <T> leftChild = null, IBinaryTree <T> rightChild = null) { this.Value = value; this.LeftChild = leftChild; this.RightChild = rightChild; }
public int Depth(IBinaryTree <T> binaryTree) { if (binaryTree == this) { return(0); } return(1 + Depth(binaryTree.Parent)); }
public BinaryTree(IBinaryTree <T> Left, IBinaryTree <T> Right, T Data) { this.Left = Left; this.Right = Right; this.Data = Data; }
public void Merge(IBinaryTree <T> otherTree) { Root = new TreeNode <T> { Left = Root, Right = otherTree.Root }; }
/// <summary> /// Creates a new nodes enumerable for a binary tree /// </summary> /// <param name="tree">Binary Tree</param> public NodesEnumerable(IBinaryTree <TKey, TValue> tree) { if (tree == null) { throw new ArgumentNullException("tree", "Tree cannot be null"); } this._tree = tree; }
public void Test_Add() { IBinaryTree <byte> bt = this.CreateInstance(1); bt.Add(10); Assert.True(bt.Contains(10)); }
public void Test_Add_When_New() { IBinaryTree <byte> bt = this.CreateInstance(1); bt.Add(100); Assert.True(bt.Contains(100)); Assert.False(bt.IsEmpty); }
public void SetUp() { intBinaryTree = new BinaryTree <int>(new Node <int>(5)); intBinaryTree.Put(3); intBinaryTree.Put(7); for (int i = 0; i < 100; i++) { intBinaryTree.Put(randomSeed.Next(0, 100)); } }
// NLR public static void PreOrderTraversal <T>(IBinaryTree <T> binaryTree) { if (binaryTree == null) { return; } Console.Write(binaryTree.Data + " "); PreOrderTraversal(binaryTree.Left); PreOrderTraversal(binaryTree.Right); }
private IReadOnlyDictionary <string, char> GetCodesWithSymbols(IBinaryTree <char> binaryTree) { var codes = new Dictionary <string, char>(); foreach (var(symbol, code) in Search(binaryTree.Root, new LinkedList <char>())) { codes[code] = symbol; } return(codes); }
public void BinaryTree_03_MakeEmpty_GetRootIsNullAfterMakeEmpty() { // Arrange IBinaryTree <int> tree = DSBuilder.CreateBinaryTreeInt(); // Act tree.MakeEmpty(); // Assert Assert.Throws(typeof(BinarySearchTreeEmptyException), () => tree.GetRoot()); }
public BinaryTreeUnitTests() { this.binaryTree = new BinaryTree <string>("*", new BinaryTree <string>("+", new BinaryTree <string>("3"), new BinaryTree <string>("2")), new BinaryTree <string>("-", new BinaryTree <string>("9"), new BinaryTree <string>("6"))); }
private Dictionary <char, string> GetSymbolsWithCodes(IBinaryTree <char> binaryTree) { var codes = new Dictionary <char, string>(); foreach (var(symbol, code) in Search(binaryTree.Root, new LinkedList <char>())) { codes[symbol] = code; } return(codes); }
public static void Test() { var source = BinaryTree.Create(42, BinaryTree.Create(1337, BinaryTree.Leaf(0), BinaryTree.Leaf(-22)), BinaryTree.Leaf(100)); IBinaryTree <string> dest = source.Select(i => i.ToString()); IBinaryTree <string> dest2 = from i in source select i.ToString(); }
public void BinaryTree_02_Size_2_OnIntTree() { // Arrange IBinaryTree <int> tree = DSBuilder.CreateBinaryTreeInt(); int expected = 6; // Act int actual = tree.Size(); // Assert Assert.AreEqual(expected, actual); }