concept of trees
DESCRIPTION
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: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/1.jpg)
Concept of Trees
Min ChenSchool of Computer Science and Engineering Seoul National University
Data Structure: Chapter 6
![Page 2: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/2.jpg)
Content
Definition of Trees Representing Rooted Tree Tree Traversal
Preorder Traversal Postorder Traversal Level Order Traversal
![Page 3: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/3.jpg)
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
![Page 4: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/4.jpg)
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
![Page 5: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/5.jpg)
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
![Page 6: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/6.jpg)
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
![Page 7: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/7.jpg)
Sibling Tree
Next Sibling
Parent
ItemFirst Child
Class SibTreeNode{ Object item ; SibTreeNode parent ; SibTreeNodefirstChild; SibTreeNodenextSibling;}
![Page 8: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/8.jpg)
Sibling Tree
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
Next Sibling
Parent
ItemFirst Child
![Page 9: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/9.jpg)
Tree Traversal
Rooted Tree Preorder Traversal Postorder Traversal Level Order Traversal
Binary Tree Inorder Traveral
![Page 10: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/10.jpg)
Preorder Traversal
Visit each node before recursively visiting its children, left to right
A
B C
D E F G H
AA’s First Child
BB’s First Child
DD has no child, then sibling
EE has no child, then sibling
FF has no child, no sibling
B has no child, no siblingA has no child, then sibling
CC’s First Child
GG has no child, then sibling
HH has no child, no sibling
![Page 11: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/11.jpg)
Preorder Traversal
Class SibTreeNode { public void preorder() { this.visit(); if(firstChild!=null){
firstChild.preorder(); } if(nextSibling!=null){
nextSibling.preorder(); } }}
Preorder Traversal Realization by Recursion
![Page 12: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/12.jpg)
Preorder Traversal
Preorder Traversal Realization by Stacks
A
B C
D E F G HABC
A
Stack:
Visiting Sequence: B
DEF
D E F C
GH
G H
![Page 13: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/13.jpg)
Postorder Traversal
Visit each node’s children (left-to-right) before the node itself
A
B C
D E F G H
![Page 14: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/14.jpg)
Postorder Traversal
Postorder Traversal Realization by RecursionClass SibTreeNode {
public void postorder() { if(firstChild!=null) {
firstChild.postorder(); } this.visit(); if(nextSibling!=null) {
nextSibling.postorder(); } }}
![Page 15: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/15.jpg)
Level Order Traversal
Visit root, then all (height-1) nodes, then all (height-2) nodes, … etc.
A
B C
D E F G H
![Page 16: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/16.jpg)
Level Order Traversal
Level Order Traversal Realization by Queues Queue:
A
A
B C
D E F G H
BCDEFGH
AVisiting Sequence: B D E FC G H
![Page 17: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/17.jpg)
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
![Page 18: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/18.jpg)
Representing Binary Tree Binary Tree Node
Right Child
Parent
ItemLeft Child
Class BiTreeNode{ Object item ; BiTreeNode parent ; BiTreeNode leftChild; BiTreeNode rightChild;}
![Page 19: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/19.jpg)
A Binary Tree
Right Child
Parent
ItemLeft Child
Right Child
Parent
ItemLeft Child
Right Child
Parent
ItemLeft Child
Right Child
Parent
ItemLeft Child
Right Child
Parent
ItemLeft Child
Right Child
Parent
ItemLeft Child
![Page 20: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/20.jpg)
Inorder Traversal for Binary Tree Visit left child, then node, then right
childClass BiTreeNode { public void inorder() { if(leftChild!=null){
leftChild.inorder();}this.visit();if(rightChild!=null) {
rightChild.inorder();}
}}
![Page 21: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/21.jpg)
Inorder Traversal for Binary Tree Visualization of inorder traversal
A
B C
D E F
![Page 22: Concept of Trees](https://reader036.vdocuments.us/reader036/viewer/2022062305/56816108550346895dd051c6/html5/thumbnails/22.jpg)
Thank you!