/// <summary> /// Move node position after deleting /// </summary> /// <param name="parent"></param> /// <param name="target"></param> /// <param name="replace"></param> private void UpdateMovieNode(MovieNode parent, MovieNode target, MovieNode replace) { if (parent.left == target) { parent.left = replace; } else { parent.right = replace; } }
/// <summary> /// Add new movie to the tree /// </summary> /// <param name="movie">Movie object</param> /// <returns>True if movie is added, False if movie already exists in the collection</returns> public bool Add(Movie movie) { if (root == null) { root = new MovieNode(movie); count++; return(true); } MovieNode pin = root; while (true) { // the title comes before the compared title if (movie.CompareTo(pin.movie) < 0) { if (pin.left == null) { pin.left = new MovieNode(movie); count++; return(true); } pin = pin.left; // the title comes after the compared title } else if (pin.movie.CompareTo(movie) < 0) { if (pin.right == null) { pin.right = new MovieNode(movie); count++; return(true); } pin = pin.right; // the title is same as the compared title (pin.movie.CompareTo(movie) == 0) // the movie should not be added to the tree } else { return(false); } } }
/// <summary> /// Get Movie object of title /// </summary> /// <param name="title">Movie title</param> /// <returns>Movie object</returns> public Movie GetMovie(string title) { MovieNode pin = root; while (pin != null) { // found a movie if (CompareMovie(pin.movie.title, title) == 0) { return(pin.movie); // the movie should be on the left side of current pin } else if (CompareMovie(title, pin.movie.title) < 0) { pin = pin.left; // the movie should be on the right side of current pin } else if (CompareMovie(pin.movie.title, title) < 0) { pin = pin.right; } } return(null); }
/// <summary> /// Remove movie objct of title from tree /// </summary> /// <param name="title">Movie title that is to be deleted</param> public void Remove(string title) { MovieNode parent = null; MovieNode pin = root; while (pin != null) { // found a target movie if (CompareMovie(pin.movie.title, title) == 0) { // no children if (pin.left == null && pin.right == null) { // current pin is root, so just empty the tree if (pin == root) { root = null; } else { // pin to be null, parent's left or right to be null UpdateMovieNode(parent, pin, null); } // has a child on the left hand side of current node (pin) } else if (pin.left != null && pin.right == null) { if (pin == root) { // root will be the node of the left hand side of current root root = root.left; } else { // move pin's left to pin's position UpdateMovieNode(parent, pin, pin.left); } // has a child on the right hand side of current node (pin) } else if (pin.left == null & pin.right != null) { if (pin == root) { root = root.right; } else { // move pin's right to pin's position UpdateMovieNode(parent, pin, pin.right); } // has children on both side of current node (pin) } else { MovieNode scopeParent = pin; MovieNode scopePin = pin.left; // find right-most node in left subtree while (scopePin.right != null) { scopeParent = scopePin; scopePin = scopePin.right; } // no right subtree in pin's left subtree // shift node up if (scopeParent == pin) { pin.movie = scopePin.movie; pin.left = scopePin.left; // let scope parent to take over scope pin's left // scope pin move to pin's (to be deleted) poistion } else { scopeParent.right = scopePin.left; pin.movie = scopePin.movie; } } count--; return; // title is on the left side of the pin } else if (CompareMovie(title, pin.movie.title) < 0) { parent = pin; pin = pin.left; // title is on the right side of the pin } else if (CompareMovie(pin.movie.title, title) < 0) { parent = pin; pin = pin.right; } } }