Esempio n. 1
0
        //Recursive function for adding a movie into the Binary Search Tree in the correct place
        //Input: Movie movie, MovieNode node
        //Output: Movie movie added to the Binary Search Tree if it belongs as one of the
        //        current node's children, otherwise traverse deeper towards where it belongs
        public void Add(Movie movie, MovieNode node)
        {
            int compare = sort == "title" ?
                          movie.Title.CompareTo(node.Movie.Title) :
                          movie.Borrowed.CompareTo(node.Movie.Borrowed);

            if (compare < 0)
            {
                if (node.Left != null)
                {
                    Add(movie, node.Left);
                }
                else
                {
                    node.Left = new MovieNode(movie);
                }
            }
            else
            {
                if (node.Right != null)
                {
                    Add(movie, node.Right);
                }
                else
                {
                    node.Right = new MovieNode(movie);
                }
            }
        }
Esempio n. 2
0
 //Recursive function for creating nodes inside the new subcollection
 //Input: MovieNode current node of old tree, MovieCollection new subcollection, int start, int end, int index of node to be visited, string sorting method
 //Output: int index of how many items have already been traversed, current node from old tree added to subcollection if index is between start and end
 public int Subcollection(MovieNode root, MovieCollection sub, int start, int end, int index, string sort)
 {
     if (root != null && sort == "title")
     {
         index = Subcollection(root.Left, sub, start, end, index, sort);
         if (index >= start)
         {
             if (index >= end)
             {
                 return(index);
             }
             sub.Add(root.Movie);
         }
         index++;
         index = Subcollection(root.Right, sub, start, end, index, sort);
     }
     else if (root != null && sort == "top")
     {
         index = Subcollection(root.Right, sub, start, end, index, sort);
         if (index >= start)
         {
             if (index >= end)
             {
                 return(index);
             }
             sub.Add(root.Movie);
         }
         index++;
         index = Subcollection(root.Left, sub, start, end, index, sort);
     }
     return(index);
 }
Esempio n. 3
0
 //Add a movie to this collection
 //Input: Movie movie
 //Output: Movie movie added to the Binary Search Tree
 public void Add(Movie movie)
 {
     if (root != null)
     {
         Add(movie, root);
     }
     else
     {
         root = new MovieNode(movie);
     }
 }
Esempio n. 4
0
 //Recursive function to display movie data for the given subcollection
 //Input: MovieNode root of "borrowed" sorted subcollection of size 10, reference to an int counting which ranking the current node is,
 //       int page
 //Output: Writes current node's ranking, movie title and borrowed count into the console
 public void TopTen(MovieNode root, ref int counter, int page)
 {
     if (root != null && counter < 10)
     {
         TopTen(root.Right, ref counter, page);
         Console.WriteLine("   {0, -24} {1, 20}",
                           (counter + (page - 1) * 5).ToString() + ((counter + (page - 1) * 5) == 10 ? ". " : ".  ") + Truncate(root.Movie.Title, 20),
                           "Borrowed " + root.Movie.Borrowed.ToString() + " times");
         counter++;
         TopTen(root.Left, ref counter, page);
     }
 }
Esempio n. 5
0
 //Constructor with optional string to specify sorting arrangements
 //Input: string sorting method
 //Output: this.sort = sorting method
 public MovieCollection(string s)
 {
     root = null;
     if (sort == "title" || sort == "top")
     {
         sort = s;
     }
     else
     {
         sort = "title";
     }
 }
Esempio n. 6
0
 //Recursive function to find movie by traversing the Binary Search Tree efficiently
 //Input: string title, MovieNode node
 //Output: Movie movie if node's movie value is correct, otherwise traverse deeper towards the correct node
 public Movie Find(string title, MovieNode r)
 {
     if (r == null)
     {
         return(null);
     }
     if (title.CompareTo(r.Movie.Title) == 0)
     {
         return(r.Movie);
     }
     if (title.CompareTo(r.Movie.Title) < 0)
     {
         return(Find(title, r.Left));
     }
     return(Find(title, r.Right));
 }
Esempio n. 7
0
 //Displays movie data for a given node
 //Input: MovieNode node
 //Output: Writes this node's movie data into the console
 public void Display(MovieNode root)
 {
     if (root != null)
     {
         Display(root.Left);
         Console.WriteLine("================================================");
         Console.WriteLine("   {0, -15} {1, 29}", "Title:", root.Movie.Title);
         Console.WriteLine("   {0, -15} {1, 29}", "Stars:", Truncate(string.Join(", ", root.Movie.Starring)));
         Console.WriteLine("   {0, -15} {1, 29}", "Director:", root.Movie.Director);
         Console.WriteLine("   {0, -15} {1, 29}", "Duration:", root.Movie.Duration.ToString() + " minutes");
         Console.WriteLine("   {0, -15} {1, 29}", "Genre:", root.Movie.Genre.ToString());
         Console.WriteLine("   {0, -15} {1, 29}", "Classification:", root.Movie.Classification.ToString());
         Console.WriteLine("   {0, -15} {1, 29}", "Release Date:", root.Movie.ReleaseDate.ToString("d", CultureInfo.CreateSpecificCulture("es-ES")));
         Console.WriteLine("   {0, -15} {1, 29}", "Copies:", root.Movie.Copies.ToString());
         Console.WriteLine("================================================");
         Display(root.Right);
     }
 }
Esempio n. 8
0
 public MovieNode(Movie m)
 {
     Movie = m;
     Left  = null;
     Right = null;
 }
Esempio n. 9
0
 public string sort = "title"; //What this collection object is sorted by
 public MovieCollection()
 {
     root = null;
 }
Esempio n. 10
0
        //Deletes a MovieNode from the tree given its title
        //Input: string title
        //Output: this object's Binary Search Tree with the given Movie's node removed
        public void Delete(string title)
        {
            MovieNode pointer = root;
            MovieNode parent  = null;

            while ((pointer != null) && (title.CompareTo(pointer.Movie.Title) != 0))  //Traverse until found the node to delete
            {
                parent = pointer;
                if (title.CompareTo(pointer.Movie.Title) < 0)
                {
                    pointer = pointer.Left;
                }
                else
                {
                    pointer = pointer.Right;
                }
            }
            if (pointer != null)  //Reassign children of removed node into the tree
            {
                if ((pointer.Left != null) && (pointer.Right != null))
                {
                    if (pointer.Left.Right == null)
                    {
                        pointer.Movie = pointer.Left.Movie;
                        pointer.Left  = pointer.Left.Left;
                    }
                    else
                    {
                        MovieNode p  = pointer.Left;
                        MovieNode pp = pointer;
                        while (p.Right != null)
                        {
                            pp = p;
                            p  = p.Right;
                        }
                        pointer.Movie = p.Movie;
                        pp.Right      = p.Left;
                    }
                }
                else
                {
                    MovieNode c;
                    if (pointer.Left != null)
                    {
                        c = pointer.Left;
                    }
                    else
                    {
                        c = pointer.Right;
                    }
                    if (pointer == root)
                    {
                        root = c;
                    }
                    else if (pointer == parent.Left)
                    {
                        parent.Left = c;
                    }
                    else
                    {
                        parent.Right = c;
                    }
                }
            }
        }
Esempio n. 11
0
 //Recursive function for the amount of nodes in the tree
 //Input: MovieNode node
 //Output: int amount of nodes below and including current node
 public int Size(MovieNode r)
 {
     return(r == null ? 0 : Size(r.Left) + 1 + Size(r.Right));
 }