/
Concept of Trees Concept of Trees

Concept of Trees - PowerPoint Presentation

celsa-spraggs
celsa-spraggs . @celsa-spraggs
Follow
391 views
Uploaded On 2016-07-30

Concept of Trees - PPT Presentation

Min Chen School of Computer Science and Engineering Seoul National University Data Structure Chapter 6 Content Definition of Trees Representing Rooted Tree Tree Traversal Preorder Traversal ID: 425795

parent child item traversal child parent traversal item sibling tree node left preorder root postorder binary visit path null nodes order sibtreenode

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Concept of Trees" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Concept of Trees

Min ChenSchool of Computer Science and Engineering Seoul National University

Data Structure: Chapter 6Slide2

Content

Definition of TreesRepresenting Rooted TreeTree TraversalPreorder TraversalPostorder Traversal

Level Order TraversalSlide3

Definition of Trees

Tree: Set of nodes and edges that connect themExactly one path between any 2 nodesRooted Tree:One distinguished node is called the root

Every node C, except root, has one parent P, the first node on path from c to the root. C is P’s child

Root has no parent

A node can have any number of childrenSlide4

Some Definitions

LeafNode with no childrenSiblingsNodes with same parentAncestorsnodes on path from d to

rott

, including d,

d’s

parent,

d’s

grand parent, … root

Descendant

If A is B’s ancestor, then B is A’s DescendantSlide5

Some Definitions (2)

Length of pathNumber of edges in pathDepth of node nLength of path from n to root

Depth of root is zero

Height

of node n

Length of path from n to its deepest descendant

Height of any leaf is zero

Height

of a tree

= Height of the

root

Subtree

rooted at n

The tree formed by n and its

descendantsSlide6

Representing Rooted Trees

G & TEach node has 3 references stored in a listItemParentChildren

Another Option: Sibling Tree

Siblings are directly linkedSlide7

Sibling Tree

Next Sibling

Parent

Item

First Child

Class

SibTreeNode

{

Object item ;

SibTreeNode

parent ;

SibTreeNode

firstChild

;

SibTreeNode

nextSibling

;

}Slide8

Sibling Tree

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First Child

Next Sibling

Parent

Item

First ChildSlide9

Tree Traversal

Rooted TreePreorder TraversalPostorder Traversal

Level Order

Traversal

Binary Tree

Inorder

TraveralSlide10

Preorder Traversal

Visit each node before recursively visiting its children, left to right

A

B

C

D

E

F

G

H

A

A’s First Child

B

B’s First Child

D

D has no child, then sibling

E

E has no child, then sibling

F

F has no child, no sibling

B has no child, no sibling

A has no child, then sibling

C

C’s First Child

G

G has no child, then sibling

H

H has no child, no siblingSlide11

Preorder Traversal

Class SibTreeNode

{

public void preorder() {

this.visit

();

if(

firstChild

!=null){

firstChild.preorder

();

}

if(

nextSibling

!=null){

nextSibling.preorder();

} }}Preorder Traversal Realization by RecursionSlide12

Preorder Traversal

Preorder Traversal Realization by Stacks

A

B

C

D

E

F

G

H

A

B

C

A

Stack:

Visiting Sequence:

B

D

E

F

D

E

F

C

G

H

G

HSlide13

Postorder Traversal

Visit each node’s children (left-to-right) before the node itself

A

B

C

D

E

F

G

HSlide14

Postorder Traversal

Postorder Traversal Realization by Recursion

Class

SibTreeNode

{

public void

postorder

() {

if(

firstChild

!=null) {

firstChild.postorder

();

}

this.visit

(); if(nextSibling!=null) { nextSibling.postorder

();

}

}

}Slide15

Level Order Traversal

Visit root, then all (height-1) nodes, then all (height-2) nodes, … etc.

A

B

C

D

E

F

G

HSlide16

Level Order Traversal

Level Order Traversal Realization by Queues

Queue:

A

A

B

C

D

E

F

G

H

B

C

D

E

F

G

H

A

Visiting Sequence:

B

D

E

F

C

G

HSlide17

Binary Tree

A Binary TreeNo node has > 2 childrenEvery child is either left child or a right child, even if it is the only childSlide18

Representing Binary Tree

Binary Tree Node

Right Child

Parent

Item

Left Child

Class

BiTreeNode

{

Object item ;

BiTreeNode

parent ;

BiTreeNode

leftChild

;

BiTreeNode

rightChild

;

}Slide19

A Binary Tree

Right Child

Parent

Item

Left Child

Right Child

Parent

Item

Left Child

Right Child

Parent

Item

Left Child

Right Child

Parent

Item

Left Child

Right Child

Parent

Item

Left Child

Right Child

Parent

Item

Left ChildSlide20

Inorder Traversal for Binary Tree

Visit left child, then node, then right child

Class

Bi

TreeNode

{

public void

in

order

()

{

if(

leftChild

!=null){

leftChild.inorder

(); } this.visit(); if(

rightChild

!=null) {

rightChild.inorder

();

}

}

}Slide21

Inorder Traversal for Binary Tree

Visualization of inorder traversal

A

B

C

D

E

FSlide22

Thank you!