public static void Do() { Tree root = new TreeNode { Left = new TreeNode { Left = new TreeLeaf { Value = 1 }, Right = new TreeLeaf { Value = 2 } }, Right = new TreeLeaf { Value = 3 } }; SumTreeVisitor visitor = new SumTreeVisitor(); root.Accept(visitor); Console.WriteLine("Suma wartości na drzewie to {0}", visitor.Sum); Console.ReadLine(); }
public void bigTreeHeightTest() { // r // / \ // a b // / \ // c d // / \ // e f // | \ // g h Tree root = new TreeNode() // r { left = new TreeNode() // a { left = new TreeNode() // c { left = new TreeNode() // e { left = new TreeLeaf(), // g right = new TreeLeaf() }, // h right = new TreeLeaf() }, // f right = new TreeLeaf() }, // d right = new TreeLeaf() // b }; HeightVisitor visitor = new HeightVisitor(); root.Accept(visitor); Assert.AreEqual(4, visitor.height); }
public QueryFilter Build(TreeNode treeNode) { if (treeNode != null) { this.isQueryFilterValid = true; treeNode.Accept(this); if (this.isQueryFilterValid && this.CurrentLevelFilters.Count > 0) { return(this.CurrentLevelFilters[0]); } } return(null); }
public void heightTest() { // r // l r // ll lr Tree root = new TreeNode() { left = new TreeNode() { left = new TreeLeaf(), right = new TreeLeaf() }, right = new TreeLeaf() }; HeightVisitor visitor = new HeightVisitor(); root.Accept(visitor); Assert.AreEqual(2, visitor.height); }