160 likes | 235 Views
TreeBag. a BST implementation. Example class - Tree Bag. remember Bag? collection of items, order does not matter repeated items allowed public void add(Comparable element) public void addAll(TreeBag addend) public Object clone( ) public int countOccurrences(Comparable target)
E N D
TreeBag a BST implementation
Example class - TreeBag • remember Bag? • collection of items, order does not matter • repeated items allowed public void add(Comparable element) public void addAll(TreeBag addend) public Object clone( ) public int countOccurrences(Comparable target) private boolean remove(Comparable target) public int size( ) public static TreeBag union(TreeBag b1, TreeBag b2)
Some BSTNode methods public boolean search(Comparable obj) public boolean insert(Comparable obj) public int size() public boolean isLeaf() public BSTNode getLeftMostData() public BSTNode removeLeftMost() public void printPreOrder() public static int height()
TreeBag class TreeBag BSTNode treeBag root
TreeBag class • data is stored in InOrder in the binary search tree (order not required for Bag) • repeated values x are stored (arbitrarily) in right subtree of original value x • effect on delete(x) • effect on countOccurrences(x)
TreeBag class 10 repeated values are leftmost elements of right subtree of value repeated 6 22 2 7 10 26 2 9 19 23 10 10 10
class TreeBag public class TreeBag implements Cloneable { BSTNode root; public TreeBag() { root = null; } … }
TreeBag - add public class TreeBag implements Cloneable { BSTNode root; … public void add (Comparable element) { if (root==null) root = new BSTNode(element,null,null); else root.insert(element); } }
TreeBag - countOccurrences public int countOccurrences(Comparable target) { // assume repeated values are inserted after int count=0; BSTNode cursor = root; while (cursor != null) { if (cursor.getData( ).compareTo(target)>0) cursor = cursor.getLeft( ); else { if (cursor.getData( ).compareTo(target)==0) count++; cursor = cursor.getRight( ); } } return count; } // write recursive version of countOccurrences()?
TreeBag - remove public boolean remove(Comparable target) // method completely in TreeBag class { BSTNode parentOfCursor = null; BSTNode cursor = root; while (cursor != null && cursor.getData().compareTo(target)!=0) { parentOfCursor = cursor; if (cursor.getData().compareTo(target)>0) cursor = cursor.getLeft( ); else cursor = cursor.getRight( ); } if (cursor == null) return false; // target not found if (cursor.getRight( ) == null) { if (parentOfCursor == null) // removing root root = cursor.getLeft( ); else if (cursor == parentOfCursor.getLeft( )) parentOfCursor.setLeft(cursor.getLeft( )); else parentOfCursor.setRight(cursor.getLeft( )); } // could include another test for right child == null else // replacing with data from next node { cursor.setData(cursor.getRight( ).getLeftmostData( )); cursor.setRight(cursor.getRight( ).removeLeftmost( )); } return true; }
TreeBag clone public Object clone( ) { TreeBag answer; try { answer = (TreeBag) super.clone( ); } catch (CloneNotSupportedException e) {throw new InternalError(e.toString( )); } answer.root = BSTNode.treeCopy(root); return answer; }
BSTNode - treeCopy public static BSTNode treeCopy(BSTNode source) { if (source == null) return null; BSTNode copy = new BSTNode(source.data,null,null); if (source.left != null) copy.left = treeCopy(source.left); if (source.right != null) copy.right = treeCopy(source.right); return copy; }
TreeBag - addAll TreeBag t1 TreeBag t2 t1.addAll(t2); root root TreeBag t1 TreeBag t2 root root
TreeBag - addAll public void addAll(TreeBag addend) { BSTNode addroot; if (addend == null) { throw new IllegalArgumentException("Null addend"); } if (root == addend.root) { // Addend is same as bag that activated method addroot = BSTNode.treeCopy(addend.root); addTree(addroot); } else addTree(addend.root); }
TreeBag - addTree private void addTree(BSTNode addroot) { if (addroot != null) { add(addroot.getData( )); addTree(addroot.getLeft( )); addTree(addroot.getRight( )); } }
TreeBag - union public static TreeBag union(TreeBag b1, TreeBag b2) { TreeBag answer = new TreeBag( ); answer.addAll(b1); answer.addAll(b2); return answer; }