Remove a leaf from a BinaryTree - java

My code below will remove a tree with two children, or one child. But a leaf (a tree with zero children) I can't seem to crack. My BSTNode<E> also contains a .parent which identifies it's parent. I'm not sure if that would help with this implementation or not.
#Override
public E delete(E target) {
return delete(this.root, target);
}
private E delete(BSTNode<E> localRoot, E target) {
// ... Trying to delete an item that's not in the tree?
if (localRoot == null) {
return null;
}
int compareResult = c.compare(target, localRoot.data);
// Traverse the tree...
if (compareResult < 0) {
return delete(localRoot.left, target);
} else if (compareResult > 0) {
return delete(localRoot.right, target);
} else {
// Found the item! Oh boy here we go!
E retVal = localRoot.data;
/*
* Both left and right exist under the targeted node.
* Find the largest child under the right side of the node.
* Set the largest data to the 'deleted' node, then delete that node.
*/
if (localRoot.left != null && localRoot.right != null) {
/*
* Two children, find the smallest and assign it.
*/
localRoot.data = localRoot.right.data;
localRoot.right = findLargestChild(localRoot.right);
} else if (localRoot.left != null) {
localRoot.data = localRoot.left.data;
localRoot.left = findSmallestChild(localRoot.left);
} else if (localRoot.right != null) {
localRoot.data = localRoot.right.data;
localRoot.right = findLargestChild(localRoot.right);
} else {
/*
* TODO:
* Remove a leaf.
*/
System.out.println("Removing leaf..." + localRoot.data.toString());
localRoot = null;
}
return retVal;
}
}

Using localRoot.parent I was able to delete (null) the child of the localRoot's parent. This feels backwards, but it's passing JUnit Tests...
private E delete(BSTNode<E> localRoot, E target) {
// ... Trying to delete an item that's not in the tree?
if (localRoot == null) {
return null;
}
int compareResult = c.compare(target, localRoot.data);
// Traverse the tree...
if (compareResult < 0) {
return delete(localRoot.left, target);
} else if (compareResult > 0) {
return delete(localRoot.right, target);
} else {
// Found the item! Oh boy here we go!
E retVal = localRoot.data;
if (localRoot.right != null) {
/*
* Assign the largest value to the tree.
*/
localRoot.data = localRoot.right.data;
localRoot.right = findLargestChild(localRoot.right);
} else if (localRoot.left != null) {
/*
* Since the largest value didn't exist, assign the smallest.
*/
localRoot.data = localRoot.left.data;
localRoot.left = findSmallestChild(localRoot.left);
} else {
/*
* Remove a leaf.
*/
System.out.println("Removing leaf or left..." + localRoot.data.toString());
if (c.compare(target, localRoot.parent.left.data) == 0) {
localRoot.parent.left = null;
} else {
localRoot.parent.right = null;
}
}
return retVal;
}
}

Related

Implementation of binary search tree add algorithm

I have to implement an add method for a BST in java, but can not get my add function to work. Could someone help me out?
private boolean add(E x, BinaryNode<E> currentNode){
if (currentNode == null){
currentNode = new BinaryNode<>(x);
size++;
return true;
}
if (currentNode.element.compareTo(x) == 0){
return false;
}
else if((currentNode.element.compareTo(x) < 0)){
if(currentNode.left == null){
currentNode.left = new BinaryNode<>(x);
size++;
return true;
} else {
add(x, currentNode.left);
}
}
else if(currentNode.element.compareTo(x) > 0){
if(currentNode.right == null){
currentNode.right = new BinaryNode<>(x);
size++;
return true;
} else {
add(x, currentNode.right);
}
}
return false;
}
public boolean add(E x){
return this.add(x, root);
}
One problem I see is that when you assign the root element you assign it to a local variable. This obviously doesn't work.
private boolean add(E x, BinaryNode<E> currentNode){
/////// REMOVE
if (currentNode == null){
currentNode = new BinaryNode<>(x);
size++;
return true;
}
///////
And add this
public boolean add(E x){
if( root == null ) {
root = new BinaryNode<>(x);
size++;
return true;
} else
return this.add(x, root);
}
Basically, the root of subtree might change, and this is a recursion, to make it work the return value should be the new root of the subtree, despite it changed or not.
Following are the add() method taken from my BST impl in Java, that with all test cases passed:
/**
* Add a new value.
*
* #param v
*/
#Override
public void add(T v) {
root = add(root, v);
}
/**
* Add to a subtree start from given node.
*
* #param current root of a subtree to add node to,
* #param v
* #return the new root of subtree,
*/
protected BSTNode<T> add(BSTNode<T> current, T v) {
if (current == null) { // subtree is empty,
size++;
return new BSTNode<>(v);
}
// compare,
int compareFlag = v.compareTo(current.value);
// check this or subtree,
if (compareFlag < 0) { // smaller, go to left subtree,
current.left = add(current.left, v);
} else if (compareFlag > 0) { // larger, go to right subtree,
current.right = add(current.right, v);
} else { // equals, ignore it,
}
return current;
}

Getting nth item of a BST

I am trying to return the data held by the nth item of a BST, I'm trying to do an inorder traversal with a counter, and when the counter is larger than n, return the current node. My current code seems to always return the first item, and I can't see where my logic is wrong. I only wrote the nth and inOrder methods, the rest were provided. I think I'm incrementing my counter too often, is that the cause or am I doing something else wrong. I'll post the main method I'm testing with below as well.
import java.util.NoSuchElementException;
public class BST {
private BTNode<Integer> root;
public BST() {
root = null;
}
public boolean insert(Integer i) {
BTNode<Integer> parent = root, child = root;
boolean goneLeft = false;
while (child != null && i.compareTo(child.data) != 0) {
parent = child;
if (i.compareTo(child.data) < 0) {
child = child.left;
goneLeft = true;
} else {
child = child.right;
goneLeft = false;
}
}
if (child != null)
return false; // number already present
else {
BTNode<Integer> leaf = new BTNode<Integer>(i);
if (parent == null) // tree was empty
root = leaf;
else if (goneLeft)
parent.left = leaf;
else
parent.right = leaf;
return true;
}
}
public int greater(int n) {
if (root == null) {
return 0;
}
else {
return n;
}
}
int c = 0;
public int nth(int n) throws NoSuchElementException {
BTNode<Integer> node = null;
if (root == null) {
throw new NoSuchElementException("Element " + n + " not found in tree");
}
else {
if (root != null){
node = inOrder(root, n);
}
}
return node.data;
}
public BTNode inOrder(BTNode<Integer> node, int n) {
c++;
while (c <= n) {
if (node.left != null) {
inOrder(node.left, n);
}
c++;
if (node.right != null) {
inOrder(node.right, n);
}
}
return node;
}
}
class BTNode<T> {
T data;
BTNode<T> left, right;
BTNode(T o) {
data = o;
left = right = null;
}
}
public class bstTest {
public static void main(String[] args) {
BST tree = new BST();
tree.insert(2);
tree.insert(5);
tree.insert(7);
tree.insert(4);
System.out.println(tree.nth(2));
}
}
An invariant you should consider is that when n = sizeOfLeftSubtree + 1, then return that node. If n is less, then go left. If n is greater, then go right and reduce n by sizeOfLeftSubtree+1. Note that I map n=1 to the first element (the leftmost element).
You could trivially calculate the size of a subtree recursively, or you can store the size at every root (every node is a root of a subtree) modifying you insert method (save in a stack/queue all nodes visited and if a new node is added just increment all sizes by 1).
If the size is stored the complexity will be O(log n). If not if could become O(n^2).
public int nth(int n) throws NoSuchElementException {
if( sizeOfTree(this.root) < n || n < 1)
throw new NoSuchElementException("Element " + n + " not found in tree");
BTNode<Integer> root = this.root;
boolean found = false;
do{
int sizeOfLeftSubtree = sizeOfTree(root.left);
if( sizeOfLeftSubtree + 1 == n ){
found = true;
}else if( n < sizeOfLeftSubtree+1 ){
root = root.left;
}else if( sizeOfLeftSubtree+1 < n ){
root = root.right;
n -= sizeOfLeftSubtree+1;
}
}while( !found );
return root.data;
}
public int sizeOfTree(BTNode<Integer> root){
if( root == null )
return 0;
else
return sizeOfTree(root.left) + 1 + sizeOfTree(root.right);
}
You don't change node in the inOrder method.
public BTNode inOrder(BTNode<Integer> node, int n) {
c++;
while (c <= n) {
if (node.left != null) {
// **** Add this - or something.
node = inOrder(node.left, n);
}
c++;
if (node.right != null) {
// **** Add this - or something.
node = inOrder(node.right, n);
}
}
return node;
}
Not suggesting this is the bug you are trying to fix but it is certainly a problem with the code.

Binary Search Tree - print out number of leaves

I have a program that is a binary search tree, the method searches for a specific word. I'm having two problems.
First is I would like to print the true or false from this method (basically making a system.out that says if the word was found), I'm assuming I would do it in main but I'm not sure how to do that.
The second problem is that I also need to print out how many leaves are in the tree, I was going to use a counter of some sort in the method but I didn't work.
My method is below but I also included it inside the class to help clear up any confusion.
Any help would be greatly appreciated.
public boolean check(BSTNode t,String key)
{
if (t == null) return false;
if (t.word.equals(key)) return true;
if (check(t.left,key)) return true;
if (check(t.right,key)) return true;
return false;
}
Whole class:
public class BST
{
BSTNode root;
BST() {
root = null;
}
public void add2Tree(String st)
{
BSTNode newNode = new BSTNode(st);
if (this.root == null) {
root = newNode;
} else {
root = addInOrder(root, newNode);
}
}
// private BSTNode insert2(BSTNode root, BSTNode newNode)
// {
// if (root == null)
// root = newNode;
// else {
// System.out.println(root.word + " " + newNode.word);
// if (root.word.compareTo(newNode.word) > 0)
// {
// root.left = (insert2(root.lTree, newNode));
// System.out.println(" left ");
// } else
// {
// root.rTree = (insert2(root.rTree, newNode));
// System.out.println(" right ");
// }
// }
// return root;
// }
public BSTNode addInOrder(BSTNode focus, BSTNode newNode) {
int comparevalue = 0;
if (focus == null) {
focus = newNode;
}
if (focus != null) {
comparevalue = newNode.word.compareTo(focus.word);
}
if (comparevalue < 0) {
focus.left = addInOrder(focus.left, newNode);
} else if (comparevalue > 0) {
focus.right = addInOrder(focus.right, newNode);
}
return (focus);
}
public void ioprint() {
System.out.println(" start inorder");
if (root == null)
System.out.println(" Null");
printinorder(root);
}
public void printinorder(BSTNode t) {
if (t != null) {
printinorder(t.left);
System.out.println(t.word);
printinorder(t.right);
}
}
public boolean check(BSTNode t,String key)
{
if (t == null) return false;
if (t.word.equals(key)) return true;
if (check(t.left,key)) return true;
if (check(t.right,key)) return true;
return false;
}
public BSTNode getroot(){
return root;
}
}
How to print true/false:
Create another class, call it Solution, Test or whatever you like.
Add a main method to it.
Populate your BST.
Call System.out.println(check(bstRoot, key)).
You can check this link to find out how to count the number of nodes in BST, it's pretty straightforward:
Counting the nodes in a binary search tree
private int countNodes(BSTNode current) {
// if it's null, it doesn't exist, return 0
if (current == null) return 0;
// count myself + my left child + my right child
return 1 + nodes(current.left) + nodes(current.right);
}

binary search tree deletion method [duplicate]

I am trying to implement a remove method for the BST structure that I have been working on. Here is the code with find, insert, and remove methods:
public class BST {
BSTNode root = new BSTNode("root");
public void insert(BSTNode root, String title){
if(root.title!=null){
if(title==root.title){
//return already in the catalog
}
else if(title.compareTo(root.title)<0){
if(root.leftChild==null){
root.leftChild = new BSTNode(title);
}
else{
insert(root.leftChild,title);
}
}
else if(title.compareTo(root.title)>0){
if(root.rightChild==null){
root.rightChild = new BSTNode(title);
}
else{
insert(root.rightChild,title);
}
}
}
}
public void find(BSTNode root, String title){
if(root!= null){
if(title==root.title){
//return(true);
}
else if(title.compareTo(root.title)<0){
find(root.leftChild, title);
}
else{
find(root.rightChild, title);
}
}
else{
//return false;
}
}
public void remove(BSTNode root, String title){
if(root==null){
return false;
}
if(title==root.title){
if(root.leftChild==null){
root = root.rightChild;
}
else if(root.rightChild==null){
root = root.leftChild;
}
else{
//code if 2 chlidren remove
}
}
else if(title.compareTo(root.title)<0){
remove(root.leftChild, title);
}
else{
remove(root.rightChild, title);
}
}
}
I was told that I could use the insert method to help me with the remove method, but I am just not seeing how I can grab the smallest/largest element, and then replace the one I am deleting with that value, then recursively delete the node that I took the replacement value, while still maintaining O(logn) complexity. Anyone have any ideas or blatant holes I missed, or anything else helpful as I bang my head about this issue?
EDIT:
I used the answers ideas to come up with this, which I believe will work but I'm getting an error that my methods (not just the remove) must return Strings, here is what the code looks like, I thought that's the return statements??
public String remove(BSTNode root, String title){
if(root==null){
return("empty root");
}
if(title==root.title){
if(root.leftChild==null){
if(root.rightChild==null){
root.title = null;
return(title+ "was removed");
}
else{
root = root.rightChild;
return(title+ "was removed");
}
}
else if(root.rightChild==null){
root = root.leftChild;
return(title+ "was removed");
}
else{
String minTitle = minTitle(root);
root.title = minTitle;
remove(root.leftChild,minTitle);
return(title+ "was removed");
}
}
else if(title.compareTo(root.title)<0){
remove(root.leftChild, title);
}
else{
remove(root.rightChild, title);
}
}
public void remove (String key, BSTNode pos)
{
if (pos == null) return;
if (key.compareTo(pos.key)<0)
remove (key, pos.leftChild);
else if (key.compareTo(pos.key)>0)
remove (key, pos.rightChild);
else {
if (pos.leftChild != null && pos.rightChild != null)
{
/* pos has two children */
BSTNode maxFromLeft = findMax (pos.leftChild); //need to make a findMax helper
//"Replacing " pos.key " with " maxFromLeft.key
pos.key = maxFromLeft.key;
remove (maxFromLeft.key, pos.leftChild);
}
else if(pos.leftChild != null) {
/* node pointed by pos has at most one child */
BSTNode trash = pos;
//"Promoting " pos.leftChild.key " to replace " pos.key
pos = pos.leftChild;
trash = null;
}
else if(pos.rightChild != null) {
/* node pointed by pos has at most one child */
BSTNode trash = pos;
/* "Promoting " pos.rightChild.key" to replace " pos.key */
pos = pos.rightChild;
trash = null;
}
else {
pos = null;
}
}
}
This is the remove for an unbalanced tree. I had the code in C++ so I have quickly translated. There may be some minor mistakes though. Does the tree you are coding have to be balanced? I also have the balanced remove if need be. I wasn't quite sure based on the wording of your question. Also make sure you add a private helper function for findMax()
void deleteTreeNode(int data){
root = deleteTreeNode(root ,data);
}
private TreeNode deleteTreeNode(TreeNode root, int data) {
TreeNode cur = root;
if(cur == null){
return cur;
}
if(cur.data > data){
cur.left = deleteTreeNode(cur.left, data);
}else if(cur.data < data){
cur.right = deleteTreeNode(cur.right, data);
}else{
if(cur.left == null && cur.right == null){
cur = null;
}else if(cur.right == null){
cur = cur.left;
}else if(cur.left == null){
cur = cur.right;
}else{
TreeNode temp = findMinFromRight(cur.right);
cur.data = temp.data;
cur.right = deleteTreeNode(cur.right, temp.data);
}
}
return cur;
}
private TreeNode findMinFromRight(TreeNode node) {
while(node.left != null){
node = node.left;
}
return node;
}
To compare objects in java use .equals() method instead of "==" operator
if(title==root.title)
^______see here
you need to use like this
if(title.equals(root.title))
or if you are interesed to ignore the case follow below code
if(title.equalsIgnoreCase(root.title))
private void deleteNode(Node temp, int n) {
if (temp == null)
return;
if (temp.number == n) {
if (temp.left == null || temp.right == null) {
Node current = temp.left == null ? temp.right : temp.left;
if (getParent(temp.number, root).left == temp)
getParent(temp.number, root).left = current;
else
getParent(temp.number, root).right = current;
} else {
Node successor = findMax(temp.left);
int data = successor.number;
deleteNode(temp.left, data);
temp.number = data;
}
} else if (temp.number > n) {
deleteNode(temp.left, n);
} else {
deleteNode(temp.right, n);
}
}
I know this is a very old question but anyways... The accepted answer's implementation is taken from c++, so the idea of pointers still exists which should be changed as there are no pointers in Java. So every time when you change the node to null or something else, that instance of the node is changed but not the original one This implementation is taken from one of the coursera course on algorithms.
public TreeNode deleteBSTNode(int value,TreeNode node)
{
if(node==null)
{
System.out.println("the value " + value + " is not found");
return null;
}
//delete
if(node.data>value) node.left = deleteBSTNode(value,node.left);
else if(node.data<value) node.right = deleteBSTNode(value,node.right);
else{
if(node.isLeaf())
return null;
if(node.right==null)
return node.left;
if(node.left==null)
return node.right;
TreeNode successor = findMax(node.left);
int data = successor.data;
deleteBSTNode(data, node.left);
node.data = data;
}
return node;
}
All the links between the nodes are pertained using the return value from the recursion.
For the Depth First Post-Order traversal and removal, use:
/*
*
* Remove uses
* depth-first Post-order traversal.
*
* The Depth First Post-order traversal follows:
* Left_Child -> Right-Child -> Node convention
*
* Partial Logic was implemented from this source:
* https://stackoverflow.com/questions/19870680/remove-method-binary-search-tree
* by: sanjay
*/
#SuppressWarnings("unchecked")
public BinarySearchTreeVertex<E> remove(BinarySearchTreeVertex<E> rootParameter, E eParameter) {
BinarySearchTreeVertex<E> deleteNode = rootParameter;
if ( deleteNode == null ) {
return deleteNode; }
if ( deleteNode.compareTo(eParameter) == 1 ) {
deleteNode.left_child = remove(deleteNode.left_child, eParameter); }
else if ( deleteNode.compareTo(eParameter) == -1 ) {
deleteNode.right_child = remove(deleteNode.right_child, eParameter); }
else {
if ( deleteNode.left_child == null && deleteNode.right_child == null ) {
deleteNode = null;
}
else if ( deleteNode.right_child == null ) {
deleteNode = deleteNode.left_child; }
else if ( deleteNode.left_child == null ) {
deleteNode = deleteNode.right_child; }
else {
BinarySearchTreeVertex<E> interNode = findMaxLeftBranch( deleteNode.left_child );
deleteNode.e = interNode.e;
deleteNode.left_child = remove(deleteNode.left_child, interNode.e);
}
} return deleteNode; } // End of remove(E e)
/*
* Checking right branch for the swap value
*/
#SuppressWarnings("rawtypes")
public BinarySearchTreeVertex findMaxLeftBranch( BinarySearchTreeVertex vertexParameter ) {
while (vertexParameter.right_child != null ) {
vertexParameter = vertexParameter.right_child; }
return vertexParameter; } // End of findMinRightBranch

making binary search tree

How do I make a BST when I have an array list of 100 elements like {3,2,6,7,...,99}?
I believe TreeSet is an implementation of a binary search tree. Since integers have a natural ordering you could simply loop through your array of integers and add them all to a TreeSet<Integer>.
Note also, that there is a method Arrays.binarySearch that does a binary search in a sorted array.
int[] someInts = {3,2,6,7, /*...,*/ 99};
// use a TreeSet
TreeSet<Integer> ints = new TreeSet<Integer>();
for (int i : someInts)
ints.add(i);
System.out.println(ints.contains(2)); // true
System.out.println(ints.contains(5)); // false
// or sort the array and use Arrays.binarySearch
Arrays.sort(someInts);
System.out.println(Arrays.binarySearch(someInts, 2) >= 0); // true
System.out.println(Arrays.binarySearch(someInts, 5) >= 0); // false
Unless you want to implement everything yourself (in that case, you might want to check here) you should take a look at [Collections.binarySearch][2].
[2]: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#binarySearch(java.util.List, java.lang.Object)
1st sort this array , than use BST
EDIT
1- BST works on the sorted array.
2- Use this psudo code See Here
I recently finished a project where we basically had to do this.
You could take a look at this class
Edit: This is in C++, I see you're coding in Java, my apologies!
/**************************************
* Tree.cpp - Created by DT
**************************************
*
* Functions:
*
* public:
* Tree();
* void addNode(int);
* bool findID(int);
* Node* findNode(int);
* Node* findParent(int);
* bool deleteID(int);
* Node* findMaximum(Node*);
* Node* findMinimum(Node*);
* void printInOrder();
* void printPreOrder();
* void printPostOrder();
* int recurseHeight(Node*);
* int getHeight();
* void inOrder(Node*);
* void preOrder(Node*);
* void postOrder(Node*);
*
***************************************/
#include <iostream>
#include "Node.h"
#include "Tree.h"
using namespace std;
Tree::Tree() {
root = NULL;
}
///////////////////////////////
// AddNode Function:
///////////////////////////////
void Tree::addNode(int id) {
if(findNode(id)) {
cout << "This ID already exists in the tree" << endl;
return;
}
//if(id == 2147483647) {
// cout << "Overflow Detected: Did you enter a really big number?\n";
// cout << "This ID is being stored as 2147483647\n";
//}
Node *newNode = new Node();
newNode->id = id;
if(root == NULL) {
root = newNode;
return;
}
Node *nextNode = root;
Node *lastNode = nextNode;
while(nextNode != NULL) {
if(id <= nextNode->id) {
lastNode = nextNode;
nextNode = nextNode->leftChild;
}
else {
lastNode = nextNode;
nextNode = nextNode->rightChild;
}
}
if(id <= lastNode->id)
lastNode->leftChild = newNode;
else
lastNode->rightChild = newNode;
}
///////////////////////////////
// FindID Function:
///////////////////////////////
bool Tree::findID(int id) {
Node *finder = root;
while(finder != NULL) {
if(id == finder->id)
return true;
if(id <= finder->id)
finder = finder->leftChild;
else
finder = finder->rightChild;
}
return false;
}
///////////////////////////////
// FindNode Helper Function:
///////////////////////////////
Node* Tree::findNode(int id) {
Node *finder = root;
while(finder != NULL) {
if(id == finder->id)
return finder;
if(id <= finder->id)
finder = finder->leftChild;
else
finder = finder->rightChild;
}
return NULL;
}
///////////////////////////////
// FindParent Helper Function:
///////////////////////////////
Node* Tree::findParent(int id) {
Node *parent = NULL;
Node *finder = root;
while(finder != NULL) {
if(id == finder->id)
return parent;
parent = finder;
if(id <= finder->id)
finder = finder->leftChild;
else
finder = finder->rightChild;
}
return NULL;
}
///////////////////////////////
// DeleteID Function:
///////////////////////////////
bool Tree::deleteID(int id) {
if(root == NULL)
return false;
Node *toDelete = findNode(id); //Find the node to delete
if(toDelete == NULL) //If we can't find it, return false
return false;
Node *parent = findParent(id); //Find the parent of the node to delete
Node *justInCase; //In case we are deleting the root node
bool deletingRoot = false; //This is a special case so handle it differently
if(root->id == id) { //If we're deleting the root node
justInCase = new Node(); //Let's create a fake parent for the root
justInCase->leftChild = root; //Just to make sure that we can run checks on parents
justInCase->rightChild = NULL;
justInCase->id = 0; //Later on in the code
parent = justInCase; //Set the parent of the root to our new fake node
deletingRoot = true; //Let the end of our function know we're deleting the root
}
bool deletingLeftChild = (parent->leftChild == toDelete);
if(toDelete->leftChild == NULL && toDelete->rightChild == NULL) {
if(toDelete == root)
root = NULL;
if(deletingLeftChild)
parent->leftChild = NULL;
else
parent->rightChild = NULL;
delete toDelete;
return true;
}
if((toDelete->leftChild == NULL || toDelete->rightChild == NULL) && (parent != NULL && !deletingRoot)) {
if(deletingLeftChild)
parent->leftChild = (toDelete->leftChild == NULL) ? toDelete->rightChild : toDelete->leftChild;
else
parent->rightChild = (toDelete->leftChild == NULL) ? toDelete->rightChild : toDelete->leftChild;
delete toDelete;
return true;
}
Node *replacer = findMaximum(toDelete->leftChild); //Replace the node we're deleting with the hightest LEFT Child
if(replacer == NULL || replacer == toDelete) //If we can't find a left child (in case of deleting root)
replacer = findMinimum(toDelete->rightChild); //Find the smallest RIGHT child
Node *replacerParent = findParent(replacer->id); //Find the parent of this child
if(replacerParent != NULL) { //If this child has a parent
if(replacerParent->leftChild == replacer) { //If the child is to the left of the parent
if(replacer->leftChild != NULL) //And the left child has a child of its own (in case of findMinimum/maximum)
replacerParent->leftChild = replacer->leftChild;//set the parent's child to this child's node
else
replacerParent->leftChild = NULL; //Otherwise, set the parent's child to NULL
}
else { //In the case of Right Child
if(replacer->rightChild != NULL) //Do the same thing
replacerParent->rightChild = replacer->rightChild;
else
replacerParent->rightChild = NULL;
}
}
toDelete->id = replacer->id; //Swap the IDs of the nodes we're deleting
delete replacer; //And delete the minimum or maximum that we found
return true;
}
///////////////////////////////
// FindMaximum Helper Function:
///////////////////////////////
Node* Tree::findMaximum(Node *theNode) {
if(theNode == NULL)
return NULL;
Node *finder = theNode;
Node *last = finder;
while(finder != NULL) {
last = finder;
finder = finder->rightChild;
}
return last;
}
///////////////////////////////
// FindMinimum Helper Function:
///////////////////////////////
Node* Tree::findMinimum(Node *theNode) {
if(theNode == NULL)
return NULL;
Node *finder = theNode;
Node *last = finder;
while(finder != NULL) {
last = finder;
finder = finder->leftChild;
}
return last;
}
///////////////////////////////
// PrintInOrder Function:
///////////////////////////////
void Tree::printInOrder() {
inOrder(root); //Recurse through our root
cout << "\b " << endl;
}
///////////////////////////////
// PrintPostOrder Function:
///////////////////////////////
void Tree::printPostOrder() {
postOrder(root); //Recurse through our root
cout << "\b " << endl;
}
///////////////////////////////
// PrintPreOrder Function:
///////////////////////////////
void Tree::printPreOrder() {
preOrder(root); //Recurse through our root
cout << "\b " << endl;
}
///////////////////////////////
// RecurseHeight Function:
///////////////////////////////
int Tree::recurseHeight(Node *node) {
if(node == NULL) return -1;
return 1 + max(recurseHeight(node->leftChild),recurseHeight(node->rightChild));
}
///////////////////////////////
// GetHeight Function:
///////////////////////////////
int Tree::getHeight() { return recurseHeight(root); } //Recurse through our root
///////////////////////////////
// InOrder Function:
///////////////////////////////
void Tree::inOrder(Node *cNode) {
if(cNode == NULL)
return;
inOrder(cNode->leftChild);
cout << cNode->id << "-";
inOrder(cNode->rightChild);
}
///////////////////////////////
// PostOrder Function:
///////////////////////////////
void Tree::postOrder(Node *cNode) {
if(cNode == NULL)
return;
postOrder(cNode->leftChild);
postOrder(cNode->rightChild);
cout << cNode->id << "-";
}
///////////////////////////////
// PreOrder Function:
///////////////////////////////
void Tree::preOrder(Node *cNode) {
if(cNode == NULL)
return;
cout << cNode->id << "-";
preOrder(cNode->leftChild);
preOrder(cNode->rightChild);
}
The node class:
/**************************************
* Node.cpp - Created by DT
**************************************
*
* An incredibly simple class that
* apparently deserves its own header
*
***************************************/
#include "Node.h"
#include <iostream>
Node::Node() {
leftChild = NULL;
rightChild = NULL;
id = NULL;
}

Categories

Resources