cosc2007 data structures ii chapter 11 trees i. 2 topics terminology

38
COSC2007 Data Structures II Chapter 11 Trees I

Upload: gervase-malone

Post on 27-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

COSC2007 Data Structures II

Chapter 11

Trees I

Page 2: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

2

Topics

Terminology

Page 3: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

3

Introduction & Terminology

Review Position-oriented ADT:

Insert, delete, or ask questions about data items at specific position

Examples: Stacks, Queues Value-oriented ADT:

Insert, delete, or ask questions about data items containing a specific value

Example?

Page 4: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

4

Introduction & Terminology

Hierarchical (Tree) structure: Parent-child relationship between elements of the

structure More?

Examples: Organization chart Library card-catalog More???

Page 5: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

5

Introduction & Terminology

Example: Library card catalogCard

Catalog

Subject Catalog

Author Catalog

Title Catalog

A - Abbex Stafford , R - Stanford, R

Zon - Zz

Stafford, R. H.

Standish, T. A.

Stanford Research Institute

Page 6: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

6

Introduction & Terminology Table of contents of a book:

Page 7: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

7

Trees and Tree Terminology

A tree is a data structure that consists of a set of nodes (vertex) and a set of edges (or branches) that connect nodes.

Page 8: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

8

Introduction & Terminology A is the root node. B is the parent of D and E. C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or

leaves A, B, C, H are internal nodes The depth (level) of E is 2 The height of the tree is (3)/4 The degree of node B is 2 Property?

Page 9: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

9

Introduction & Terminology Path from node n1 to nk:

A sequence of nodes n1, n2, … . nk such that there is an edge between each pair of nodes (n1, n2) (n2, n3), . . . (nk-1, nk)

Height h: The number of nodes on the longest path from the root to a

leaf

Ancestor-descendent relationship Ancestor of node n:

A node on the path from the root to n Descendent of node n:

A node on the path from n to a leaf

Page 10: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

10

Introduction & Terminology

Nodes: Contains information of an element Each node may contain one or more pointers

to other nodes A node can have more than one immediate

successor (child) that lies directly below it Each node has at most one predecessor

(parent) that lies directly above it

Page 11: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

11

Binary Trees A binary tree is an ordered tree in which every node has at most two children. A binary tree is:

either empty or consists of a

root node (internal node) a left binary tree and a right binary tree

Each node has at most two children Left child Right child

Page 12: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

12

Binary Trees

Special trees Left (right) subtree of node n:

In a binary tree, the left (right) child (if any) of node n plus its descendants

Empty BT: A binary tree with no nodes

Binary tree is position-oriented or value-oriented? But?

Page 13: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

13

Number Of Nodes-height is h Suppose the height is h

Minimum number of nodes in a binary tree: At least one node at each of first h levels.

Maximum number of nodes in a binary tree: All possible nodes at first h levels are present

Page 14: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

14

Number Of Nodes & Height

Let n be the number of nodes in a binary tree whose height is h.h <= n <= 2h – 1

If we know n, what will be the value for h??

Page 15: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

15

A

B

D E

H I J K

C

F G

LM N O

Full Binary Trees In a full binary tree,

every leaf node has the same depth every non-leaf node (internal node) has two children.

A

B

D E

H I J K

C

F G

L

Not a full binary tree. A full binary tree.

Page 16: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

16

Complete Binary Tree In a complete binary tree

every level except the deepest must contain as many nodes as possible ( that is, the tree that remains after the deepest level is removed must be full).

at the deepest level, all nodes are as far to the left as possible.

A

B

D E

H I J K

C

F G

L

Not a Complete Binary Tree

A

B

D E

H I J K

C

F G

L

A Complete Binary Tree

Page 17: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

17

Proper Binary Tree In a proper binary tree,

each node has exactly zero or two children each internal node has exactly two children.

A

B

D E

H I J K

C

F G

L

A

B

D E

H I J K

C

F G

Not a proper binary tree A proper binary tree

Page 18: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

18

An Aside: A Representation for Complete Binary Trees

Since tree is complete, it maps nicely onto an array representation.

A

B

D E

H I J K

C

F G

L

0 1 2 3 4 5 6 7 8 9 10 11 12

A B C D E F G H I J K LT:

last

Page 19: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

19

Properties of the Array Representation

Data from the root node is always in T[0]. Suppose some node appears in T[i]

data for its parent is always at location T[(i-1)/2]

(using integer division) data for it’s children nodes appears in locations

T[2*i+1] for the left child

T[2*i+2] for the right child formulas provide an implicit representation of the edges can use these formulas to implement efficient algorithms for

traversing the tree and moving around in various ways.

Page 20: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

20

What Tree?

A

B

D E

H I J K

C

F

L

Page 21: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

21

Balanced vs. Unbalanced Binary Trees

A

B

D E

H I J K

C

F G

L

root

A

B

D

E

H I

J K

C

F G

L

start

Sort of BalancedMostly Unbalanced

A binary tree in which the left and right subtrees of any node have heights that differ by at most 1

Page 22: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

22

Binary search tree (BST)

A binary tree where The value in any node n is greater than the

value in every node in n’s left subtree The value in any node n is less than the

value of every node in n's right subtree The subtrees are binary search trees too

60

7020

10 40

5030

Page 23: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

23

Comparing Trees

These two trees are not the same tree!

A

B

A

B

Why?

Page 24: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

24

Review Which of the following ADT is value-

oriented? list sorted list stack queue binary tree

Page 25: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

25

Review The ______ is a position-oriented ADT

that is not linear. sorted list queue binary tree list

Page 26: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

26

Review A node of a tree is called a(n) ______.

edge root branch vertex

Page 27: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

27

Review The lines between the nodes of a tree are

called ______. branches edges Arches subtrees

Page 28: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

28

Review The node that is directly above node n in a

tree is called the ______ of node n. root leaf parent child

Page 29: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

29

Review In a tree, the children of the same parent

are called ______. leafs siblings roots contemporaries

Page 30: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

30

Review Each node in a tree has ______.

exactly one parent at most one parent exactly two parents at most two parents

Page 31: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

31

Review A descendant of node n is a node on a

path from node n to ______. the root a leaf a sibling of node n a child of node n

Page 32: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

32

Review A subtree of node n is a subtree rooted at

______. node n the parent of node n a child of node n a sibling of node n

Page 33: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

33

Review The ______ of a tree is the number of

nodes on the longest path from the root to a leaf. height length width age

Page 34: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

34

Review In a ______ of height h, all nodes that are

at a level less than h have two children each. general tree binary tree full binary tree complete binary tree

Page 35: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

35

Review A ______ of height h is full down to level h

– 1, with level h filled in from left to right. full binary tree complete binary tree balanced binary tree general tree

Page 36: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

36

Review In ______, the left and right subtrees of

any node have heights that differ by at most 1. all trees all binary tress n-ary trees balanced binary trees

Page 37: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

37

Review Which of the following is NOT a property of a

complete binary tree of height h? all nodes at level h – 2 and above have two children

each when a node at level h – 1 has children, all nodes to

its left at the same level have two children each when a node at level h – 1 has one child, it is a left

child all leaves are at level h

Page 38: COSC2007 Data Structures II Chapter 11 Trees I. 2 Topics Terminology

38

Review In an array based representation of a

complete binary tree, which of the following represents the left child of node tree[i]? tree[i+2] tree[i–2] tree[2*i+1] tree[2*i+2]