/// <summary> /// determines the level of a binary tree /// </summary> /// <param name="root">the tree root</param> /// <returns>the level</returns> public static int FindTreeLevel(TreeNode root) { QueueForTrees methodQ = new QueueForTrees(); int levelAnswer = 1; methodQ.Enqueue(root); while (methodQ.Front != null) { if (methodQ.Front.Value.LeftChild != null || methodQ.Front.Value.RightChild != null) { levelAnswer++; } Node tempNode = methodQ.Dequeue(); if (tempNode.Value.LeftChild != null) { methodQ.Enqueue(tempNode.Value.LeftChild); } if (tempNode.Value.RightChild != null) { methodQ.Enqueue(tempNode.Value.RightChild); } } return(levelAnswer); }
public void TestPeek2() { QueueForTrees newQueue8 = new QueueForTrees(); Node returnNode4 = newQueue8.Peek(); Assert.Null(returnNode4); }
public static int ECSumOdd(TreeNode root) { QueueForTrees methodQueue = new QueueForTrees(); methodQueue.Enqueue(root); int sum = 0; while (methodQueue.Peek() != null) { if (methodQueue.Front.Value.Value % 2 == 1 || methodQueue.Front.Value.Value % 2 == -1) { sum = sum + methodQueue.Front.Value.Value; } Node temp = methodQueue.Dequeue(); if (temp.Value.LeftChild != null) { methodQueue.Enqueue(temp.Value.LeftChild); } if (temp.Value.RightChild != null) { methodQueue.Enqueue(temp.Value.RightChild); } } return(sum); }
/// <summary> /// determines if a value is an ancestor of another value in a binary tree /// </summary> /// <param name="A">is this an ancestor</param> /// <param name="B">this is the one we are checking if A was an ancestor</param> /// <param name="root">the binary tree</param> /// <returns></returns> public static bool IsAncestor(int A, int B, TreeNode root) { QueueForTrees methodQ = new QueueForTrees(); bool foundA = false; methodQ.Enqueue(root); while (methodQ.Front != null) { Node tempNode = methodQ.Dequeue(); if (tempNode.Value.Value == A) { foundA = true; } if (tempNode.Value.Value == B && foundA == true) { return(true); } if (tempNode.Value.LeftChild != null) { methodQ.Enqueue(tempNode.Value.LeftChild); } if (tempNode.Value.RightChild != null) { methodQ.Enqueue(tempNode.Value.RightChild); } } return(false); }
/// <summary> /// finds the max value in a tree /// </summary> /// <param name="root">the tree</param> /// <returns>the max value of the tree</returns> public static int FindMaxValue(TreeNode root) { QueueForTrees methodQueue = new QueueForTrees(); int maxValue = root.Value; methodQueue.Enqueue(root); while (methodQueue.Peek() != null) { if (methodQueue.Front.Value.Value > maxValue) { maxValue = methodQueue.Front.Value.Value; } Node temp = methodQueue.Dequeue(); if (temp.Value.LeftChild != null) { methodQueue.Enqueue(temp.Value.LeftChild); } if (temp.Value.RightChild != null) { methodQueue.Enqueue(temp.Value.RightChild); } } return(maxValue); }
public void TestEQ1() { TreeNode treeNode1 = new TreeNode(1); TreeNode treeNode2 = new TreeNode(2); Node node1 = new Node(treeNode1); QueueForTrees newQueue1 = new QueueForTrees(node1); newQueue1.Enqueue(treeNode2); Assert.Equal(2, newQueue1.Rear.Value.Value); }
public void TestDQ2() { TreeNode treeNode14 = new TreeNode(1); TreeNode treeNode15 = new TreeNode(2); Node node5 = new Node(treeNode14); QueueForTrees newQueue5 = new QueueForTrees(node5); newQueue5.Enqueue(treeNode15); Node returnNode1 = newQueue5.Dequeue(); Assert.Equal(1, returnNode1.Value.Value); }
public void TestEQ2() { TreeNode treeNode3 = new TreeNode(1); TreeNode treeNode4 = new TreeNode(2); TreeNode treeNode5 = new TreeNode(3); Node node2 = new Node(treeNode3); QueueForTrees newQueue2 = new QueueForTrees(node2); newQueue2.Enqueue(treeNode4); newQueue2.Enqueue(treeNode5); Assert.Equal(3, newQueue2.Rear.Value.Value); }
public void TestPeek1() { TreeNode treeNode18 = new TreeNode(1); TreeNode treeNode19 = new TreeNode(2); Node node7 = new Node(treeNode18); QueueForTrees newQueue7 = new QueueForTrees(node7); newQueue7.Enqueue(treeNode19); Node returnNode3 = newQueue7.Peek(); Assert.Equal(1, returnNode3.Value.Value); }
/// <summary> /// Compares two trees and itentify where they shares the same values /// </summary> /// <param name="root1">one tree</param> /// <param name="root2">second tree</param> /// <returns>a list of the values the two trees share</returns> public static List <int> TreeIntersection(BinaryTree root1, BinaryTree root2) { QueueForTrees algoQueue = new QueueForTrees(); Hashtable algoHashtable = new Hashtable(); List <int> returnAnswer = new List <int>(); string valForHT = ""; bool comparison; if (root1 == null || root2 == null) { return(returnAnswer); } TreeNode algoQT1Node = root1.root; algoQueue.Enqueue(algoQT1Node); while (algoQueue.Front != null) { Node temp = algoQueue.Dequeue(); algoHashtable.AddToHashTable(temp.Value.Value.ToString(), temp.Value.Value.ToString()); if (temp.Value.LeftChild != null) { algoQueue.Enqueue(temp.Value.LeftChild); } if (temp.Value.RightChild != null) { algoQueue.Enqueue(temp.Value.RightChild); } } //can re-use the queue because it wouldn't exit above while loop until it was empty TreeNode algoQT2Node = root2.root; algoQueue.Enqueue(algoQT2Node); while (algoQueue.Front != null) { Node temp = algoQueue.Dequeue(); valForHT = temp.Value.Value.ToString(); comparison = algoHashtable.HashTableContains(valForHT); if (comparison == true) { returnAnswer.Add(temp.Value.Value); } if (temp.Value.LeftChild != null) { algoQueue.Enqueue(temp.Value.LeftChild); } if (temp.Value.RightChild != null) { algoQueue.Enqueue(temp.Value.RightChild); } } return(returnAnswer); }
public void TestDQ3() { TreeNode treeNode16 = new TreeNode(1); TreeNode treeNode17 = new TreeNode(2); Node node6 = new Node(treeNode16); QueueForTrees newQueue6 = new QueueForTrees(node6); newQueue6.Enqueue(treeNode17); newQueue6.Dequeue(); Node returnNode2 = newQueue6.Dequeue(); Assert.Equal(2, returnNode2.Value.Value); }
public void TestPeek3() { TreeNode treeNode20 = new TreeNode(1); TreeNode treeNode21 = new TreeNode(2); Node node8 = new Node(treeNode20); QueueForTrees newQueue9 = new QueueForTrees(node8); newQueue9.Enqueue(treeNode21); newQueue9.Dequeue(); Node returnNode5 = newQueue9.Peek(); Assert.Equal(2, returnNode5.Value.Value); }
public void TestEQ3() { TreeNode treeNode6 = new TreeNode(1); TreeNode treeNode7 = new TreeNode(2); TreeNode treeNode8 = new TreeNode(3); TreeNode treeNode9 = new TreeNode(4); Node node3 = new Node(treeNode6); QueueForTrees newQueue3 = new QueueForTrees(node3); newQueue3.Enqueue(treeNode7); newQueue3.Enqueue(treeNode8); newQueue3.Enqueue(treeNode9); Assert.Equal(4, newQueue3.Rear.Value.Value); }
public void TestDQ1() { TreeNode treeNode10 = new TreeNode(1); TreeNode treeNode11 = new TreeNode(2); TreeNode treeNode12 = new TreeNode(3); TreeNode treeNode13 = new TreeNode(4); Node node4 = new Node(treeNode10); QueueForTrees newQueue4 = new QueueForTrees(node4); newQueue4.Enqueue(treeNode11); newQueue4.Enqueue(treeNode12); newQueue4.Enqueue(treeNode13); newQueue4.Dequeue(); Assert.Equal(2, newQueue4.Front.Value.Value); }
/// <summary> /// performs a breadth first traversal of a tree (meaning from top down and left to right /// </summary> /// <param name="root">Node that holds a treenode as its "value"</param> public static void BreadthFirst(Node root) { QueueForTrees methodQueue = new QueueForTrees(); methodQueue.Enqueue(root.Value); while (methodQueue.Peek() != null) { Console.WriteLine(methodQueue.Front.Value.Value); Node temp = methodQueue.Dequeue(); if (temp.Value.LeftChild != null) { methodQueue.Enqueue(temp.Value.LeftChild); } if (temp.Value.RightChild != null) { methodQueue.Enqueue(temp.Value.RightChild); } } }