Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT Lecture 14.

Similar presentations


Presentation on theme: "1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT Lecture 14."— Presentation transcript:

1 1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT Lecture 14 ADS2 Lecture 14

2 2 Sets Mathematical notation:{1,5,3} Operations: –Membership : 3 {1,3,5} true, 3 {1,2,6} false (say 3 {1,2,6}) – Union : {1,3,5} {2,5} = {1,2,3,5} {1,3,5} {5} = {1,3,5} –Subtraction - : {1,3,5} - {2,5} = {1,3} {1,3,5} - {4} = {1,3,5} –Intersection : {1,3,5} {2,5} = {5} Special case, the empty set: = { } More examples of sets? See board A set is a collection of distinct objects

3 Set ADT ADS2 Lecture 14 3 public interface Set extends Iterable { /**Adds specified element to set if not already present*/ public boolean add(E e); /**Removes all of the elements from this set*/ public void clear(); /**Returns true if set contains specified element*/ public boolean contains(E e); /**Returns true if set contains no elements*/ public boolean isEmpty(); /**Removes specified element from this set, if present*/ public boolean remove(E e); /**Returns number of elements in set (cardinality)*/ public int size(); } A (very) simplified version of the java.util Set interface

4 Array and linked list implementations of set Simple (extendible) array implementation Sorted (extendible) array implementation Linked list implementation Let n=size of array and s=current size of set. Complexities are: ADS2 Lecture 144 See Sets folder from moodle ArraySorted Array Linked list addO(1)O(log n)O(1) containsO(n)O(log n)O(s) removeO(n)O(log n)O(s)

5 5 Implementations of set contd. Using a sorted array, contains and remove operations are faster, thanks to binary search But then we have to fix the max size of the set (or extend array), and this is expensive spacewise There is another option which allows us to use a technique similar to binary search on a dynamic data structure…. A binary search tree… (a special sort of binary tree) ADS2 Lecture 14

6 6 Complexity of set operations using binary search tree If we implement the set as a binary search tree the time complexities are: ADS2 Lecture 14 ArraySorted Array Linked list Binary search tree addO(1)O(log n)O(1)O(log s) containsO(n)O(log n)O(s)O(log s) removeO(n)O(log n)O(s)O(log s) Will come back to set ADT later on. For now will look at BT (or BST) in own right.

7 7 Trees A tree is either empty or is a collection of nodes consisting of –a root node r and –Zero or more subtrees T 1,…,T k each of whose roots are connected via a directed edge to r. The root of each subtree is said to be a child for r, and r is the parent of each subree. Every non-root node has a unique parent. See board for examples r T1T1 T2T2 T3T3 TkTk... Note: In practice we omit arrows, assume all lines pointing down ADS2 Lecture 14

8 8 What do we mean by a path in a tree? From recursive definition of tree see that if there is a (directed) edge from node n i to n j, then n i is the parent of n j and n j is the child of n i For two nodes n 1 and n k, a path from n 1 to n k is a sequence of nodes n 1,n 2, …, n k such that n i is the parent of n i+1 for 1 i k For any path, the length is the no. of edges on the path For any node n, the depth of n is the length of the path from n to the root Depth of a tree is the depth of its deepest node. If we call the node containing x, x then path from e to a is e,c,b,a depth of a is 3 depth of tree is 3 ADS2 Lecture 14

9 9 Binary Trees A binary tree is a tree in which each node has A reference to a left node A value A reference to a right node A binary tree is either empty or Contains a single node r (the root) whose left and right subtrees are binary trees The tree is accessed via a reference to the root. The nodes with both child references null are the leaves. Examples, see board Remember: to be a tree need unique path from root to every node Binary: every node has at most 2 children. recursive definition! ADS2 Lecture 14

10 10 This tree is quite well balanced. An extreme unbalanced tree might have no right pointers - would look more like a linked list. Generally tree-based algorithms work most efficiently on balanced trees. A binary tree in which no value occurs in more than one node is injective. We deal only with injective binary trees. Balance A typical binary tree: ADS2 Lecture 14

11 11 Node of binary tree Binary tree node: ADS2 Lecture 14 public class BTNode { private E element; private BTNode left; private BTNode right; /**Creates a node with null references*/ public BTNode(){ this(null,null,null); } /** Creates node with the given element, L and R nodes*/ public BTNode(E e, BTNode l,BTNode r){ element = e; left=l; right=r; } A question: How would we represent the nodes of a tree (not binary)? We dont know how many children each node has plus getters and setters

12 Definitions ADS2 Lecture 1412 p a node of a binary tree p.left - node of a binary tree - the left subtree p.right - node of a binary tree - the right subtree if p.left=q - p is the parent of q and q is the left child of p if p.right=q - p is the parent of q and q is the right child of p. p Left subtree of p Right subtree of p

13 13 Traversals A traversal of a binary tree is a sequence of nodes of the tree. Special traversals: 1. Inorder traversal - defined recursively The inorder traversal of an empty tree is the empty sequence The inorder traversal of a non-empty tree is: the inorder traversal of the left subtree (a sequence) the root value (a sequence with one member) the inorder traversal of the right subtree (a sequence) ADS2 Lecture 14

14 14 Example (inorder traversal) Inorder traversal is a b c d e f g h i j Examples for you, see board. There will be some additional examples (of all types of traversal) on the examples sheet (some from previous exam papers..) ADS2 Lecture 14

15 15 Traversals contd. 2. Preorder traversal – defined recursively The preorder traversal of an empty tree is the empty sequence The preorder traversal of a non-empty tree is: the root value (a sequence with one member) the preorder traversal of the left subtree (a sequence) the preorder traversal of the right subtree (a sequence) Preorder traversal is e c b a d i g f h j Examples for you, see board. ADS2 Lecture 14

16 16 Traversals contd. Postorder traversal defined recursively: The postorder traversal of an empty tree is the empty sequence The postorder traversal of a non-empty tree is: the postorder traversal of the left subtree (a sequence) the postorder traversal of the right subtree (a sequence) the root value (a sequence with one member) Postorder traversal is a b d c f h g j i e Examples for you, see board. ADS2 Lecture 14

17 Inorder traversal in pseudocode ADS2 Lecture 1417 Algorithm Inorder(B): Input:Binary Tree B with root r Output: String representation of inorder traversal of B if B is not empty then B L left subtree of B B R right subtree of B return Inorder(B L ) + r + Inorder(B R ) else return "" We can implement this using recursion in Java, just like we did for singly linked lists.

18 Inorder traversal in Java, using recursion ADS2 Lecture 1418 public class BinaryTree > { private BTNode root; private int size; public BinaryTree(){ root=null; size=0; } /**constructor to create new tree with specific node as root*/ public BinaryTree(BTNode p){ root=p; //havent defined size - come back to this } Approach 1 (Similar to Linked list approach) Assume following instance variables and constructors in BinaryTree class:

19 Inorder traversal in Java contd. ADS2 Lecture 1419 /**create string representing inorder traversal of tree*/ public String toString(){ if (root==null) return ""; else{ BSTree tempTreeL=new BinaryTree (root.getLeft()); BSTree tempTreeR=new BinaryTree (root.getRight()); return tempTreeL + " " + root.getElement()+ " " + tempTreeR; } The non-recursive version takes about 50 of lines of code. This approach almost works. But the new tree we have defined using constructor has no size value defined (so not really a binary tree). Would either have to remove the size instance variable (but we want size() operation to be fast) or calculate size of binary tree starting from given node whenever we do recursion: expensive.

20 ADS2 Lecture 1420 So instead we implement recursion without using constructor, but have to refer to the root of our tree in all our (recursive) methods. Would also use this approach on linked lists if size instance variable included. To avoid having to refer to nodes in external methods, can (like you did in your lab exercise) add an additional method that has no parameter but calls the other one with the root (see next slide). Inorder traversal in Java contd.

21 ADS2 Lecture 1421 Inorder traversal in Java contd. /**create string from node p using inorder traversal*/ public String toString(BTNode p){ if (p==null) return ""; else return (toString(p.getLeft())+ " " + p.getElement()+ " " + p.getElement() + " " + toString(p.getRight())); } /** create string for whole tree*/ public String toString(){ return this.toString(root); } now need root node passed as parameter Could define an iterator using inorder traversal*, or one of the other two traversals… Actually, you will be doing this in your next lab exercise (Assessment 2) Approach 2 (we use this one) –assume same instance variables, and first constructor as before (but not second, wont need it now).

22 22 Binary search trees A binary search tree is a binary tree whose inorder traversal is in sorted order As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree There are many more (binary) search trees that have inorder tranersal a b c d e f g h i j See board. ADS2 Lecture 14

23 23 Implementing search in a binary search tree Search –Can implement binary search in O(log #s) time on average –Takes longer if the tree is badly balanced For every node X, value in all nodes in left subtree of X are less than (or equal to)* value in X, and value of all nodes in right subtree are greater than (or equal to)* value in X Algorithm is simple: if x < root then search left subtree if x > root then search right subtree When the tree is balanced the path length to the leaves is log #s *but we only want injective BSTs, so not equal to Example, see board ADS2 Lecture 14

24 Search in binary search tree, pseudocode ADS2 Lecture 1424 Algorithm Contains(B,e,p): Input:Binary Tree B with root p, and element e Output: whether B contains e if B is not empty then if root.element=e then return true else if root.element> e then l left child of p return Contains(B,e,l) else r right child of p return Contains(B,e,r) else return false

25 ADS2 Lecture 1425 a b c d e inorder: e,d,c,b,a preorder: a,b,c,d,e postorder: e,d,c,b,a a b e d c inorder: e,c,b,d,a preorder: a,b,c,e,d postorder: e,c,d,b,a Examples from last week: (i) (ii)

26 ADS2 Lecture 1426 a d e f h g b c inorder: d,c,e,g,f,h,b,a preorder: a,b,c,d,e,f,g,h postorder: d,g,h,f,e,c,b,a c e g f a b d inorder: b,a,d,c,f,e,g preorder: a,b,c,d,e,f,g postorder: b,d,f,g,e,c,a (iii) (iv)


Download ppt "1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT Lecture 14."

Similar presentations


Ads by Google