1 / 16

TreeBag

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)

shad-mendez
Download Presentation

TreeBag

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TreeBag a BST implementation

  2. 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)

  3. 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()

  4. TreeBag class TreeBag BSTNode treeBag root

  5. 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)

  6. 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

  7. class TreeBag public class TreeBag implements Cloneable { BSTNode root; public TreeBag() { root = null; } … }

  8. 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); } }

  9. 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()?

  10. 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; }

  11. 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; }

  12. 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; }

  13. TreeBag - addAll TreeBag t1 TreeBag t2 t1.addAll(t2); root root TreeBag t1 TreeBag t2 root root

  14. 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); }

  15. TreeBag - addTree private void addTree(BSTNode addroot) { if (addroot != null) { add(addroot.getData( )); addTree(addroot.getLeft( )); addTree(addroot.getRight( )); } }

  16. TreeBag - union public static TreeBag union(TreeBag b1, TreeBag b2) { TreeBag answer = new TreeBag( ); answer.addAll(b1); answer.addAll(b2); return answer; }

More Related