Trees Last time: recursion In the last lecture, we learned about - - PowerPoint PPT Presentation

trees last time recursion
SMART_READER_LITE
LIVE PREVIEW

Trees Last time: recursion In the last lecture, we learned about - - PowerPoint PPT Presentation

Trees Last time: recursion In the last lecture, we learned about recursion & divide-and-conquer Split the problem into smaller parts Solve each of the smaller parts separately: easier to code & understand! Apply these


slide-1
SLIDE 1

Trees

slide-2
SLIDE 2

Binary searching & introduction to trees

2 CMPS 12B, UC Santa Cruz

Last time: recursion

In the last lecture, we learned about recursion &

divide-and-conquer

Split the problem into smaller parts Solve each of the smaller parts separately: easier to code

& understand!

Apply these techniques to storing data so that it is

Ordered Easy and efficient to find

List-type structures don’t do both

Lists & arrays: ordered, but lookup is slow

We want a structure that can do both!

slide-3
SLIDE 3

Binary searching & introduction to trees

3 CMPS 12B, UC Santa Cruz

Quickly finding a particular item…

Problem: in a class of n students, who has the mth

best grade?

Use a (sorted) linked list?

Easy to find: count m links from the start Difficult to insert: must search along the list to find the

correct insertion point

Use an array?

Same kinds of advantages and disadvantages as linked list

slide-4
SLIDE 4

Binary searching & introduction to trees

4 CMPS 12B, UC Santa Cruz

What if we only have the value?

Rather than find the mth best grade, find the student

whose grade is 77

Can’t just count m items any more! Must scan the list / array until we find the correct student

A better way: binary search

slide-5
SLIDE 5

Binary searching & introduction to trees

5 CMPS 12B, UC Santa Cruz

Binary Search

Take a sorted array of

values

While (item not found)

“Guess” the item in the

middle of the array

See if the desired item is

above or below the guess

Narrow down the search area

by half

This works in log2(N) tries

  • n an array with N values

Much faster than simply

scanning

slide-6
SLIDE 6

Binary searching & introduction to trees

6 CMPS 12B, UC Santa Cruz

Binary search

Similar to recursion

Problem split in half at each

step

Main difference: ignore the

half where the value isn’t

Recursion doesn’t usually

save time

Easier to program, though

Binary search saves time!

Rule out half of the

remaining values at each step

Like recursion where we

ignore half of the problem each time we recurse

i n tbsea rch ( i n tva lues [ ] , i n tf i ndTh i s ) { i n tr ange = va lues . l eng th ; i n tbase = ; i n t m id ; wh i l e ( r ange > 1 ) { r ange = ( r ange+1 ) /2 ; m id = base+ range ; i f ( f i ndTh i s > va lues [m id ] ) { base = m id ; } e l se i f ( f i ndTh i s==va lues [m id ] ) { b reak ; } } i f ( va lues [m id ]== f i ndTh i s ) { r e tu rn (m id ) ; } e l se { r e tu rn (

  • 1

) ; } }

slide-7
SLIDE 7

Binary searching & introduction to trees

7 CMPS 12B, UC Santa Cruz

Binary search is great, but…

Binary search works well with arrays

Easy to find element n in constant time Difficult to insert things into the middle of the array

Binary search doesn’t work well with linked lists

Can’t find element n in constant time: long lists => long time to find

elements

Easy to insert and delete things in the middle

Modify linked lists to make searching easier?

Keep references into the middle of the list (1/4, 1/2, 3/4, or similar)?

Good idea, but doesn’t scale that well Must recreate shortcuts when things are inserted or deleted

Create a new structure that uses links but is still easy to do binary

search on?

slide-8
SLIDE 8

Binary searching & introduction to trees

8 CMPS 12B, UC Santa Cruz

Solution: trees

  • A tree is a linked data structure

where nodes may have more than

  • ne “next”
  • Terms

“next” of a node is its child “prev” of a node is its parent Base of the tree is the root Nodes along path to root are

ancestors

Nodes “below” this one are

descendants

Nodes with no children are leaf

nodes

  • Binary tree: tree in which each

node has at most two children

A B C D E

Class BTNode { Ob jec t i t em; BTNodel e f t ; BTNode r i gh t ; BTNode pa ren t ; }

root child parent leaf

slide-9
SLIDE 9

Binary searching & introduction to trees

9 CMPS 12B, UC Santa Cruz

Why use trees?

Advantages of linked lists

Insert or delete anywhere with ease Grow to any size

Advantages of arrays

Easy to do binary search Easy to keep sorted

And, lookup can be done quickly if the tree is sorted Disadvantages?

Overhead: three references per node in the tree It’s easy to have trees grow the wrong way…

slide-10
SLIDE 10

Binary searching & introduction to trees

10 CMPS 12B, UC Santa Cruz

More tree terms

  • Note: subtree can start at any

node

There’s a subtree rooted at C! Subtrees follow same rules as

trees

  • A tree’s height is the largest

number of nodes from root to leaf

Height of the tree on the right is

4 (A->C->D->F)

  • Balanced binary tree

For each node, the height of the

left and right subtree differ by at most 1

This tree is not balanced!

  • Full tree

No missing nodes For all nodes, height of left and

right subtree are equal

A B C D E F

slide-11
SLIDE 11

Binary searching & introduction to trees

11 CMPS 12B, UC Santa Cruz

Classes used in building binary trees

As with linked lists, two classes in binary trees

TreeNode: an individual node in the tree BinaryTree: a subtree rooted at a particular TreeNode

TreeNodes support the usual operations

TreeNode (Object newItem) TreeNode (Object newItem, TreeNode lt, TreeNode rt) Object getItem() void setItem (Object newItem) TreeNode getLeft/Right() TreeNode setLeft/Right (TreeNode left) Note: this implementation doesn’t have “up” pointers in each node

that point to the node’s parent

These operations are straightforward

Similar to operations in linked lists

slide-12
SLIDE 12

Binary searching & introduction to trees

12 CMPS 12B, UC Santa Cruz

Methods to build binary trees

Constructors

BinaryTree(): creates an empty tree BinaryTree(Object rootItem): creates a tree with a root BinaryTree(Object root, BinaryTree lt, BinaryTree rt): creates a tree

with a root and left & right subtrees

Attach things to the tree (root must already exist)

attachLeft/Right (Object newItem): attach an object to the left or right

  • f the root

attachLeft/RightSubtree (BinaryTree tree): attach an entire tree to the

root

attachLeft() could be done by creating a new subtree and attaching it

with attachLeftSubtree()…

Exceptions thrown for

Non-existent root Trying to attach something on top of an existing subtree

slide-13
SLIDE 13

Binary searching & introduction to trees

13 CMPS 12B, UC Santa Cruz

Methods to take trees apart

Often, necessary to take a tree apart

Make it better (more balanced) Delete an item

This can be done with

BinaryTree detachLeft/RightSubtree (): detaches a subtree

from the root, and returns it

Left or right node set to null

Informational methods

Object getRootItem () void setRootItem (Object newItem) boolean isEmpty()

How can we use these methods to build up a tree?

slide-14
SLIDE 14

Binary searching & introduction to trees

14 CMPS 12B, UC Santa Cruz

Create a simple tree

A B D C

bt = BinaryTree (“A”); bt.attachLeft (“B”); ct = BinaryTree (“C”); ct.attachLeft (“D”); bt.attachRightSubtree (ct);

slide-15
SLIDE 15

Binary searching & introduction to trees

15 CMPS 12B, UC Santa Cruz

Example: Words Stored Lexicographically

Foo Bar Map Gas Net Fry

slide-16
SLIDE 16

Binary searching & introduction to trees

16 CMPS 12B, UC Santa Cruz

Example: Equation a + (b*c)

+ a * b c

slide-17
SLIDE 17

Binary searching & introduction to trees

17 CMPS 12B, UC Santa Cruz

Example: Equation (a+b) * c

* c + a b

slide-18
SLIDE 18

Binary searching & introduction to trees

18 CMPS 12B, UC Santa Cruz

Example: Organization Chart

Chancellor Other Vice Chancellor Vice Chancellor Dean 1 Dean 2 Department Chair

slide-19
SLIDE 19

Binary searching & introduction to trees

19 CMPS 12B, UC Santa Cruz

A Recursive Definition of Binary Trees

A tree T is a binary tree if either

T has no nodes, or T is of the form Where n is a node, and TL and TR are both binary trees.

n TL TR

slide-20
SLIDE 20

Binary searching & introduction to trees

20 CMPS 12B, UC Santa Cruz

Height of a Tree

Level of a node

If n is the root of T, it is at level 1 If n is not the root, its level is 1 higher than that of its

parent

Height of a tree

If T is empty, its height is 0 If T is not empty, its height is equal to the maximum level

  • f its nodes

Recursive definition of height

If T is empty, its height is 0 If T is nonempty, its height is equal to 1 + the height of its

tallest subtree: height(T) = 1+max{height(TL),height(TR)}

slide-21
SLIDE 21

Binary searching & introduction to trees

21 CMPS 12B, UC Santa Cruz

Types of Binary Trees

Full binary tree

All nodes at level k<h have two children each All leaves are at the same level

Complete binary tree

All nodes at level k<h-1 have two children each, and When a node has children, all nodes to its left have

children, and

When a node has one child, it is a left child

Balanced binary tree

The height of each node’s subtrees differ by at most 1.

slide-22
SLIDE 22

Binary searching & introduction to trees

22 CMPS 12B, UC Santa Cruz

Binary Tree ADT

Basic Operations

Create an empty binary tree Create a one-node binary tree, given an item Remove all nodes from a binary tree Determine whether a binary tree is empty Determine what data is at a binary tree’s root

General Operations

Create a binary tree given an item, and left/right subtrees Set root item Attach left or right item Attach left/right subtree Detach left/right subtree

slide-23
SLIDE 23

Binary searching & introduction to trees

23 CMPS 12B, UC Santa Cruz

Binary Tree Code

See examples

slide-24
SLIDE 24

Binary searching & introduction to trees

24 CMPS 12B, UC Santa Cruz

Iterators

Provide a general way of traversing a tree

Can’t use internal types like TreeNode! Instead, use an iterator

An iterator is a class whose purpose is to allow other

structures to be “read” in order

Example (sort of): Tokenizer

An iterator supports a set of methods

Constructor: specifies the data structure to iterate over hasNext(): true if there is another object to iterate to next(): returns the next object in the traversal

slide-25
SLIDE 25

Binary searching & introduction to trees

25 CMPS 12B, UC Santa Cruz

Traversing a binary tree

  • Trees can be traversed in three orders
  • Pre-order: root, L, R
  • A, B, C, D, F, E
  • In-order: L, root, R
  • B, A, F, D, C, E
  • Post-order: L, R, root
  • B, F, D, E, C, A
  • Order chosen depends on
  • What the tree is being used for
  • What the traversal is supposed to

accomplish

  • Traversal is done recursively!
  • Treat L, R as trees in their own right
  • Recursively visit them

A B C D E F

slide-26
SLIDE 26

Binary searching & introduction to trees

26 CMPS 12B, UC Santa Cruz

Pre-order traversal

  • Visit nodes in this order
  • Root node
  • Left subtree
  • Right subtree
  • Recursive visit
  • Perform operation at the leaf
  • Printing in this example

A B C D E F

vo id p reo rde r ( ) { Sys tem.ou t . p r i n t l n( r

  • t

. va l ue ) ; i f ( l e f t ! = nu l l ) { l e f t . p reo rde r ( ) ; } i f ( r i gh t ! = nu l l ) { r i gh t . p reo rde r ( ) ; } }

1 2 3 4 5 6

slide-27
SLIDE 27

Binary searching & introduction to trees

27 CMPS 12B, UC Santa Cruz

In-order traversal

  • Visit nodes in this order
  • Left subtree
  • Root node
  • Right subtree
  • Recursive visit
  • Perform operation at the leaf
  • Printing in this example

A B C D E F

vo id i no rde r( ) { i f ( l e f t ! = nu l l ) { l e f t . i no rde r( ) ; } Sys tem.ou t . p r i n t l n( r

  • t

. va l ue ) ; i f ( r i gh t ! = nu l l ) { r i gh t . i no rde r( ) ; } }

2 1 5 4 3 6

slide-28
SLIDE 28

Binary searching & introduction to trees

28 CMPS 12B, UC Santa Cruz

Post-order traversal

  • Visit nodes in this order
  • Left subtree
  • Right subtree
  • Root node
  • Recursive visit
  • Perform operation at the leaf
  • Printing in this example

A B C D E F

vo id pos to rde r( ) { i f ( l e f t ! = nu l l ) { l e f t . pos to rde r( ) ; } i f ( r i gh t ! = nu l l ) { r i gh t . pos to rde r( ) ; } Sys tem.ou t . p r i n t l n( r

  • t

. va l ue ) ; }

6 1 5 3 2 4

slide-29
SLIDE 29

Binary searching & introduction to trees

29 CMPS 12B, UC Santa Cruz

Binary search tree

  • Rule 1: left child is less than

parent

  • Rule 2: right child is greater than

parent

  • Insert a new node by

Following the links down Attaching the new node where it

“should” go

  • Result:

All nodes in the left subtree are

less than the root!

All nodes in the right subtree are

greater than the root

lion dog frog mouse panda cat marmot egret

slide-30
SLIDE 30

Binary searching & introduction to trees

30 CMPS 12B, UC Santa Cruz

Binary search tree

  • The value stored in each node for

comparison is a “key”

  • Examples:

Directories List of students …

  • Recursive definition of binary

search tree

For each node n, n’s key is greater than every key

in TL

n’s key is less than every key in

TR

TL and TR are binary search

trees.

lion dog frog mouse panda cat marmot egret

slide-31
SLIDE 31

Binary searching & introduction to trees

31 CMPS 12B, UC Santa Cruz

Searching in a binary search tree

  • Searching is similar to insertion

Go left if less Go right if more

  • Can be done recursively
  • Can be done non-recursively

Loop, setting appropriate subtree

to root each time

lion dog frog mouse panda cat marmot egret

slide-32
SLIDE 32

Binary searching & introduction to trees

32 CMPS 12B, UC Santa Cruz

Recursively Searching a Binary Search Tree

search(binary search tree, searchKey) { if(empty) // not found else if(key = = searchKey) // found else if(key > searchKey) search(left subtree, searchKey); else search(right subtree, searchKey); }

slide-33
SLIDE 33

Binary searching & introduction to trees

33 CMPS 12B, UC Santa Cruz

lion dog frog mouse panda cat marmot egret

Accessing a BST in sorted order

How can we print this tree

in sorted order?

Starting at root, we know

All nodes in left subtree are

“less” than root

All nodes in right subtree are

“greater” than root

Left & right subtrees can both

be printed in sorted order

Solution: in-order traversal!

Print all nodes less than root

in sorted order

Print root Print all nodes greater than

root in sorted order

slide-34
SLIDE 34

Binary searching & introduction to trees

34 CMPS 12B, UC Santa Cruz

Binary Search Tree Operations

Insert an item (O(logn))

Search until null is reached – place the item there

Delete an item with a given search key (O(logn))

Three cases N has no children – easy, set parent’s reference to null N has one child – like a linked list N has two children – replace with inorder successor

Retrieve an item with a given search key (O(logn))

Binary search (yay)

Traverse the items in some order (O(n))

slide-35
SLIDE 35

Binary searching & introduction to trees

35 CMPS 12B, UC Santa Cruz

Balancing trees (overview)

  • Binary trees are most effective

when they are balanced

Maximum depth of any node is

no more than 1 greater than minimum depth of any node

  • How can we balance a tree?

Preserve ordering rules! Rearrange tree to fix heights

  • Ensure tree is balanced after each

insertion

Prevent it from getting too far

  • ut of balance
  • Use a special kind of binary tree:

2-3 tree

General idea: keep the tree

balanced at every step

Details beyond the scope of this

class lion dog frog mouse panda cat eagle egret lion dog frog mouse panda cat eagle egret

slide-36
SLIDE 36

Binary searching & introduction to trees

36 CMPS 12B, UC Santa Cruz

Saving and restoring binary trees

  • What if we want to save a tree?

Print it out in such a way that we can restore it later Provide sufficient information to reconstruct an exact copy

  • Use pre-order traversal

Print information about each node saying whether it has left, right, neither or

both subtrees

Print the contents of the node

  • Reconstruct recursively

A-2 B-0 C-2 D-L F-0 E-0 A B C D E F A B C D E F

Tree bu i l dT ree ( ) { r ead va l ue , s t a tus t = new T ree ( va l ue ) ; i f ( s t a tus == 2 | | s t a t us == L ) { t . a t t achL (bu i l dT ree ( ) ) ; } i f ( s t a tus == 2 | | s t a t us == R ) { t . a t t achR (bu i l dT ree ( ) ) ; } r e t u rn ( t ) ; }

slide-37
SLIDE 37

Binary searching & introduction to trees

37 CMPS 12B, UC Santa Cruz

Why save and restore trees?

Helpful when one program creates a tree that another

wants to use

Placement within the tree could be important! Might not simply be sorted…

slide-38
SLIDE 38

Binary searching & introduction to trees

38 CMPS 12B, UC Santa Cruz

General Trees

What if you want to have any number of children in

a tree?

What data structure would you use to store the children?