data structures - 13. introduction to trees

Post on 24-Jun-2015

152 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Universidade Federal da ParaíbaCentro de Informática

Introduction to TreesLecture 16

1107186 – Estrutura de Dados – Turma 02

Prof. Christian Azambuja PagotCI / UFPB

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

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

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

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

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

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

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

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

10Universidade Federal da ParaíbaCentro de Informática

Binary Tree Implementation

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

parent

value

left right

Node

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

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?

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.

14Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n n

15Universidade Federal da ParaíbaCentro de Informática

n-ary Tree Implementation

● Example

Node

root

p

left rsib

n

n

p

left rsibn n

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

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

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

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

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

21Universidade Federal da ParaíbaCentro de Informática

Tree Traversal

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

– Depth-first.

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

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

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

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

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

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:

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:

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:

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

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:

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:

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:

top related