Introduction to Data Structure
Download
Report
Transcript Introduction to Data Structure
Data Structure: Chapter 6
Min Chen
School of Computer Science and Engineering
Seoul National University
Definition of Trees
Representing Rooted Tree
Tree Traversal
Preorder Traversal
Postorder Traversal
Level Order Traversal
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
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
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
G &T
Each node has 3 references stored in a list
▪ Item
▪ Parent
▪ Children
Another Option: Sibling Tree
Siblings are directly linked
Class SibTreeNode
{
Object
item ;
SibTreeNode
parent ;
SibTreeNode
firstChild;
SibTreeNode
nextSibling;
}
Parent
Item
First Child
Next Sibling
Parent
Item
First Child
First Child
First Child
Next Sibling
Parent
Parent
Parent
Item
Item
Item
Next Sibling
First Child
Next Sibling
First Child
Next Sibling
Parent
Parent
Parent
Parent
Item
Item
Item
Item
Next Sibling
First Child
Next Sibling
First Child
Next Sibling
First Child
Next Sibling
Rooted Tree
Preorder Traversal
Postorder Traversal
Level Order Traversal
Binary Tree
Inorder Traveral
Visit each node before recursively visiting its
children, left to right
A
A
A’s First Child
B
D
E
B
C
F
G
B’s First Child
D
H
B has no child, no sibling
A has no child, then sibling
C
C’s First Child
G
D has no child, then sibling G has no child, then sibling
E
H
E has no child, then sibling H has no child, no sibling
F
F has no child, no sibling
Preorder Traversal Realization by Recursion
Class SibTreeNode {
public void preorder() {
this.visit();
if(firstChild!=null){
firstChild.preorder();
}
if(nextSibling!=null){
nextSibling.preorder();
}
}
}
Preorder Traversal Realization by Stacks
Stack:
A
B
C
D
E
D
E
F
G
G
B
F
H
H
A
C
Visiting Sequence:
A
B
D
E
F
C
G
H
Visit each node’s children (left-to-right)
before the node itself
A
B
D
E
C
F
G
H
Postorder Traversal Realization by Recursion
Class SibTreeNode {
public void postorder() {
if(firstChild!=null) {
firstChild.postorder();
}
this.visit();
if(nextSibling!=null) {
nextSibling.postorder();
}
}
}
Visit root, then all (height-1) nodes, then all
(height-2) nodes, … etc.
A
B
D
E
C
F
G
H
Level Order Traversal Realization by Queues
Queue:
A
B
H
C
G
F
D
E
F
G
H
E
D
C
B
Visiting Sequence:
A
B
C
D
E
F
G
H
A
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
Binary Tree Node
Class BiTreeNode
{
Object
item ;
BiTreeNode
parent ;
BiTreeNode
leftChild;
BiTreeNode
rightChild;
}
Parent
Item
Left Child
Right Child
Parent
Item
Left Child
Right Child
Parent
Parent
Item
Item
Left Child
Right Child
Left Child
Right Child
Parent
Parent
Parent
Item
Item
Item
Left Child
Right Child
Left Child
Right Child
Left Child
Right Child
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();
}
}
}
Visualization of inorder traversal
A
B
D
C
E
F