data structures - 13. introduction to trees

33
Universidade Federal da Paraíba Centro de Informática Introduction to Trees Lecture 16 1107186 – Estrutura de Dados – Turma 02 Prof. Christian Azambuja Pagot CI / UFPB

Upload: emerson-ferreira

Post on 24-Jun-2015

151 views

Category:

Education


1 download

DESCRIPTION

Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.

TRANSCRIPT

Page 1: Data Structures - 13. Introduction to Trees

Universidade Federal da ParaíbaCentro de Informática

Introduction to TreesLecture 16

1107186 – Estrutura de Dados – Turma 02

Prof. Christian Azambuja PagotCI / UFPB

Page 2: Data Structures - 13. Introduction to Trees

2Universidade Federal da ParaíbaCentro de Informática

What is a Tree?

● In graph theory, a tree is defined as a connected acyclic graph.

● Example:

3

5

6

4

1

2 However, one such graph may generate

different tree data structures!

Node

Edge

Page 3: Data Structures - 13. Introduction to Trees

3Universidade Federal da ParaíbaCentro de Informática

What is a Tree?

● In graph theory, a tree is defined as a connected acyclic graph.

● Example:

3

5

6

4

1

2

4

1

2

3

4

5

Root node

Page 4: Data Structures - 13. Introduction to Trees

4Universidade Federal da ParaíbaCentro de Informática

What is a Tree?

● In graph theory, a tree is defined as a connected acyclic graph.

● Example:

3

5

6

4

2

14

3

5 26

1

Root node

Page 5: Data Structures - 13. Introduction to Trees

5Universidade Federal da ParaíbaCentro de Informática

Definitions

● Path Length– Number of edges on a path.

● Depth of a Node– Equal to the path length to the root.

● Height of a Node– Equal to the path length from the

node to its deepest descendant.

● Height of a Tree– Equal to the height of the root.

2

9

87

1

4

3

65

Page 6: Data Structures - 13. Introduction to Trees

6Universidade Federal da ParaíbaCentro de Informática

Definitions

● Child Node– Each node can have n

child nodes.

● Parent Node– It is unique for each

node.

– The root does not have parent node.

2

9

87

1

4

3

65

Page 7: Data Structures - 13. Introduction to Trees

7Universidade Federal da ParaíbaCentro de Informática

Definitions

● Leaf Node– Has no child node.

● Siblings– Nodes with the same parent.

● Ancestors– All nodes on the path to the root.

● Descendants of a node n– All nodes that share n as an

ancestor.

2

9

87

1

4

3

65

Page 8: Data Structures - 13. Introduction to Trees

8Universidade Federal da ParaíbaCentro de Informática

Definitions

● Subtree– Tree formed by a node

and all its descendants.

2

9

87

1

4

3

65

Page 9: Data Structures - 13. Introduction to Trees

9Universidade Federal da ParaíbaCentro de Informática

Definitions

● Tree Arity– n-ary.

– Binary. 2

9

87

4

3

65

1

n-ary tree

3

4

2

65

1

binary tree

Page 10: Data Structures - 13. Introduction to Trees

10Universidade Federal da ParaíbaCentro de Informática

Binary Tree Implementation

● Node– Pointers to parent, left and right child.

parent

value

left right

Node

Page 11: Data Structures - 13. Introduction to Trees

11Universidade Federal da ParaíbaCentro de Informática

Binary Tree Implementation

parent

value

null

left null right null

Node

root

parent

value

null

left null right null

Node

left child

Page 12: Data Structures - 13. Introduction to Trees

12Universidade Federal da ParaíbaCentro de Informática

Binary Tree Implementation

parent

value

null

left right null

Node

root

parent

value

left null right null

Node

How about a n-ary tree?

Page 13: Data Structures - 13. Introduction to Trees

13Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Node– Pointers to parent, left child and right sibling.

parent

value

left rsibling

Node

According to “Introduction to Algorithms”, Cormen, Leiserson, Rivest, Stein.

Page 14: Data Structures - 13. Introduction to Trees

14Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n n

Page 15: Data Structures - 13. Introduction to Trees

15Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn n

Page 16: Data Structures - 13. Introduction to Trees

16Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn

p

left rsibn n

Page 17: Data Structures - 13. Introduction to Trees

17Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn

p

left rsibn

p

left rsibn n

Page 18: Data Structures - 13. Introduction to Trees

18Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn

p

left rsibn

p

left rsibn

p

left rsibn n

Page 19: Data Structures - 13. Introduction to Trees

19Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn

p

left rsibn

p

left rsib

p

left rsibn n

p

left rsibn n

Page 20: Data Structures - 13. Introduction to Trees

20Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn

p

left rsibn

p

left rsib

p

left rsibn n

p

left rsibn

p

left rsibn n

Page 21: Data Structures - 13. Introduction to Trees

21Universidade Federal da ParaíbaCentro de Informática

Tree Traversal

● There are two types of tree traversal algorithms:– Breadth-first.

– Depth-first.

Page 22: Data Structures - 13. Introduction to Trees

22Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Visits only once each node of a graph on a level-by-level basis.

3

4

2

65

1

Page 23: Data Structures - 13. Introduction to Trees

23Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Visits only once each node of a graph on a level-by-level basis.

3

4

2

65

1

Page 24: Data Structures - 13. Introduction to Trees

24Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Visits only once each node of a graph on a level-by-level basis.

3

4

2

65

1

Page 25: Data Structures - 13. Introduction to Trees

25Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Visits only once each node of a graph on a level-by-level basis.

3

4

2

65

1

Page 26: Data Structures - 13. Introduction to Trees

26Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Visits only once each node of a graph on a level-by-level basis.

3

4

2

65

1

Page 27: Data Structures - 13. Introduction to Trees

27Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Implementationstruct Node{

struct Node* parent;int value;struct Node* left;struct Node* right;

};

struct Queue{

int first;int last;struct Node* elements[10];

};

struct Queue q;... int main(...) ...

C code excerpt:

Page 28: Data Structures - 13. Introduction to Trees

28Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Implementationvoid Enqueue(struct Queue* q, struct Node* n){

q­>elements[++q­>last] = n;}

struct Node* Dequeue(struct Queue* q){

return q­>elements[q­>first++];}

int QueueNotEmpty(struct Queue* q){

return q­>first <= q­>last;}

... int main(...) ...

C code excerpt:

Page 29: Data Structures - 13. Introduction to Trees

29Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Breadth-first-search (BFS)

● Implementationvoid BFS(struct Node* n){

if (n != NULL){

Enqueue(&q,n);

while(QueueNotEmpty(&q)){

struct Node* v = Dequeue(&q);

printf("node value: %i\n",v­>value);

if (v­>left != NULL)Enqueue(&q,v­>left);

if (v­>right != NULL)Enqueue(&q,v­>right);

}}

}... int main(...) ...

C code excerpt:

Page 30: Data Structures - 13. Introduction to Trees

30Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Depth-First-Search (DFS)

● Explores as far as possible each branch of the tree before backtracking.– Pre-order:

– In-order:

– Post-order:3

4

2

65

11, 2, 4, 5, 6, 3

5, 4, 6, 2, 1, 3

5, 6, 4, 2, 3, 1

Page 31: Data Structures - 13. Introduction to Trees

31Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Pre-Order

● Implementation

void PreOrder(struct Node* n){

if (n != NULL){

printf("node value: %i\n", n­>value);PreOrder(n­>left);PreOrder(n­>right);

}}

C code excerpt:

Page 32: Data Structures - 13. Introduction to Trees

32Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: In-Order

● Implementation

void InOrder(struct Node* n){

if (n != NULL){

InOrder(n­>left);printf("node value: %i\n", n­>value);InOrder(n­>right);

}}

C code excerpt:

Page 33: Data Structures - 13. Introduction to Trees

33Universidade Federal da ParaíbaCentro de Informática

Tree Traversal: Post-Order

● Implementation

void PostOrder(struct Node* n){

if (n != NULL){

PostOrder(n­>left);PostOrder(n­>right);printf("node value: %i\n", n­>value);

}}

C code excerpt: