1 / 29

Binary Trees

Binary Trees. Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRStruct You are expected to read about alternate implementation strategies in the textbook. Methods of a Binary Recursive Structure (BRStruct).

pilar
Download Presentation

Binary Trees

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. Binary Trees • Like a list, a (binary) tree can be empty or non-empty. • In class we will explore a state-based implementation, similar to the LRStruct • You are expected to read about alternate implementation strategies in the textbook

  2. Methods of aBinary Recursive Structure (BRStruct) • method to grow tree: insertRoot • method to shrink tree: removeRoot • accessor/mutator pair for datum: setDatum/getDatum • accessor/mutator pair for left: setLeft/getLeft • accessor/mutator pair for right: setRight/getRight • visitor support: execute

  3. State transitions • Empty  NonEmpty: insertRoot on an empty tree • NonEmpty  Empty: removeRoot from a tree that is a leaf • A leaf is a tree both of whose children are empty. • No other state transitions can occur.

  4. Moreover… • insertRoot throws an exception if called on a nonEmpty tree • removeRoot throws an exception if called on a non-leaf tree • Isn’t this very restrictive?

  5. Yes…but • It is very restrictive, but for good reasons: • What would it mean to add a root to a tree that already has one? • If you could remove the root from a tree with nonEmpty children, what would become of those children?

  6. Usage of a BRS • A BRStruct is not used “raw”, but is used to implement more specialized trees. • Consider how we defined the SortedList in terms of the LRStruct: by composition. • We will now explore how to define a sorted binary tree (a Binary Search Tree) by composition with a BRStruct.

  7. Binary Search Tree (BST) • A binary search tree (BST) is a binary tree which maintains its elements in order. • The BST order condition requires that, for every non-empty tree, the value at the root is greater than every value in the tree’s left subtree, and less than every value in the tree’s right subtree.

  8. Example 1Does this tree satisfy the BST order condition? Fred Betty Wilma Barney Pebbles

  9. No!Barney < Betty < Pebbles > Fred < Wilma Fred Betty Wilma Barney Pebbles

  10. Example 2Does this tree satisfy the BST order condition? Fred Betty Wilma Barney Pebbles

  11. Yes!Barney < Betty < Fred < Pebbles < Wilma Fred Betty Wilma Barney Pebbles

  12. Example 3Does this tree satisfy the BST order condition? Fred Betty Wilma Barney Pebbles

  13. No!Betty > Barney < Fred < Pebbles < Wilma Fred Betty Wilma Barney Pebbles

  14. Example 4…but if we swap Betty & Barney, we restore order! Fred Barney Wilma Betty Pebbles

  15. Operations on aBST<E extends Comparable> • public BST insert(E item) • public BST remove(E item) • public boolean member(E item) • How do we support these? They don’t exist as methods on the BRStruct.

  16. Visitors to the rescue!  • Visitors on the BRStruct have the same basic structure as on the LRStruct: they deal with two cases: emptyCase and nonEmpty Case. • In our example: public BRStruct<E> emptyCase(BRStruct<E> host, E arg) public BRStruct<E> nonEmptyCase(BRStruct<E> host, E arg)

  17. Example: toStringVisitor public class ToStringVisitor extends IAlgo { public static final ToStringVisitor SINGLETON = new ToStringVisitor(); private ToStringVisitor() {} public Object emptyCase(BRS host, Object input) { return "[]"; } public Object nonEmptyCase(BRS host, Object input) { return "[" + host.getLeft().execute(this, null) + " " + host.getRoot().toString() + " " + host.getRight().execute(this, null) + "]"; } }

  18. Back to the BST • Let’s first consider the insert operation. • We must consider two possibilities: • the underlying BRStruct is empty • just insertRoot • the underlying BRStruct is nonEmpty • we can’t insertRoot, because the BRStruct is nonEmpty • compare new item with root – determine whether new item belongs in left or right subtree – insert recursively into correct subtree • EXERCISE: DEFINE THIS VISITOR

  19. A first cut at the visitor public class InsertVisitor<E extends Comparable> implements IAlgo<E,E,Object> { public Object emptyCase(BRStruct<E> host, E item) { return host.insertRoot(item); } public Object nonEmptyCase(BRStruct<E> host, E item) { if (item.compareTo(host.getDatum()) < 0 ) { // item belongs in left subtree return host.getLeft().execute(this, item); } else if (item.compareTo(host.getRoot()) > 0 ) { // item belongs in right subtree return host.getRight().execute(this, item); } else { // item is already in tree (Note UNIQUENESS ASSUMPTION) return host; } } }

  20. How about determining membership for an item? • We must consider two possibilities: • the underlying BRStruct is empty • just item was not found • the underlying BRStruct is nonEmpty • compare new item with root – determine whether new item has been found, or whether it would be in left or right subtree – look recursively into correct subtree • EXERCISE: DEFINE THIS VISITOR

  21. A first cut at the visitor public class MemberVisitor<E extends Comparable> implements IAlgo<E,E,Boolean> { public Boolean emptyCase(BRStruct<E> host, E item) { return false; } public Boolean nonEmptyCase(BRStruct<E> host, E item) { if (item.compareTo(host.getDatum()) < 0 ) { // item belongs in left subtree return host.getLeft().execute(this, item); } else if (item.compareTo(host.getRoot()) > 0 ) { // item belongs in right subtree return host.getRight().execute(this, item); } else { // item is in tree return true; } } }

  22. Did you notice similarity? • The structure of the insert and membership visitors was the same. • A small number of details differed. • Let’s unify! • EXERCISE: DEFINE THIS VISITOR

  23. The find visitor public class MemberVisitor<E extends Comparable> implements IAlgo<E,E,BRStruct<E>> { public BRStruct<E> emptyCase(BRStruct<E> host, E item) { // item is in not in tree, but would belong in host return host; } public BRStruct<E> nonEmptyCase(BRStruct<E> host, E item) { if (item.compareTo(host.getDatum()) < 0 ) { // item belongs in left subtree return host.getLeft().execute(this, item); } else if (item.compareTo(host.getRoot()) > 0 ) { // item belongs in right subtree return host.getRight().execute(this, item); } else { // item appears in tree as datum of host return host; } } }

  24. Look at BST implementation • insert, remove and member ALL make use of the same FindVisitor: • first find the insertionPoint, • then do the right thing.

  25. The insert method public BST insert(E item) { BRStruct<E> tree = _tree.execute(new FindVisitor<E>(), item); only insertiteminto insertPoint if empty (duplicates ignored) return this; } EXERCISE: SOLVE PURPLE PROBLEM

  26. The insert method public BST insert(E item) { BRStruct<E> tree = _tree.execute(new FindVisitor<E>(), item); tree.execute(new IAlgo<E,E,Object>() { public Object emptyCase(BRStruct<E> host, E input) { host.insertRoot(input); return null; } public Object nonEmptyCase(BRStruct<E> host, E input) { return null; } }, item); return this; }

  27. The member method public BST member(E item) { BRStruct<E> tree = _tree.execute(new FindVisitor<E>(), item); return whether insertPoint is empty or nonEmpty } EXERCISE: SOLVE PURPLE PROBLEM

  28. The member method public BST member(E item) { BRStruct<E> tree = _tree.execute(new FindVisitor<E>(), item); return tree.execute(new IAlgo<Object,E,Boolean>() { public Boolean emptyCase(BRS host, Object _) { return false; } public Boolean nonEmptyCase(BRS host, Object _) { return true; } }, null); }

  29. The remove method? • Next time!

More Related