Browse Source

working insert method for binary tree - FINALLY.

master
Charles Reid 8 years ago
parent
commit
846f482b36
  1. 173
      trees/search-trees/Weiss.java

173
trees/search-trees/Weiss.java

@ -6,19 +6,29 @@ @@ -6,19 +6,29 @@
*/
public class Weiss {
public static void main(String[] args) {
BinarySearchTree b = new BinarySearchTree();;
BinarySearchTree b = new BinarySearchTree();
b.insert(new Integer(5));
b.insert(new Integer(4));
b.insert(new Integer(9));
b.insert(new Integer(8));
b.insert(new Integer(48));
b.insert(new Integer(43));
b.insert(new Integer(49));
b.insert(new Integer(44));
b.insert(new Integer(45));
b.insert(new Integer(58));
b.insert(new Integer(68));
b.insert(new Integer(108));
b.insert(new Integer(138));
b.insert(new Integer(78));
b.insert(new Integer(-8));
b.insert(new Integer(-10));
b.insert(new Integer(-2));
b.insert(new Integer(-3));
b.printTree();
}
}
/**
* Binary search tree
* implemented using integers.
@ -47,7 +57,9 @@ class BinarySearchTree { @@ -47,7 +57,9 @@ class BinarySearchTree {
this.element = e;
}
public String toString() { return this.element.toString(); }
public Integer getElement() { return this.element; }
public void setElement(Integer e) { this.element = e; }
public BinaryNode getLeft() { return this.left; }
public BinaryNode getRight() { return this.right; }
@ -61,7 +73,7 @@ class BinarySearchTree { @@ -61,7 +73,7 @@ class BinarySearchTree {
/////////////////////////
int size;
private int size;
BinaryNode root;
@ -80,10 +92,57 @@ class BinarySearchTree { @@ -80,10 +92,57 @@ class BinarySearchTree {
return this.size==0;
}
/** Create a clone
* of this current binary tree,
* and return it as a new binary tree.
*/
public BinarySearchTree clone() {
BinarySearchTree btree = new BinarySearchTree();
btree.root = clone(this.root);
return btree;
}
private BinaryNode clone(BinaryNode node) {
if(node==null) {
// Base case: leaf node
return null;
} else {
// Recursive case: non-leaf node.
// Apply definition of tree, and clone the subtrees.
return new BinaryNode(node.getElement(),
clone(node.getLeft()),
clone(node.getRight()));
}
}
/** Prints the tree to the console. */
//public void printTree() {}
public void printTree() {
printTree_r(this.root, 0);
}
/** Prints the tree to the console recursively,
* one line per node.
*/
private void printTree_r(BinaryNode node, int depth) {
if(node==null) {
return;
} else {
// perform visit action on this node
System.out.println(space(4*depth)+node);
// perform visit action on this node's children
printTree_r(node.getLeft(), depth+1);
printTree_r(node.getRight(), depth+1);
}
}
/** Just make some spaces. */
private String space(int n) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<n; i++) { sb.append(" "); }
return sb.toString();
}
// Non-recursive versions of findMin/findMax
@ -129,7 +188,7 @@ class BinarySearchTree { @@ -129,7 +188,7 @@ class BinarySearchTree {
}
// recursive case:
// keep going
return findMin_r(node.getLleft());
return findMin_r(node.getLeft());
}
/** Private method that returns the largest element in the tree, recursively.
@ -156,18 +215,64 @@ class BinarySearchTree { @@ -156,18 +215,64 @@ class BinarySearchTree {
// Insert methods:
/** Insert x into the tree, ignoring duplicates. */
public void insert(Integer x) {
// Insert at the root, let recursion sort them out
insert(x, this.root);
}
/** Insert at binary node t */
private void insert(Integer x, BinaryNode t) {
// Fundamental property of binary search trees:
if(isEmpty()) {
// Just... handle it.
this.root = new BinaryNode(x,null,null);
this.size++;
} else {
// Insert at the root, let recursion sort them out
insert_r(x, this.root);
System.out.println("recursive insertion of "+x);
System.out.println("what is root? "+this.root);
System.out.println("what is root left? "+this.root.getLeft());
System.out.println("what is root right? "+this.root.getRight());
}
}
private void insert_r(Integer x, BinaryNode node) {
// Key to getting the weirdness worked out
// was not dealing with the null case -
// once we get to the null pointer,
// we lose track of what we were originally pointing to
// (like, the root node or the child node or whatever).
// Instead, check for the null case, and handle it right there.
if(x < node.getElement()) {
// Go left or insert left
if(node.left==null) {
node.left = new BinaryNode(x,null,null);
size++;
} else {
insert_r(x, node.left);
}
} else if(x > node.getElement()) {
// Go right
if(node.right==null) {
node.right = new BinaryNode(x,null,null);
size++;
} else {
insert_r(x, node.right);
}
} else {
// Matches something in the tree
// Do nothing
}
}
// Search methods:
@ -183,6 +288,7 @@ class BinarySearchTree { @@ -183,6 +288,7 @@ class BinarySearchTree {
if(t==null) {
// Base case #1 - I'm empty
return false;
} else if( x < t.getElement() ) {
// Go left
return contains(x, t.getLeft());
@ -207,8 +313,47 @@ class BinarySearchTree { @@ -207,8 +313,47 @@ class BinarySearchTree {
remove(x, this.root);
}
private void remove(Integer x, BinaryNode t) {
/** Private recursive removal operation.
* As with many data structures,
* removal is tricky.
*
* If node is a leaf - delete immediately.
* If the node has one child - adjust parent's link to bypass to child.
* If the node has two childre:
* - Replace the data of this node with smallest data of right subtree.
* - Recursively delete that node, which is now empty.
* - Smallest node in right subtree cannot have left child
*
* x is item to remove. node is root of subtree.
* */
private void remove(Integer x, BinaryNode node) {
if(node==null) {
// Item not found, return nothing to caller
return;
}
if( x < node.getElement() ) {
// Not equal, keep looking.
// Go left
remove(x, node.getLeft());
} else if( x > node.getElement() ) {
// Not equal, keep looking.
// Go right
remove(x, node.getRight());
} else if(node.getLeft()!=null && node.getRight()!=null) {
// Equal: two children case.
// Find smallest value in right subtree,
// set current node value to it.
BinaryNode min = findMin(node.getRight());
node.setElement(min.getElement());
} else {
// Equal: one child case
//BinaryNode old = node;
// Pick left or right.
node = (node.getLeft()!=null)?node.getLeft():node.getRight();
size--;
}
}

Loading…
Cancel
Save