welcome to cis 068 !

26
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees

Upload: brennan-saunders

Post on 03-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Welcome to CIS 068 !. Lesson 12: Data Structures 3 Trees. Overview. Binary Trees Complete Trees Heaps Binary Search Trees Balanced Trees. Definitions. root. Node 0 is ancestor of all other nodes Nodes 1-6 are descendants of node 0. Node 0. Node 1,2,3 are children of root. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Welcome to CIS 068 !

CIS 068

Welcome to CIS 068 !Lesson 12:

Data Structures 3

Trees

Page 2: Welcome to CIS 068 !

CIS 068

Overview

• Binary Trees

• Complete Trees

• Heaps

• Binary Search Trees

• Balanced Trees

Page 3: Welcome to CIS 068 !

CIS 068

Definitions

Node 0

Node 1 Node 2 Node 3

Node 4 Node 5 Node 6

leaves

root

Node 1,2,3 are children of root

Node 4 and 5 are siblings

Node 1 is parent of

Nodes 4,5

Node 0 is ancestor

of all other nodes

Nodes 1-6 are

descendants

of node 0

Page 4: Welcome to CIS 068 !

CIS 068

Binary TreesDef. (recursively defined data structure) :

A binary tree is either

• empty (no nodes), or

• has a root node, a left binary tree, and a right binary tree as children

… hence it is a tree with at most two children for each node

Page 5: Welcome to CIS 068 !

CIS 068

Complete TreesDef.:• A tree in which all leaf nodes are at some depth

n or n-1, and all leaves at depth n are toward the left

complete incomplete incomplete

depth 1

depth 2

depth 3

Page 6: Welcome to CIS 068 !

CIS 068

Complete TreesProperties:• A complete tree with depth n has at most 2n+1 – 1 elements

• A complete tree with depth n has at least 2n elements

• The index of the left child of node k is 2k+1, the index of the right child of node is 2k+2

0

1 2

3

7

4 5 6

8

depth 1

depth 2

depth 3

Page 7: Welcome to CIS 068 !

CIS 068

Complete TreesStorage of complete trees in arrays:

0

1 2

3

7

4 5 6

8

0 1 2 3 4 5 6 7 8

2k+1, 2k+2k=3

Page 8: Welcome to CIS 068 !

CIS 068

HeapDef.:• A complete binary tree where every node has a

value greater than or equal to the key of its parent

89

76 80

37 32 39

Page 9: Welcome to CIS 068 !

CIS 068

HeapWhat for ?• Sorting (HEAPSORT):

– Sort elements into heap structure

• How to – Insert ?

– Delete ?

• Order of magnitude ?

Page 10: Welcome to CIS 068 !

CIS 068

• Example of Heapsort: HEAPSORT-APPLET

• Insert / Delete: (board)

Heaps provide a structure for efficient retrieval of maximum values !

How to look for arbitrary values ? Binary Search Trees !

Heap

Page 11: Welcome to CIS 068 !

CIS 068

Def.:

• A binary tree where every node's left subtree has values less than the node's value, and every right subtree has values greater.

Binary Search Trees

76

39 80

32 47 79

Page 12: Welcome to CIS 068 !

CIS 068

Remarks:

• A heap is NOT a binary search tree !

• A binary search tree is not necessarily complete (see example)!

• (Worst case: create BST of sorted list)

Binary Search Trees

76

39

80

32 37

89

Page 13: Welcome to CIS 068 !

CIS 068

• How to search in binary search tree ?

– (Answer is straightforward)– Applet: animated BST

• Order of magnitude ?

• How to achieve O(log n) ?– balanced binary search trees !

Binary Search Trees

Page 14: Welcome to CIS 068 !

CIS 068

Def.:

• A tree whose subtrees differ in height by no more than one and the subtrees are height-balanced, too. An empty tree is height-balanced.

Balanced Trees

Page 15: Welcome to CIS 068 !

CIS 068

Remark:

• Every complete tree is balanced, but not vice versa !

Balanced Trees

12

8 18

5 11 17

4

Page 16: Welcome to CIS 068 !

CIS 068

How to create balanced trees ?

Rotation !

Binary Search Trees

Page 17: Welcome to CIS 068 !

CIS 068

Rotation

Page 18: Welcome to CIS 068 !

CIS 068

Rotation

Page 19: Welcome to CIS 068 !

CIS 068

Rotation

Page 20: Welcome to CIS 068 !

CIS 068

AVL Trees

How to use Rotation to create balanced trees:

AVL Trees

(Adelson-Velskii + Landis, 1962)

Page 21: Welcome to CIS 068 !

CIS 068

Idea:

• Keep track of the difference in the depth of each subtree as items are inserted or removed

• Use rotation to bring tree into balance if difference is not in range of +-1

AVL Trees

Page 22: Welcome to CIS 068 !

CIS 068

Example 1: single rotation

Is a single rotation always the solution ?

AVL Trees

Page 23: Welcome to CIS 068 !

CIS 068

Example 2: single rotation

Example 2: the left-heavy tree got a right-heavy tree !

AVL Trees

Page 24: Welcome to CIS 068 !

CIS 068

Example 3: rotation of subtrees

• Balance is achieved by rotating the subtrees in ‘a certain way’

AVL Trees

Page 25: Welcome to CIS 068 !

CIS 068

Resume:• Special binary trees provide an efficient structure for

sorting and searching

• Complete binary trees can be stored in an array without link-structure overhead

• Heaps are used to sort arrays for retrieval of maximum values (typical application: shape-abstraction- assignment !)

• Binary search trees are used for access to arbitrary objects in O(log n), achieved by balancing trees

• AVL trees are one example for balanced trees, using rotation to keep the balance

AVL Trees

Page 26: Welcome to CIS 068 !

CIS 068

…to be continued

Trees