cse 3358 note set 5

17
CSE 3358 NOTE SET 5 Data Structures and Algorithms

Upload: debra-joseph

Post on 30-Dec-2015

31 views

Category:

Documents


0 download

DESCRIPTION

CSE 3358 Note Set 5. Data Structures and Algorithms. Trees. Overcome the linear nature of a list. Allows for storage of hierarchical data. Recursive Definition of Tree: An empty structure is empty tree - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 3358  Note Set  5

CSE 3358 NOTE SET 5

Data Structures and Algorithms

Page 2: CSE 3358  Note Set  5

Trees

Overcome the linear nature of a list. Allows for storage of hierarchical data

Recursive Definition of Tree:

• An empty structure is empty tree• If t1, …, tk are disjointed trees, then the structures

whose root has as its children the roots of t1, …, tk is also a tree

• Only structures generated by these two rules are trees

Page 3: CSE 3358  Note Set  5

Tree Terminology

• Root

• Children

• Parent

• Ancestors

• Descendants

Page 4: CSE 3358  Note Set  5

Tree Terminology

A

B C

K L

G H

JI

FED

• Height of C

• Path from A to J

• Length of Path from A to J

• Depth of F

Page 5: CSE 3358  Note Set  5

Tree Terminology

20

10 15

18 19

16 17

98

765

• Children of 10

• Ancestors of 18

• Descendant 15

• Leaves of the tree

Page 6: CSE 3358  Note Set  5

Generic Tree Traversals

Pre-order Work is done on children before parent is processed

Post-order Work is done on parent after children are processed

Page 7: CSE 3358  Note Set  5

Binary Tree

Each node has 2 children at max. Left and Right Benefit w/ respect to efficiency of

algorithms Complete Binary Tree

All non-leaf nodes have both children

All leaves are at same level

Page 8: CSE 3358  Note Set  5

Worst Case Binary Tree

Page 9: CSE 3358  Note Set  5

Binary Search Tree

Binary Search Property:

For each node n of the tree, all values stores in its left subtree are less than the value v stored in n and all values stored in the right subtree are greater than v.

v

• No =. Only < and >. • Storing multiple copies of the same value is avoided.• Meaning of < and > depend on what the data payload of the

node is• Average Depth is O(lg N)

Page 10: CSE 3358  Note Set  5

Implementation

Can be implemented as: Array

struct node { T* payload; int leftIdx; int rightIdx;};

If element deleted, update of entire tree may be necessary. Linked Dynamic structure

struct node { T* payload; node *left, *right};

Page 11: CSE 3358  Note Set  5

Search Intuition

Page 12: CSE 3358  Note Set  5

Complexity of Search

Based on the number of comparisons performed during the searching process depends on the number of nodes on the unique path

from root to the node being searched for. Complexity: The length of the path leading to this

node plus 1. depends on the overall shape of the tree

Think about a tree that is essentially a List vs a balanced tree

Internal Path Length:

The sum of all path lengths of all nodes.

Page 13: CSE 3358  Note Set  5

IPL

14

12 16

Page 14: CSE 3358  Note Set  5

IPL

14

7 27

3 9 32

Page 15: CSE 3358  Note Set  5

IPL

14

7 27

3 9 32

Formula for IPL:

Formula for Average IPL:

Page 16: CSE 3358  Note Set  5

IPL for Binary Tree

Worst Case: tree that looks like a Linked List

Best Case: all leaves in tree of height h are in at most two levels only nodes in the next to last level can have children

2

3

4

5

Page 17: CSE 3358  Note Set  5

?