Esempio n. 1
0
        //Function for finding a movie in the tree
        public MovieTreeNode Find(Movie movie)
        {
            string        movieTitle  = movie.getTitle();
            MovieTreeNode currentNode = this;

            //While there is still a node to search in
            while (currentNode != null)
            {
                //If the movie it's searching for is the current node, return the node
                if (string.Compare(movieTitle, currentNode.movie.getTitle()) == 0 && isDeleted == false)
                {
                    return(currentNode);
                }
                //If the movie is less than the current node and the left subtree is not empty, search the left subtree
                else if (string.Compare(movieTitle, currentNode.movie.getTitle()) < 0 && leftNode != null)
                {
                    return(leftNode.FindRecursive(movie));
                }
                //Otherwise search the right subtree if it's not empty
                else if (rightNode != null)
                {
                    return(rightNode.FindRecursive(movie));
                }
                //The movie doesn't exist in the tree
                else
                {
                    return(null);
                }
            }

            return(null);
        }
Esempio n. 2
0
        //Function for inserting a movie object into the tree
        public void Insert(Movie movieAdd)
        {
            string movieTitileAdd = movieAdd.getTitle();

            //If the movie to be added is greatter than the current node
            if (string.Compare(movieTitleAdd, movie.getTitle()) >= 0)
            {
                if (rightNode == null)
                {
                    //Movie is added to the right of the parent node if the right subtree is empty
                    rightNode = new MovieTreeNode(movieAdd);
                }
                else
                {
                    //If the right subtree isn't empty, go to the right subtree
                    rightNode.Insert(movieAdd);
                }
            }
            //If the movie to be added is less than the current node
            else
            {
                //If the left subtree is empty, add the movie to the left subtree
                if (leftNode == null)
                {
                    leftNode = new MovieTreeNode(movieAdd);
                }
                //If it isn't empty, go to the left subtree
                else
                {
                    leftNode.Insert(movieAdd);
                }
            }
        }
        private MovieTreeNode Replacement(MovieTreeNode currentNode)
        {
            MovieTreeNode replacementParent = currentNode;
            MovieTreeNode replacement       = currentNode;
            MovieTreeNode current           = currentNode;

            //Finding the last left node of the right node
            while (current != null)
            {
                replacementParent = replacement;
                replacement       = current;
                current           = current.LeftNode;
            }

            //If the right node of the node to be deleted has atleast one left node
            if (replacement != currentNode.RightNode)
            {
                replacementParent.LeftNode = replacement.RightNode;
                //Moving the right node of the node to be deleted to the right of the replacement node
                replacement.RightNode = currentNode.RightNode;
            }
            //Moving the left child of the node to be deleted to the left child of the deleted nodes right child
            replacement.LeftNode = currentNode.LeftNode;

            return(replacement);
        }
 //Function for inserting node into tree
 public void Insert(Movie movieAdd)
 {
     if (root != null)
     {
         root.Insert(movieAdd);
     }
     else
     {
         //Movie is added as the root node
         root = new MovieTreeNode(movieAdd);
     }
 }
        public int Remove(string movieTitle)
        {
            //Setting the the current node to the root
            MovieTreeNode current = root;
            //Setting the parent node to the root
            MovieTreeNode parent    = root;
            bool          leftChild = false;

            //If the tree is empty, then nothing happens
            if (current == null)
            {
                return(-1);
            }

            //While the current search node does not equal the movie title we're searhing for and the current search node is not empty
            while (current != null)
            {
                parent = current;
                if (string.Compare(movieTitle, current.Movie.getTitle()) == 0)
                {
                    break;
                }
                //If the movie title we're searching for is less than the current node than we'll search the left side of the tree
                else if (string.Compare(movieTitle, current.Movie.getTitle()) < 0)
                {
                    //Setting the current node to the left node
                    current   = current.LeftNode;
                    leftChild = true;
                }
                //Otherwise we'll search the right side of the tree
                else
                {
                    //Setting the current node to the right node
                    current   = current.RightNode;
                    leftChild = false;
                }
            }

            //If the current node is empty, then nothing happens
            if (current == null)
            {
                return(-1);
            }

            //If the movie title to be deleted is a leaf
            if (current.RightNode == null && current.LeftNode == null)
            {
                //If the movieTitle to be deleted is the root node
                if (current == root)
                {
                    root = null;
                }
                else
                {
                    //If the current node is a left child
                    if (leftChild)
                    {
                        //Removing the movie from the tree
                        parent.LeftNode = null;
                    }
                    else
                    {
                        //Removing the movie from the tree
                        parent.RightNode = null;
                    }
                }
            }
            //If the node to be deleted only has a left child
            else if (current.RightNode == null)
            {
                //If the node to be deleted is the root
                if (current == root)
                {
                    //Move the left child to the root
                    root = current.LeftNode;
                }
                else
                {
                    //If node to be deleted is a left child
                    if (leftChild)
                    {
                        //Setting the parents left node to the current node
                        parent.LeftNode = current.LeftNode;
                    }
                    else
                    {
                        parent.RightNode = current.LeftNode;
                    }
                }
            }
            //If the node to be deleted only has a right child
            else if (current.LeftNode == null)
            {
                //If the node to be deleted is the root
                if (current == root)
                {
                    //Setting the root to the right child
                    root = current.RightNode;
                }
                else
                {
                    if (leftChild)
                    {
                        //Setting the parents left node to the current node
                        parent.LeftNode = current.RightNode;
                    }
                    else
                    {
                        //Setting the parents right node to the current node
                        parent.RightNode = current.RightNode;
                    }
                }
            }
            //If the node to be deleted has a left and right child
            else
            {
                MovieTreeNode replacement = Replacement(current);

                //If the node to be deleted is the root node
                if (current == root)
                {
                    root = replacement;
                }
                //If the node to be deleted is a left child
                else if (leftChild)
                {
                    parent.LeftNode = replacement;
                }
                //If the node to be deleted is a right child
                else
                {
                    parent.RightNode = replacement;
                }
            }

            return(1);
        }