Exemple #1
0
 /// <summary>
 /// Traverse the first binary tree and put the value of each node into a hashmap
 /// </summary>
 /// <param name="root"> tree1 root node </param>
 /// <param name="hashMap"> new hashmap </param>
 /// <returns></returns>
 public static HashMap <int> HashTableTree1(DataStructures.Trees.Node <int> root, HashMap <int> hashMap)
 {
     hashMap.Set(root.Value, root.Value);
     if (root.Left != null)
     {
         HashTableTree1(root.Left, hashMap);
     }
     if (root.Right != null)
     {
         HashTableTree1(root.Right, hashMap);
     }
     return(hashMap);
 }
Exemple #2
0
 /// <summary>
 /// Traverse the second binary tree.  If collision occurs put double value into a list
 /// </summary>
 /// <param name="root"> tree 2 root node </param>
 /// <param name="hashMap"> hashMap with values from tree 1 </param>
 /// <param name="list"> empty list </param>
 /// <returns></returns>
 public static List <int> CompareTree2(DataStructures.Trees.Node <int> root, HashMap <int> hashMap, List <int> list)
 {
     if (hashMap.Contains(root.Value))
     {
         list.Add(root.Value);
     }
     if (root.Left != null)
     {
         CompareTree2(root.Left, hashMap, list);
     }
     if (root.Right != null)
     {
         CompareTree2(root.Right, hashMap, list);
     }
     return(list);
 }
Exemple #3
0
        /// <summary>
        /// Helper function to populate a BinaryTree object
        /// </summary>
        /// <returns> BinaryTree object </returns>
        private BinaryTree <char> PopulateBinaryTree()
        {
            BinaryTree <char> newTree = new BinaryTree <char>();

            DataStructures.Trees.Node <char> nodeA = new DataStructures.Trees.Node <char>('A');
            DataStructures.Trees.Node <char> nodeB = new DataStructures.Trees.Node <char>('B');
            DataStructures.Trees.Node <char> nodeC = new DataStructures.Trees.Node <char>('C');
            DataStructures.Trees.Node <char> nodeD = new DataStructures.Trees.Node <char>('D');
            DataStructures.Trees.Node <char> nodeE = new DataStructures.Trees.Node <char>('E');
            DataStructures.Trees.Node <char> nodeF = new DataStructures.Trees.Node <char>('F');

            nodeA.Left  = nodeB;
            nodeA.Right = nodeC;
            nodeB.Left  = nodeD;
            nodeB.Right = nodeE;
            nodeC.Left  = nodeF;

            newTree.Root = nodeA;
            return(newTree);
        }
Exemple #4
0
        /// <summary>
        /// Find the total value of all node values in a BinaryTree<int>
        /// </summary>
        /// <param name="tree"> BinaryTree<int> object </param>
        /// <returns> int total value </returns>
        public static int FindValue(BinaryTree <int> tree)
        {
            int total = 0;

            Queue <DataStructures.Trees.Node <int> > queue = new Queue <DataStructures.Trees.Node <int> >();

            queue.Enqueue(tree.Root);

            while (queue.Front != null)
            {
                DataStructures.Trees.Node <int> current = queue.Dequeue();
                total += current.Value;
                if (current.Left != null)
                {
                    queue.Enqueue(current.Left);
                }
                if (current.Right != null)
                {
                    queue.Enqueue(current.Right);
                }
            }
            return(total);
        }