Ejemplo n.º 1
0
            // Recursively locates an empty slot in the binary tree and inserts the node


            //add
            private void add(TTreeNode node, ref TTreeNode tree)
            {
                if (tree == null)
                {
                    tree = node;
                }
                else
                {
                    // If we find a node with the same name then it's
                    // a duplicate and we can't continue
                    int comparison = DateTime.Compare(node.name, tree.name);
                    if (comparison == 0)
                    {
                        throw new Exception();
                    }

                    if (comparison < 0)
                    {
                        add(node, ref tree.left);
                    }
                    else
                    {
                        add(node, ref tree.right);
                    }
                }
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Find name in tree. Return a reference to the node
            /// if symbol found else return null to indicate failure.
            /// </summary>
            /// <param name="name">Name of node to locate</param>
            /// <returns>Returns null if it fails to find the node, else returns reference to node</returns>

            //find
            public TTreeNode findSymbol(DateTime name)
            {
                TTreeNode np = root;
                int       cmp;

                while (np != null)
                {
                    cmp = DateTime.Compare(name, np.name);
                    if (cmp == 0)   // found !
                    {
                        return(np);
                    }

                    if (cmp < 0)
                    {
                        np = np.left;
                    }
                    else
                    {
                        np = np.right;
                    }

                    SearchAlgorithms.GlobalCounter++;//Count the reps
                }

                return(null);  // Return null to indicate failure to find name
            }
Ejemplo n.º 3
0
 // Constructor  to create a single node
 public TTreeNode(DateTime name, int d)
 {
     this.name = name;
     value     = d;
     left      = null;
     right     = null;
 }
Ejemplo n.º 4
0
        public List <StoredData> searchByDateA2(List <StoredData> sortedListToSearchA2, DateTime dateToFind)
        {
            int countOfRepetitions = 0; // use this to hold the number of repetitions your algorithm has to make to search the data
            //#####################################################################################################
            //Place your second search algorithm below here - the results must be in the List 'searchResults'
            //This will be displayed on the right-hand-side of the console window
            //#####################################################################################################
            List <StoredData> searchResults = new List <StoredData>();

            //Binary Tree
            //Average case: O(log n)
            //Worst case: O(n)



            TBinarySTree <DateTime> bt = new TBinarySTree <DateTime>();


            //insert the data into the tree
            for (int i = 0; i < sortedListToSearchA2.Count; i++)
            {
                bt.insert(sortedListToSearchA2[i].TxDate, i);
            }

            // Retrieve data from the tree with its index number
            TTreeNode symbol = bt.findSymbol(dateToFind);

            if (symbol != null)
            {
                searchResults.Add(sortedListToSearchA2[symbol.value]);
            }

            else if (symbol == null) //if date cannot be found, adds an empty StoredData object to results list
            {
                StoredData newObj = new StoredData("Not Found", DateTime.Now, 0, 0, 0, 0);
                searchResults.Add(newObj);
            }

            countOfRepetitions = SearchAlgorithms.GlobalCounter;

            //#####################################################################################################
            //Place your second search algorithm above here - the results must be in the List 'searchResults'
            //#####################################################################################################
            // *** your search result data should be placed in 'searchResults'
            searchResults.First().SearchTypeAndTime = "Using 'Binary Tree Search A2' search by date"; //rewrite the text to show user which algorithm you have used
            // place your total count of loops (countOfRepetitions) here
            searchResults.First().CountRepetitions = countOfRepetitions;                              //rewrite to show user the number of steps (loops) this algorithm has made to sort the data
            return(searchResults);
        }
Ejemplo n.º 5
0
        //A recursive method which is used to transform the tree nodes
        // into a string that graphviz can interpretade
        public static string toGraphviz(TTreeNode node)
        {
            StringBuilder stringBuilder = new StringBuilder(); //Create a stringbuilder var

            //Check if the node on it's left and right fields are not empty
            if (node.left != null)
            {
                //Append a new format for each node for graphviz to be able to read it
                stringBuilder.AppendFormat("{0}->{1}{2}", node.ToString(), node.left.ToString(), Environment.NewLine);
                stringBuilder.Append(toGraphviz(node.left));
            }

            if (node.right != null)
            {
                stringBuilder.AppendFormat("{0}->{1}{2}", node.ToString(), node.right.ToString(), Environment.NewLine);
                stringBuilder.Append(toGraphviz(node.right));
            }
            return(stringBuilder.ToString());
        }
Ejemplo n.º 6
0
            //insert
            public TTreeNode insert(DateTime name, int d)
            {
                TTreeNode node = new TTreeNode(name, d);

                try
                {
                    if (root == null)
                    {
                        root = node;
                    }
                    else
                    {
                        add(node, ref root);
                    }
                    _count++;
                    return(node);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
Ejemplo n.º 7
0
 public IndexTrackedNode(int index, TTreeNode node)
 {
     this.Index = index;
     this.Node  = node;
 }
Ejemplo n.º 8
0
 public TBinarySTree()
 {
     root   = null;
     _count = 0;
 }