Download presentation
1
Chapter 7. Binary Search Trees
Lecture 14 Chapter 7. Binary Search Trees Set ADT Introduction to trees/binary trees traversals Binary search trees -BST implementation of set ADT ADS2 Lecture 14
2
Sets A set is a collection of distinct objects
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: F = { } More examples of sets? See board
3
Set ADT A (very) simplified version of the java.util Set interface
public interface Set<E> extends Iterable<E>{ /**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(); } ADS2 Lecture 14
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: See Sets folder from moodle Array Sorted Array Linked list add O(1) O(log n) contains O(n) O(s) remove ADS2 Lecture 14
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
Complexity of set operations using binary search tree
If we implement the set as a binary search tree the time complexities are: Array Sorted Array Linked list Binary search tree add O(1) O(log n) O(log s) contains O(n) O(s) remove Will come back to set ADT later on. For now will look at BT (or BST) in own right. ADS2 Lecture 14
7
Trees A tree is either empty or is a collection of nodes consisting of a root node r and Zero or more subtrees T1,…,Tk 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. r Note: In practice we omit arrows, assume all lines pointing down T2 T3 Tk T1 . . . See board for examples ADS2 Lecture 14
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 ni to nj, then ni is the parent of nj and nj is the child of ni For two nodes n1 and nk, a path from n1 to nk is a sequence of nodes n1,n2, … , nk such that ni is the parent of ni+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
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. recursive definition! Examples, see board Remember: to be a tree need unique path from root to every node Binary: every node has at most 2 children. ADS2 Lecture 14
10
Balance A typical binary tree: 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. ADS2 Lecture 14
11
Node of binary tree Binary tree node: A question:
How would we represent the nodes of a tree (not binary)? We don’t know how many children each node has Binary tree node: public class BTNode<E>{ private E element; private BTNode<E> left; private BTNode<E> 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<E> l,BTNode<E> r){ element = e; left=l; right=r; plus getters and setters ADS2 Lecture 14
12
Definitions 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 ADS2 Lecture 14
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
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
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
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
Algorithm Inorder(B): Input:Binary Tree B with root r Output: String representation of inorder traversal of B if B is not empty then BL left subtree of B BR right subtree of B return Inorder(BL) + r + Inorder(BR) else return "" We can implement this using recursion in Java, just like we did for singly linked lists. ADS2 Lecture 14
18
Inorder traversal in Java, using recursion
Approach 1 (Similar to Linked list approach) Assume following instance variables and constructors in BinaryTree class: public class BinaryTree<E extends Comparable<E>> { private BTNode<E> root; private int size; public BinaryTree(){ root=null; size=0; } /**constructor to create new tree with specific node as root*/ public BinaryTree(BTNode<E> p){ root=p; //haven’t defined size - come back to this ADS2 Lecture 14
19
Inorder traversal in Java contd.
/**create string representing inorder traversal of tree*/ public String toString(){ if (root==null) return ""; else{ BSTree<E> tempTreeL=new BinaryTree<E>(root.getLeft()); BSTree<E> tempTreeR=new BinaryTree<E>(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. ADS2 Lecture 14
20
Inorder traversal in Java contd.
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). ADS2 Lecture 14
21
Inorder traversal in Java contd.
Approach 2 (we use this one) –assume same instance variables, and first constructor as before (but not second, won’t need it now). /**create string from node p using inorder traversal*/ public String toString(BTNode<E> 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) ADS2 Lecture 14
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
Implementing search in a binary search tree
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
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) r right child of p return Contains(B,e,r) else return false ADS2 Lecture 14
25
Examples from last week:
b c d e (i) inorder: e,d,c,b,a preorder: a,b,c,d,e postorder: e,d,c,b,a a b e d c (ii) inorder: e,c,b,d,a preorder: a,b,c,e,d postorder: e,c,d,b,a ADS2 Lecture 14
26
(iii) 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 (iv) ADS2 Lecture 14
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.