26
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

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

Embed Size (px)

Citation preview

Page 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

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

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

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

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

Set ADT

ADS2 Lecture 143

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();}

A (very) simplified version of the java.util Set interface

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

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 14 4

See Sets folder from moodle

Array Sorted Array

Linked list

add O(1) O(log n) O(1)

contains O(n) O(log n) O(s)

remove O(n) O(log n) O(s)

Page 5: 1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT 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

Page 6: 1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT 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:

ADS2 Lecture 14

Array Sorted Array

Linked list

Binary search tree

add O(1) O(log n) O(1) O(log s)

contains O(n) O(log n) O(s) O(log s)

remove O(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.

Page 7: 1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT 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.

See board for examples

r

T1T2 T3 Tk. . .

Note:

In practice we omit arrows, assume all lines pointing down

ADS2 Lecture 14

Page 8: 1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT 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.

i

e

c

b

a

d jg

f h

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 3ADS2 Lecture 14

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

9

Binary TreesA 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 boardRemember: 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

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

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 efficientlyon 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:

i

e

c

b

a

d jg

f h

ADS2 Lecture 14

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

11

Node of binary treeBinary tree node:

ADS2 Lecture 14

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; }

A question:

How would we represent the nodes of a tree (not binary)?

We don’t know how many children each node has

plus getters and setters

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

Definitions

ADS2 Lecture 14 12

p a node of a binary treep.left - node of a binary tree - the left subtreep.right - node of a binary tree - the right subtreeif p.left=q - p is the parent of q and q is the left child of pif p.right=q - p is the parent of q and q is the right child of

p.

i

e

c

b

a

d jg

f h

p

Left subtree of p

Right subtree of p

Page 13: 1 Chapter 7. Binary Search Trees -Set ADT -Introduction to trees/binary trees -traversals -Binary search trees -BST implementation of set ADT 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 sequenceThe 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

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

14

Example (inorder traversal)

i

e

c

b

a

d jg

f h

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

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

15

Traversals contd.2. Preorder traversal – defined recursively

The preorder traversal of an empty tree is the empty sequenceThe 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)

i

e

c

b

a

d jg

f h

Preorder traversal is e c b a d i g f h j

Examples for you, see board.

ADS2 Lecture 14

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

16

Traversals contd.

Postorder traversal defined recursively:

The postorder traversal of an empty tree is the empty sequenceThe 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)

i

e

c

b

a

d jg

f h

Postorder traversal is a b d c f h g j i e

Examples for you, see board.

ADS2 Lecture 14

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

Inorder traversal in pseudocode

ADS2 Lecture 14 17

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.

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

Inorder traversal in Java, using recursion

ADS2 Lecture 14 18

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 }

Approach 1 (Similar to Linked list approach) Assume following instance variables and constructors in BinaryTree class:

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

Inorder traversal in Java contd.

ADS2 Lecture 14 19

/**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.

Page 20: 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 20

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.

Page 21: 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 21

Inorder traversal in Java contd.

/**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)

Approach 2 (we use this one) –assume same instance variables, and first constructor as before (but not second, won’t need it now).

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

22

Binary search trees

• A binary search tree is a binary tree whose inorder traversal is in sorted order

i

e

c

b

a

d jg

f h

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

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

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 subtreeif 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

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

Search in binary search tree, pseudocode

ADS2 Lecture 14 24

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

Page 25: 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 25

a

b

c

d

e

inorder: e,d,c,b,apreorder: a,b,c,d,epostorder: e,d,c,b,a

a

b

e

dc

inorder: e,c,b,d,apreorder: a,b,c,e,dpostorder: e,c,d,b,a

Examples from last week:

(i)

(ii)

Page 26: 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 26

a

de

f

hg

b

c

inorder: d,c,e,g,f,h,b,apreorder: a,b,c,d,e,f,g,hpostorder: d,g,h,f,e,c,b,a

c

e

gf

a

b

d

inorder: b,a,d,c,f,e,gpreorder: a,b,c,d,e,f,gpostorder: b,d,f,g,e,c,a

(iii)

(iv)