Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are...

20
Binary Search Trees

Transcript of Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are...

Page 1: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Binary Search Trees

Page 2: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

BST Properties

• Have all properties of binary tree

• Items in left subtree are smaller than items in any node

• Items in right subtree are larger than items in any node

Page 3: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Items

• Items must be comparable

• All items have a unique value

• Given two distinct items x and y either– value(x) < value(y)– value(x) > value(y)

• If value(x) = value(y) then x = y

• It will simplify programming to assume there are no duplicates in our set of items.

Page 4: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Items

• Need to map Items to a numerical value

• Integers– Value(x) = x

• People– Value(x) = ssn– Value(x) = student id

Page 5: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Comparable Interface

• Want general tree code• Requirement of item is that it supports

– <– >– =

• Java uses Interfaces for implementation• Similar to abstract method• Specify a method that using class is

responsible for

Page 6: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

BST Operations

• Constructor

• Insert

• Find– Findmin– Findmax

• Remove

Page 7: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

BST Operations

• Generally Recursive

BinaryNode operation( Comparable x, BinaryNode t ) {// End of path

if( t == null ) return null;

if( x.compareTo( t.element ) < 0 )return operation( x, t.left );

else if( x.compareTo( t.element ) > 0 )return operation( x, t.right );

else return t; // Match}

Page 8: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

BST Find Method

private BinaryNode find( Comparable x, BinaryNode t ) { if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); else return t; // Match }

Page 9: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

BST Remove Operations

• Remove– Node is leaf

• Remove node

– Node has one child• Replace node with child

– Node has two children• Replace node with smallest child of right subtree.

Page 10: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Removing with value 2

6

2 8

1 5

3

4

6

3 8

1 5

3

4

6

3 8

1 5

3

4

Page 11: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Remove method private BinaryNode remove( Comparable x, BinaryNode t ) { if( t == null ) return t; // Item not found; do nothing if( x.compareTo( t.element ) < 0 ) t.left = remove( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return t; }

Page 12: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Internal Path Length

• Review depth/height

• Depth– Depth is number of path segments from root

to node– Depth of node is distance from root to that

node.– Depth is unique– Depth of root is 0

Page 13: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Internal Path Length

• Height– Height is maximum distance from node to a

leaf.– There can be many paths from a node to a

leaf.– The height of the tree is another way of

saying height of the root.

Page 14: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Internal Path Length

• IPL is the sum of the depths of all the nodes in a tree

• It gives a measure of how well balanced the tree is.

Page 15: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Internal Path Length

11

2

N = 4IPL = 1 + 1 + 2 = 4

Page 16: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Internal Path Length

1

2

N = 4IPL = 1 + 2 + 3 = 6

3

Page 17: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Average IPL for N nodesN = 4

• Calculate IPL of all possible trees

1

2

3

11

2

1

2 2

Page 18: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

BST Efficiency

• If tree is balanced O(log(n))

• No guarantee that tree will be balanced

• Analysis in book suggests on IPL = O(nlog(n))

• This analysis is based on the assumption that all trees are equally likely

• Could always get the worst case (a degenerate tree).

Page 19: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Where do BST fit in

• Simple to understand

• Works for small datasets

• Basis for more complicated trees

• Using inheritance can implement– AVL trees– Splay trees– Red Black trees

Page 20: Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.

Do your Lex