COMP 103 Binary Search Trees III 2 use
COMP 103 Binary Search Trees (III)
2 use of Linked. Nodes to implement a List Linked. Node can step along recursively, or. . . the calling class can do the same iteratively, with a reference (e. g. we called this "rest") Linked. List could be an implementation of List not much more than ref to a Linked. Node (now empty List ok ✔) a List needs an iterator, that keeps track of where it is up to List step along the list, with a ref to "next" (exactly like "rest"). . . needs to implement contains, add and remove: these can be done iteratively by Linked. List, or. . .
3 cf: use of Tree. Nodes to implement a Set Tree. Node can do a pre-order traversal recursively (although not breadth-1 st) or. . . the calling class can do same thing iteratively, with a Stack BSTSet which could be an O(log n) implementation of Set not much more than ref to a Tree. Node (& now empty Set ok ✔) a Set needs an iterator, that keeps track of where it is up to Set needs to implement contains, add, remove and so
recall: Depth-1 st traversal, iteratively with Stack 4 private void Depth. First. Traversal(Tree. Node root) { Stack<Tree. Node> todo = new Stack<Tree. Node>(); todo. push(root); pre-order while (!todo. is. Empty()) { Tree. Node node = todo. pop(); System. out. println(node. employee()); for (Tree. Node child : node. get. Children()) todo. push(child); } Justine } Jeremy John Jules Julia Jordan Jada Jack Jenna Jake James Jackie Jacob
An iterator() method for BST Set 5 Returns an iterator that steps through all items of the set Could easily do this breadth-first or pre-order depth-first. . But for a BSTSet, it would be best to do an in-order traversal! The Iterator has to store the state of the iteration, which means mimicking the behaviour of an iterative, in-order traversal ⇒ Use a stack Initialise by putting root on stack, and all its left descendants ⇒ Each call to next() should do Required one iteration of the loo for an Remove top node from stack in-order traversal (cf. pre-order, which puts both Put right child of node onto stack, and all its left children on stack)
Binary Tree Iterator (In Order) 6 Need to also push all left hand descendants when add node to stack: root 5 3 1 Jack Jeremy Jade 4 Joshua 7 Jasmine John 9 8 Julia 6 2 Jacob Justin
Binary Tree Iterator (In Order) 7 Need to push all left hand children when add node to stack: Need to add right child to stack when process a node: Jack root 5 Jade Jeremy 3 1 Jack Jade 4 Joshua 7 Jasmine John 9 8 Julia 6 2 Jacob Justin
Binary Tree Iterator (In Order) 8 Need to push all left hand children when add node to stack: Need to add right child to stack when process a node: Jasmine root Jade 5 3 1 Jack Jeremy Jade 4 Jacob Jasmine John 9 8 Julia 6 2 Jacob Jack Joshua 7 Justin Jeremy
BSTSet: Iterator (private class inside BSTSet class) 9 private class BSTSet. Iterator implements Iterator<E> { Stack<BSTNode> stack = new Stack<BSTNode>(); public BSTSet. Iterator (BSTNode root) { for (BSTNode nd = root; nd != null; nd = nd. left) stack. push(nd); } public boolean has. Next() { return !stack. is. Empty(); } public E next() { } } if (stack. is. Empty()) throw new No. Such. Element. Exception(); BSTNode node = stack. pop(); for (BSTNode nd = node. right; nd != null; nd = nd. left) stack. push(nd); return node. value;
- Slides: 9