min chen school of computer science and engineering seoul national university
Download
Skip this Video
Download Presentation
Concept of Trees

Loading in 2 Seconds...

play fullscreen
1 / 22

Concept of Trees - PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on

Data Structure: Chapter 6. Min Chen School of Computer Science and Engineering Seoul National University. Concept of Trees. Content. Definition of Trees Representing Rooted Tree Tree Traversal Preorder Traversal Postorder Traversal Level Order Traversal. Definition of Trees. Tree:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Concept of Trees' - yanni


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
min chen school of computer science and engineering seoul national university

Data Structure: Chapter 6

Min Chen

School of Computer Science and Engineering

Seoul National University

Concept of Trees

content
Content
  • Definition of Trees
  • Representing Rooted Tree
  • Tree Traversal
    • Preorder Traversal
    • Postorder Traversal
    • Level Order Traversal
definition of trees
Definition of Trees
  • Tree:
    • Set of nodes and edges that connect them
    • Exactly one path between any 2 nodes
  • Rooted 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 children
some definitions
Some Definitions
  • Leaf
    • Node with no children
  • Siblings
    • Nodes with same parent
  • Ancestors
    • nodes 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 Descendant
some definitions 2
Some Definitions (2)
  • Length of path
    • Number of edges in path
  • Depth of node n
    • Length 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 descendants
representing rooted trees
Representing Rooted Trees
  • G & T
    • Each node has 3 references stored in a list
      • Item
      • Parent
      • Children
  • Another Option: Sibling Tree
    • Siblings are directly linked
sibling tree
Sibling Tree

Parent

Class SibTreeNode

{

Object item ;

SibTreeNode parent ;

SibTreeNodefirstChild;

SibTreeNodenextSibling;

}

Item

First Child

Next Sibling

sibling tree1
Sibling Tree

Parent

Parent

Parent

Parent

Parent

Parent

Parent

Parent

Item

Item

Item

Item

Item

Item

Item

Item

First Child

First Child

First Child

First Child

First Child

First Child

First Child

First Child

Next Sibling

Next Sibling

Next Sibling

Next Sibling

Next Sibling

Next Sibling

Next Sibling

Next Sibling

tree traversal
Tree Traversal
  • Rooted Tree
    • Preorder Traversal
    • Postorder Traversal
    • Level Order Traversal
  • Binary Tree
    • InorderTraveral
preorder traversal
Preorder Traversal
  • Visit each node before recursively visiting its children, left to right

B has no child, no sibling

A

A

A has no child, then sibling

A’s First Child

C

B

B

C

C’s First Child

B’s First Child

D

G

D

E

F

G

H

G has no child, then sibling

D has no child, then sibling

E

H

H has no child, no sibling

E has no child, then sibling

F

F has no child, no sibling

preorder traversal1
Preorder Traversal
  • Preorder Traversal Realization by Recursion

Class SibTreeNode {

public void preorder() {

this.visit();

if(firstChild!=null){

firstChild.preorder();

}

if(nextSibling!=null){

nextSibling.preorder();

}

}

}

preorder traversal2
Preorder Traversal
  • Preorder Traversal Realization by Stacks

Stack:

A

B

C

D

E

G

B

F

D

E

F

G

H

C

H

A

Visiting Sequence:

A

B

D

E

F

C

G

H

postorder traversal
Postorder Traversal
  • Visit each node’s children (left-to-right) before the node itself

A

B

C

D

E

F

G

H

postorder traversal1
Postorder Traversal
  • Postorder Traversal Realization by Recursion

Class SibTreeNode {

public void postorder() {

if(firstChild!=null) {

firstChild.postorder();

}

this.visit();

if(nextSibling!=null) {

nextSibling.postorder();

}

}

}

level order traversal
Level Order Traversal
  • Visit root, then all (height-1) nodes, then all (height-2) nodes, … etc.

A

B

C

D

E

F

G

H

level order traversal1
Level Order Traversal
  • Level Order Traversal Realization by Queues

Queue:

A

H

B

C

G

F

D

E

F

G

H

E

D

C

B

A

Visiting Sequence:

A

B

C

D

E

F

G

H

binary tree
Binary Tree
  • A Binary Tree
    • No node has > 2 children
    • Every child is either left child or a right child, even if it is the only child
representing binary tree
Representing Binary Tree
  • Binary Tree Node

Parent

Item

Class BiTreeNode

{

Object item ;

BiTreeNode parent ;

BiTreeNodeleftChild;

BiTreeNoderightChild;

}

Left Child

Right Child

a binary tree
A Binary Tree

Parent

Parent

Parent

Parent

Parent

Parent

Item

Item

Item

Item

Item

Item

Left Child

Left Child

Left Child

Left Child

Left Child

Left Child

Right Child

Right Child

Right Child

Right Child

Right Child

Right Child

inorder traversal for binary tree
Inorder Traversal for Binary Tree
  • Visit left child, then node, then right child

Class BiTreeNode{

public void inorder() {

if(leftChild!=null){

leftChild.inorder();

}

this.visit();

if(rightChild!=null) {

rightChild.inorder();

}

}

}

inorder traversal for binary tree1
Inorder Traversal for Binary Tree
  • Visualization of inorder traversal

A

B

C

D

E

F

ad