Trees and Binary Search Trees Using binary trees
Trees and Binary Search Trees • Using binary trees • Definition of a binary search tree • Implementing binary search trees – Add Element – Remove Element • Using Binary Search Trees: Ordered Lists • Reading: L&C 10. 1 – 10. 9 1
Using Binary Trees • An expression tree has an operation at the root and at each internal node with an operand at each leaf • An expression tree is evaluated from the bottom up + Infix Expression (5 - 3) * 4 + 9 Postfix Expression 5 3– 4*9+ * 5 9 4 3 2
Using Binary Trees • Expression. Tree. Obj class public class Expression. Tree. Obj { private int termtype; // node or leaf private char operator; // operator (node) or private int value // operand value (leaf) // constructor public Expression. Tree. Obj(int type, char op, int val) { // initialize the object attributes (not shown) } // typical accessor methods (also not shown) } 3
Using Binary Trees • Expression. Tree class public class Expression. Tree extends Linked. Binary. Tree<Expression. Tree. Obj> { public Expression. Tree (Expression. Tree. Obj element, Expression. Tree left. Subtree, Expression. Tree right. Subtree) { super(element, left. Subtree, right. Subtree); } public int evaluate. Tree() { return evaluate. Node(root); } } 4
Using Binary Trees • Postfix tree construction (See L&C Figure 12. 11) Assume the postfix expression is 5 3 – 4 * 9 + Create a stack Get 5 -> push(new Expression. Tree(5, null) Get 3 -> push(new Expression. Tree(3, null) Get - -> op 2 = pop() op 1 = pop() push(new Expression. Tree(‘-’, op 1, op 2) Get 4 -> push(new Expression. Tree(4, null) Get * -> op 2 = pop() op 1 = pop() push(new Expression. Tree(‘*’, op 1, op 2) Get 9 -> push(new Expression. Tree(9, null) Get + -> op 2 = pop() op 1 = pop() push(new Expression. Tree(‘+’, op 1, op 2) At end-> pop the completed Expression. Tree 5
Using Binary Trees • Recursive tree evaluation method (my version) private int evaluate. Node(Binary. Tree. Node<T> root) { if (root != null) { Expression. Tree. Obj temp = root. get. Element(); if (temp. is. Operator()) return compute. Term(temp. get. Operator(), evaluate. Node(root. get. Left()), evaluate. Node(root. get. Right())); else return temp. get. Value(); } return 0; } 6
Binary Search Tree Definition • A binary search tree is a binary tree with the added property that for each node: – The left child is less than the parent – The parent is less than or equal to the right child • Class of objects stored in a binary search tree must implement Comparable interface • Now we can define operations to add and remove elements according to search order 7
Implementing Binary Search Trees <<interface>> Binary. Tree. ADT<T> <<interface>> Binary. Search. Tree. ADT<T> OR <T extends Comparable<T>> + remove. Left. Subtree( ) : void + remove. Right. Subtree( ) : void + add. Element(element : T) : void + remove. All. Elements( ) : void + remove. Element(target : T) : T + is. Empty( ) : boolean + remove. All. Ocurrences + size( ) : int (target : T) : void <<extends>> + contains( ) : boolean + remove. Min() : T + find( ) : T + remove. Max() : T + to. String( ) : String + find. Min( ) : T + iterator. In. Order( ) : Iterator<T> + find. Max( ) : T + iterator. Pre. Order( ) : Iterator<T> + iterator. Post. Order( ) : Iterator<T> Note possible addition of + iterator. Level. Order( ) : Iterator<T> extends Comparable<T> to generic specification of <T> Note: to. String is missing in L&C Fig 10. 2 for search trees to optionally 8 force a bound on type T
Implementing Binary Search Trees <<interface>> Binary. Search. Tree. ADT<T> OR <T extends Comparable<T>> <<interface>> Binary. Tree. ADT<T> <<extends>> <<implements>> Linked. Binary. Tree<T> # count : int # root : Binary. Tree. Node<T> <<implements>> Linked. Binary. Search. Tree <T extends Comparable<T>> <<extends>> Note adding the bound of extends Comparable<T> to generic specification of <T> for search trees allows use of compare. To w/o cast 9
Implementing Binary Search Trees • Linked. Binary. Search. Tree – Header public class Linked. Binary. Search. Tree <T extends Comparable<T>> extends Linked. Binary. Tree<T> implements Binary. Search. Tree. ADT<T> – Constructors public Linked. Binary. Search. Tree() { super(); } public Linked. Binary. Search. Tree(T element) { super(element); } • Why not allow use of the overloaded three argument Linked. Binary. Tree constructor? 10
Implementing Binary Search Trees • Semantics for method add. Element Add 5 Add 7 Add 3 Add 4 5 5 7 3 7 4 11
Implementing Binary Search Trees • Linked. Binary. Search. Tree method add. Element public void add. Element(T element) { Binary. Tree. Node<T> temp = new Binary. Tree. Node<T>(element); if (is. Empty()) // using parent method root = temp; // set parent attribute root else { // need to add to existing tree Binary. Tree. Node<T> current = root; boolean added = false; 12
Implementing Binary Search Trees • Linked. Binary. Search. Tree method add. Element while (!added) { // compare. To allowed for <T extends Comparable<T>> if(element. compare. To(current. get. Element()) < 0) if (current. get. Left() == null) { current. set. Left(temp); added = true; } else current = current. get. Left(); else 13
Implementing Binary Search Trees • Linked. Binary. Search. Tree method add. Element if (current. get. Right() == null) { current. set. Right(temp); added = true; } else current = current. get. Right(); } } count++; // increment parent attribute } 14
Implementing Binary Search Trees • Semantics for method remove. Element • Can’t remove a node by making a reference point around the node being removed • Must promote another node to take its place 1. If node has no children, replacement is null 2. If node has only one child, replacement is child 3. If node has two children, replacement is the inorder successor of the node (equal elements are placed to the right) 15
Implementing Binary Search Trees • Semantics for method remove. Element Original Tree Rule 1 Remove 3 10 5 3 10 15 7 13 Rule 2 Remove 5 5 10 15 7 13 Rule 3 Remove 10 7 13 15 7 15 13 Note: Corrects error in position of leaf 13 in L&C Figure 10. 4 16
Implementing Binary Search Trees • Linked. Binary. Search. Tree method replacement protected Binary. Tree. Node<T> replacement (Binary. Tree. Node<T> node) { if (node == null) // paranoid programming? return null; if(node. get. Left() == null && node. get. Right() == null) return null; if (node. get. Left() != null && node. get. Right() == null) return node. get. Left(); if (node. get. Left() == null && node. get. Right() != null) return node. get. Right(); // easy cases done - need to work harder now 17
Implementing Binary Search Trees • Linked. Binary. Search. Tree method replacement Binary. Tree. Node<T> current = node. get. Right(); Binary. Tree. Node<T> parent = node; while (current. get. Left() != null) { parent = current; current = current. get. Left(); } if (current != node. get. Right()) { parent. set. Left(current. get. Right()); current. set. Right(node. get. Right()); } current. set. Left(node. get. Left()); return current; } 18
Using Binary Search Trees: Implementing Ordered Lists • Binary. Search. Tree. List<T> class • A List API implemented using a Binary. Search. Tree • Each List method is mapped one-to-one to a method of the Linked. Binary. Search. Tree<T> class List Linked. Binary. Search. Tree void add(T element) T remove. First() T remove. Last() T remove(T element) T first() T last() Iterator<T> iterator() void add. Element(T element) T remove. Min() T remove. Max() T remove. Element(T element) T find. Min() T find. Max() {return iterator. In. Order(); } 19
Using Binary Search Trees: Implementing Ordered Lists • Analysis of Binary. Search. Tree. List class Method remove. First remove. Last remove contains is. Empty/size add Linked. List O(1) O(n) O(1) O(n) Binary. Search. Tree. List O(log n) * O(log n) O(1) O(log n) * * Add and remove may cause tree to become unbalanced, but there is an O(log n) operation to rebalance the tree at the correct point 20
- Slides: 20