saturday, 04 apr 2010 university of palestine computer science ii trees
TRANSCRIPT
Saturday 04 Apr 2010
University of Palestine
Computer Science II
Trees
Trees
bull Introductionbull The Trees Abstract Date Typebull Basic Algorithms on Trees
Outline
Trees
bull Productivity experts say that breakthroughs come by thinking ldquononlinearlyrdquo
bull Trees as a nonlinear data structure gives ways of accessing and finding elements of better performance than linear structures such as sequences
Introduction
Trees
bull The main terminology for tree data structures comes from family trees with the term ldquoparentrdquo ldquochildrdquo ldquoancestorrdquo and ldquodescendentrdquo
Introduction
Trees
Example Prophets tree [1]
سيدنا على صلي اللهممحمد
[1] httpimg246imageshackusimg24635828618qz1png
The Trees Abstract Data Type
A tree is an abstract data type that stores elements hierarchally With the exception of the top element each element in a tree has a parent element and zero or more children
A tree is usually visualized by placing elements inside ovals and rectangles and by drawing the connections between parents and children with straight lines
Definition
root
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Trees
bull Introductionbull The Trees Abstract Date Typebull Basic Algorithms on Trees
Outline
Trees
bull Productivity experts say that breakthroughs come by thinking ldquononlinearlyrdquo
bull Trees as a nonlinear data structure gives ways of accessing and finding elements of better performance than linear structures such as sequences
Introduction
Trees
bull The main terminology for tree data structures comes from family trees with the term ldquoparentrdquo ldquochildrdquo ldquoancestorrdquo and ldquodescendentrdquo
Introduction
Trees
Example Prophets tree [1]
سيدنا على صلي اللهممحمد
[1] httpimg246imageshackusimg24635828618qz1png
The Trees Abstract Data Type
A tree is an abstract data type that stores elements hierarchally With the exception of the top element each element in a tree has a parent element and zero or more children
A tree is usually visualized by placing elements inside ovals and rectangles and by drawing the connections between parents and children with straight lines
Definition
root
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Trees
bull Productivity experts say that breakthroughs come by thinking ldquononlinearlyrdquo
bull Trees as a nonlinear data structure gives ways of accessing and finding elements of better performance than linear structures such as sequences
Introduction
Trees
bull The main terminology for tree data structures comes from family trees with the term ldquoparentrdquo ldquochildrdquo ldquoancestorrdquo and ldquodescendentrdquo
Introduction
Trees
Example Prophets tree [1]
سيدنا على صلي اللهممحمد
[1] httpimg246imageshackusimg24635828618qz1png
The Trees Abstract Data Type
A tree is an abstract data type that stores elements hierarchally With the exception of the top element each element in a tree has a parent element and zero or more children
A tree is usually visualized by placing elements inside ovals and rectangles and by drawing the connections between parents and children with straight lines
Definition
root
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Trees
bull The main terminology for tree data structures comes from family trees with the term ldquoparentrdquo ldquochildrdquo ldquoancestorrdquo and ldquodescendentrdquo
Introduction
Trees
Example Prophets tree [1]
سيدنا على صلي اللهممحمد
[1] httpimg246imageshackusimg24635828618qz1png
The Trees Abstract Data Type
A tree is an abstract data type that stores elements hierarchally With the exception of the top element each element in a tree has a parent element and zero or more children
A tree is usually visualized by placing elements inside ovals and rectangles and by drawing the connections between parents and children with straight lines
Definition
root
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Trees
Example Prophets tree [1]
سيدنا على صلي اللهممحمد
[1] httpimg246imageshackusimg24635828618qz1png
The Trees Abstract Data Type
A tree is an abstract data type that stores elements hierarchally With the exception of the top element each element in a tree has a parent element and zero or more children
A tree is usually visualized by placing elements inside ovals and rectangles and by drawing the connections between parents and children with straight lines
Definition
root
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
A tree is an abstract data type that stores elements hierarchally With the exception of the top element each element in a tree has a parent element and zero or more children
A tree is usually visualized by placing elements inside ovals and rectangles and by drawing the connections between parents and children with straight lines
Definition
root
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
The tree T is a set of nodes storing elements in a parent-child relationship with the following properties
bull T has a distinguished node r called the root of T that has no parent
bull Each node v of T distinct from r has a parent node ubull For every node v of T the root r is ancestor of vbull If a node u is the parent of node v then we say that v is a child of ubull Two nodes that are children of the same parent are siblingsbull A node is external or leave if it has no childrenbull A node is internal if it has one or more children
Terminology and Basic properties
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Terminology and Basic properties
root
Parent of I J K
children of F
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Terminology and Basic properties
Descendant of B A
Descendants of F B and A
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Terminology and Basic properties
Ancestor of all other nodes
Ancestor of I J K
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
A tree T is ordered if there is a linear ordering defined for the children of each node in such a way we can identify the children of a node as being the first second third and so on Ordered trees typically indicate the linear relationship existing between siblings by listing them in a sequence or enumeration in the correct order
Terminology and Basic Properties
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Terminology and Basic Properties
Book
Preface Part A Part B References
P1 P2 CH 1 Ch2 Ch3 Ch4 Ch5 P1
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
A binary tree is a tree with the following propertiesbull Each internal node has at most two children (exactly two for properbinary trees)bull The children of a node are an ordered pairbull We call the children of an internal node left child and right childbull Alternative recursive definition bull A binary tree is either1048708 a tree consisting of a single node or1048708 a tree whose root has an ordered pair of children each of which is a
binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Example of a binary tree
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Binary trees applicationsDecision trees
Binary tree associated with a decision process1048708 internal nodes questions with yesno answer1048708 external nodes decisionsExample dining
Terminology and Basic Properties
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Binary trees applications
Binary tree associated with an arithmetic expression1048708 internal nodes operators1048708 external nodes operandsExample arithmetic expression tree for theexpression (2 times (a minus 1) + (3 times b))
Terminology and Basic Properties
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Generic methods
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()
Accessor methods
Position root()Position parent(Position p)Enumeration children(Position p)
Query methods
boolean isInternal(Position p)boolean isExternal(Position p)boolean isRoot(Position p)
Update method
Object replace (Position p Object o)void swap(Position v Position w)
Trees ADT methods
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
A Tree Interface in Java
PositionalContainerltltinterfacegtgt
SimpleTreeltltinterfacegtgt
bull int size()bull boolean isEmpty()bull Enumeration elements()bull Enumeration positions()bull Object replace (Position p Object o)bull void swap(Position v Position w)
bull Position root()bull Position parent(Position p)bull Enumeration children(Position p)bull boolean isInternal(Position p)bull boolean isExternal(Position p)bull boolean isRoot(Position p)
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Basic Algorithms on Trees
Depth and Height
bull The depth of a node v is the number of ancestors of v excluding v itself
bull If v is the root then the depth of v is 0bull Otherwise the depth of v is one plus the depth of the parent v
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Basic Algorithms on Trees
Depth and Height
public int depth(SimpleTree TPosition v)
if(TisRoot(v))
return(0)
else
return(1+depth(Tparent(v)))
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Basic Algorithms on Trees
Depth and Height
bull The height of an entire tree T is defined to be the height of the root of T Another way to view the height of a tree T is the maximum depth of a node of T which is achieved at one or more external nodes
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Basic Algorithms on Trees
Depth and Height
public int height1(SimpleTree T)
int h
Enumberation nodes_of_T=Tpositions()
while(nodes_of_ThasMoreElements())
Position v=(Position)nodes_of_TnextElement()
if(TisExternal(v))
h=Mathmax(hdepth(Tv))
return h
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
The Trees Abstract Data Type
Example
Height Algorithm 1
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Basic Algorithms on Trees
Depth and Height
public int height2(SimpleTree T Position v)if(TisExternal(v))return 0elseint h=0Enumeration children_of_v= Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()h=Mathmax(hTheight2(w))return 1+h
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Preorder Traversal
Definition
bull A traversal of a tree T is a systematic way of accessing or ldquovisitingrdquo all the nodes of T
bull In a preorder traversal of a tree T the root of T is visited first and then the subtree rooted at its children are traversed recursively
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Preorder TraversalAlgorithm
Algorithm preorder(Tv)Visit(v)for each child w of v dopreorder(Tw)
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Preorder TraversalPreorder Print
public void preorderPrint(SimpleTree TPosition v)Systemoutprintln(Telement(v))Enumeration children_of_v=Tchildren(v)while(children_of_vhasMoreElements())Position w=(Position)children_of_vnextElement()preorderPrint(Tw)
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Postorder TraversalDefinition
bull Recursively traverses the subtrees rooted at the children of the root first and then visits the root
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-
Preorder TraversalAlgorithm
Algorithm postorder(Tv)for each child w of v dopreorder(Tw)Visit(v)
- Computer Science II
- Trees
- Slide 3
- Slide 4
- Slide 5
- The Trees Abstract Data Type
- Slide 7
- Slide 8
- Slide 9
- Slide 10
- Slide 11
- Slide 12
- Slide 13
- Slide 14
- Slide 15
- Slide 16
- Slide 17
- Slide 18
- Basic Algorithms on Trees
- Slide 20
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Preorder Traversal
- Slide 26
- Slide 27
- Postorder Traversal
- Slide 29
-