Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Zynga Slingo Trainer v5.12
iTunes Gift Card Generator V3.1 2012
Diablo 3 GOLD Coins FREE
Working PS3 Jailbreak 3.65 And 3.66
ExtaBit Premium Accounts and Cookies
Steam Wallet Hack - Money Adder & Hack v3
Empires & Allies Hack Cheat Trainer v5.4.1
Eve Onnline 60 Days Time Card Generator v2
Xbox Lve Generator v3
Better way to get content via jQuery $.get()
Popular
XBOX POINTS GENERATOR - MICROSOFT POINTS GENERATOR v1.2012
11 may 2012 premium uploading accounts 100% working
Free Microsoft Points
Free Microsoft Points - Microsoft Points Generator - Xbox Live Codes 2012
Car Town Free Blue Points Hack
Free CarTown Blue Points Generator and CarTown Templates
Free Microsoft Points
Simple Days Purger
Sharecash Downloader Bypass Surveys New 05/2012
PAYPAL REMOVE ACCESS LIMITED ACCOUNT 100% Working
Pastable version of
first BST
<pre class='prettyprint' language='java'>package BST; import exceptions.DuplicateItemException; // Here I implement a very basic binary search tree, which // checks for duplicate items. The tree is not balanced, // so it is possible for an instance of this class to // degenerate into a linked list (thus rendering any // operations that would normally be logarithmic in regards // to time would be linear. public class BST<E> { // Node is a statically-nested class. As such, it doesn't // have access to any other member of the enclosing class, // BST. @SuppressWarnings("unchecked") private static class Node<Comparable> { public Node left; public Node right; public Comparable data; public Node(Comparable data, Node left, Node right) { this.left = null; this.right = null; this.data = data; } } private Node<Comparable> root; private int count; public BST() { root = null; count = 0; } public boolean isEmpty() { return root == null; } // This contains() method only delegates work // to the recusrive contains() method. public boolean contains (Comparable data) { return contains(root, data); } @SuppressWarnings("unchecked") private boolean contains (Node ref, Comparable data) { if (ref == null) return false; else if (data.compareTo(ref.data) == 0) return true; else if (data.compareTo(ref.data) < 0) return contains (ref.left, data); else if (data.compareTo(ref.data) > 0) return contains (ref.right, data); else return false; } // This add() method only delegates the work of // actually inserting an item to the insert() // method. @SuppressWarnings("unchecked") public void add (Comparable data) throws DuplicateItemException { root = insert(root, data); } private Node insert (Node current, Comparable data) throws DuplicateItemException { if(current == null) current = new Node(data, null, null); else if (data.compareTo(current.data) < 0) current.left = insert (current.left, data); else if (data.compareTo(current.data) > 0) current.right = insert (current.right, data); else if (data.compareTo(current.data) == 0) throw new DuplicateItemException (data.toString()); count++; return current; } // This method just delegates the work of // traversing the tree to the print() // method. public void printAll() { print(root); } // This method does an in-order traversal // of the tree in order to print the value // of each node. private Node print (Node current) { if (current == null) return current; else { print (current.left); System.out.println(current.data); print (current.right); return current; } } @SuppressWarnings("unchecked") public void remove(Comparable data) { root = remove (root, data); } @SuppressWarnings("unchecked") private Node remove (Node current, Comparable data) { if (data.compareTo(current.data) == 0) { // Node has a right subtree, but no left subtree. if (current.left == null && current.right != null) { Comparable childData = (Comparable) current.right.data; current.data = childData; current.right = null; } // Node has a left subtree, but no right subtree. else if (current.left != null && current.right == null) { Comparable childData = (Comparable) current.left.data; current.data = childData; current.left = null; } // Node is a leaf node. else if (current.left == null && current.right == null) current = null; // Node has both a left & right subtree. else { Node successor = findInorderSuccessor(current.right); current.data = successor.data; current.right = successor.right; } count--; return current; } else { if (data.compareTo(current.data) > 0) { current.right = remove (current.right, data); return current; } else { current.left = remove (current.left, data); return current; } } } @SuppressWarnings("unchecked") private Node findInorderSuccessor(Node current) { while (current.left != null) current = current.left; return current; } }</pre> <a href="http://www.refactormycode.com/codes/1979-first-bst" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>