Presentation is loading. Please wait.

Presentation is loading. Please wait.

TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.

Similar presentations


Presentation on theme: "TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void."— 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 treeBagroot BSTNode TreeBag

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 622 2710 19 29 26 23 10 repeated values are leftmost elements of right subtree of value repeated 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 root TreeBag t2 root t1.addAll(t2); TreeBag t1 root TreeBag t2 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; }


Download ppt "TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void."

Similar presentations


Ads by Google