// Function that adds the movie object into the BST. public void Insert(Movie movie) { // Setup a new TreeNode to add to BST. TreeNode newTreeNode = new TreeNode(); // Assign the movie object to the movie object in the TreeNode. newTreeNode.movie = movie; bool exit = false; // If there is no root in the BST, set the root to the new tree // node. // Increment the count of movies in the tree. if (root == null) { root = newTreeNode; movieCount++; } else { // Setup a new treeNode for the current node and parent node // to start comparisons when travelling in tree. TreeNode current = root; TreeNode parent; while (!exit) { // Assign parent node to the current node being compared. parent = current; // If the comparison returns -1, traverse left side of // current node. if (string.Compare (movie.GetTitle(), current.movie.GetTitle()) == -1) { // Assign the left node of the current node to the // current variable current = current.left; //If its empty add at this location if (current == null) { // Add treeNode to parent of current. parent.left = newTreeNode; // Increase count of movies in BST. movieCount++; exit = true; } } else { //Assign the right node of the current node to the // current variable current = current.right; //If its empty add at this location if (current == null) { // Add treeNode to parent of current. parent.right = newTreeNode; // Increase count of movies in BST. movieCount++; exit = true; } } } } }
// Function that deletes the movie from the BST - accounts for 0 // children, 1 child on L or R and 2 children deletion. // No output. public bool Delete(Movie movie, MemberCollection memberCollection) { // Check to see if the movie is already rented to a user. Movie[] rentedMovies = memberCollection.ReturnCurrentBorrowedMovies(); bool check = false; // Mark check as true if the entered movie is currently rented. foreach (Movie m in rentedMovies) { if (m.GetTitle() == movie.GetTitle()) { check = true; } } //If it is not currently rented out to user, delete the movie // from BST. if (!check) { TreeNode current = root; TreeNode parent = root; bool leftNode = true; // Check to see if the current movie is the user entered // movie then traverse the BST based off name comparison. while (current.movie.GetTitle() != movie.GetTitle()) { parent = current; // If searched name is less than current node, travel // left tree. if (string.Compare(movie.GetTitle(), current.movie.GetTitle()) == -1) { leftNode = true; current = current.left; } // If searched name is greater than current node, travel // right tree. else { leftNode = false; current = current.right; } if (current == null) { return(false); } } if ((current.left == null) && (current.right == null)) { // Delete the node as it has no children. if (current == root) { root = null; return(true); } // Delete the left child else if (leftNode) { parent.left = null; return(true); } // Delete the right child else { parent.right = null; return(true); } } // If current node's right node is empty, replace with left // node else if (current.right == null) { if (current == root) { root = current.left; return(true); } // Replace current node with its left child in BST. else if (leftNode) { parent.left = current.left; return(true); } // Replace current node with its right child in BST. else { parent.right = current.left; return(true); } } // If current node's left node is empty, replace with right // node else if (current.left == null) { if (current == root) { root = current.right; return(true); } // Replace current node with its left child in BST. else if (leftNode) { parent.left = current.right; return(true); } // Replace current node with its right child in BST. else { parent.right = current.right; return(true); } } // Node has 2 children else { // If far right most node of current node is empty, // current node becomes left node. if (current.left.right == null) { current.movie = current.left.movie; current.left = current.left.left; return(true); } // Replace current node with the far right most node. else { TreeNode node = current.left; TreeNode parentNode = current; while (node.right != null) { parentNode = node; node = node.right; } current.movie = node.movie; parentNode.right = node.left; return(true); } } } else { Console.WriteLine("You cannot delete a " + "movie that is currently rented out."); return(false); } }