29
Binary Search Trees Nilanjan Banerjee

Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

Embed Size (px)

Citation preview

Page 1: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

Binary Search Trees

Nilanjan Banerjee

Page 2: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

2

Goal of today’s lectureLearn about Binary Search Trees

Discuss the first midterm

Page 3: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

3

Binary search treesbinary search tree ("BST"): a binary tree where each non-

empty node R has the following properties:elements of R's left subtree contain data "less than" R's data,elements of R's right subtree contain data "greater than" R's,R's left and right subtrees are also binary search trees.

BSTs store their elements insorted order, which is helpfulfor searching/sorting tasks.

9160

8729

55

42-3

overall root

Page 4: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

4

BST examplesWhich of the trees shown are legal binary search trees?

xk

qg

m

e

b 1810

115

8

4

2 7

20

18

42

-7-1

-5

21.38.1

9.61.9

7.2

Page 5: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

5

Searching a BSTDescribe an algorithm for searching a binary search tree.

Try searching for the value 31, then 6.

What is the maximumnumber of nodes youwould need to examineto perform any search?

What is the computationalComplexity of searching?

12

18

7

4 15

overall root

-2 1613

35

31

22 58

19 8740

Page 6: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

6

Exercise

Write a contains method that takes advantage of the BST structure.

tree.contains(29) true tree.contains(55) true tree.contains(63) false tree.contains(35) false

9160

8729

55

42-3

overall root

Page 7: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

7

Exercise solution// Returns whether this BST contains the given integer.public boolean contains(int value) { return contains(overallRoot, value);}

private boolean contains(IntTreeNode node, int value) { if (node == null) { return false; // base case: not found here } else if (node.data == value) { return true; // base case: found here } else if (node.data > value) { return contains(node.left, value); } else { // root.data < value return contains(node.right, value); }}

Page 8: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

8

Adding to a BSTSuppose we want to add new values to the BST below.

Where should the value 14 be added?Where should 3 be added? 7?

If the tree is empty, whereshould a new value be added?

What is the general algorithm?

1910

115

8

4

2 7

25

22

overall root

Page 9: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

9

Adding exerciseDraw what a binary search tree would look like if the

following values were added to an initially empty tree in this order:

50207598803115039231177

50

20 75

80

9811

39

31

15023

77

Page 10: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

10

ExerciseAdd a method add to the SearchTree class that adds a

given integer value to the BST.Add the new value in the proper place to maintain BST

ordering.

tree.add(49);

9160

8729

55

42-3

overall root

49

Page 11: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

11

An incorrect solution// Adds the given value to this BST in sorted order.public void add(int value) { add(overallRoot, value);}

private void add(IntTreeNode node, int value) { if (node == null) { node = new IntTreeNode(value); } else if (node.data > value) { add(node.left, value); } else if (node.data < value) { add(node.right, value); } // else node.data == value, so // it's a duplicate (don't add)}

Why doesn't this solution work?

9160

8729

55

42-3

overallRoot

Page 12: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

The x = change(x)pattern

Page 13: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

13

A tangent: Change a pointWhat is the state of the object referred to by p after this

code?

public static void main(String[] args) { Point p = new Point(1, 2); change(p); System.out.println(p);}

public static void change(Point thePoint) { thePoint.x = 3; thePoint.y = 4;}

// answer: (3, 4)

2y1xp

Page 14: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

14

Change point, version 2What is the state of the object referred to by p after this

code?

public static void main(String[] args) { Point p = new Point(1, 2); change(p); System.out.println(p);}

public static void change(Point thePoint) { thePoint = new Point(3, 4);}

// answer: (1, 2)

2y1xp

4y3x

Page 15: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

15

Changing referencesIf a method dereferences a variable (with . ) and modifies

the object it refers to, that change will be seen by the caller.

public static void change(Point thePoint) { thePoint.x = 3; // affects p thePoint.setY(4); // affects p

If a method reassigns a variable to refer to a new object, that change will not affect the variable passed in by the caller.

public static void change(Point thePoint) { thePoint = new Point(3, 4); // p unchanged thePoint = null; // p unchanged

What if we want to make the variable passed in become null?

Page 16: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

16

Change point, version 3What is the state of the object referred to by p after this

code?

public static void main(String[] args) { Point p = new Point(1, 2); change(p); System.out.println(p);}

public static Point change(Point thePoint) { thePoint = new Point(3, 4); return thePoint;}

// answer: (1, 2)

2y1xp

4y3x

Page 17: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

17

Change point, version 4What is the state of the object referred to by p after this

code?

public static void main(String[] args) { Point p = new Point(1, 2); p = change(p); System.out.println(p);}

public static Point change(Point thePoint) { thePoint = new Point(3, 4); return thePoint;}

// answer: (3, 4)

2y1xp

4y3x

Page 18: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

18

x = change(x);If you want to write a method that can change the object

that a variable refers to, you must do three things:1. pass in the original state of the object to the method2. return the new (possibly changed) object from the method3. re-assign the caller's variable to store the returned result

p = change(p); // in main

public static Point change(Point thePoint) { thePoint = new Point(99, -1); return thePoint;

Page 19: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

19

The problemMuch like with linked lists, if we just modify what a local

variable refers to, it won't change the collection.

private void add(IntTreeNode node, int value) { if (node == null) { node = new IntTreeNode(value); }

9160

8729

55

42-3

overallRoot

49node

Page 20: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

20

A correct solution// Adds the given value to this BST in sorted order.public void add(int value) { overallRoot = add(overallRoot, value);}

private IntTreeNode add(IntTreeNode node, int value) { if (node == null) { node = new IntTreeNode(value); } else if (node.data > value) { node.left = add(node.left, value); } else if (node.data < value) { node.right = add(node.right, value); } // else a duplicate; do nothing

return node;}

What happens when node is a leaf?

9160

8729

55

42-3

overallRoot

Page 21: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

21

21

DeletionThere are the following possible cases when we delete

a node:The node to be deleted has no children. In this case,

all we need to do is delete the node.The node to be deleted has only a right subtree. We

delete the node and attach the right subtree to the deleted node’s parent.

The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node’s parent.

The node to be deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees.

Page 22: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

22

22

Deletion from the middle of a treeRather than simply delete the node, we try to maintain the

existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways.

Page 23: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

23

23

Deletion from the middle of a treeWe can find the largest node in the deleted node’s left

subtree and move its data to replace the deleted node’s data.

We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data.

Either of these moves preserves the integrity of the binary search tree.

Page 24: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

24

24

Page 25: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

25

25

(continued)

Page 26: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

26

26

27 27

27 27

Page 27: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

27

27

Performance of BST methods What is the asymptotic performance of each

of the BST methods?

Best Case Worst Case Average Case

searching

insert

remove

Page 28: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

28

28

Exam ReviewTopic 1: Java Review

Generics in Java, Type Erasure, Virtual Fuction invocation caused by Inheritance

Asymptotic AnalysisBig Oh notation --- you should be able to look at an

implementation and figure it running time in Big OhL’Hospital Rule

Linked ListUnderstand the difference between using an ArrayList and a

LinkedListRunning time of a LinkedList insertion, deletion, searching an

elementOne problem where you will have to manipulate a linked listStack and Queues and Adapter Classes

Page 29: Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

29

29

Exam Trees

Definition of a treeOne algorithmic question with treesHow to traverse a tree

Recursive implementation as well as the non-recursive implementation

Inorder, Preorder, Postorder, LevelOrder