![Page 1: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/1.jpg)
Tree traversals and binary trees
Comp Sci 1575 Data Structures
![Page 2: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/2.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 3: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/3.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 4: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/4.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees!
![Page 5: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/5.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Tree is a non-linear ordered ADT
![Page 6: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/6.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 7: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/7.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees are upside down in computer science
Real tree: leaves at the top, root(s) at the bottom
Computer science tree: root at the top, leave(s) at the bottom
![Page 8: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/8.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees are composed of nodes and edges
• Node is an element in a tree• Edge is the connection between one node and another
![Page 9: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/9.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Tree
• Widely used abstract data type (ADT), or data structureimplementing the ADT, that simulates a hierarchical treestructure, with a root value and subtrees of children with aparent node, represented as a set of linked nodes.
• A (possibly non-linear) data structure made up of nodes orvertices and edges without having any cycle.
![Page 10: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/10.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 11: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/11.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 12: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/12.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Root at the top, leaves at the bottom
![Page 13: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/13.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Family matters
• Root is the node at the top of the tree, and has noparent. There is only one root per tree and one path fromthe root node to any node.
• Leaves are bottom nodes without any sub-trees orchildren (less commonly called External node)
![Page 14: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/14.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 15: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/15.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Parents are ancestors of children
![Page 16: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/16.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Family matters
• Parent of a node is the single node linked directly aboveit. Any node except the root node has one edge upward toa node called parent. Parent is the converse notion of achild.
• Child of a node is a node linked directly below it, directlyconnected by its edge downward, when moving away fromthe root
• Siblings are an n group of nodes with the same parent.
• Ancestor is any node from which a node descends directlyor indirectly, which is any node reachable by repeatedproceeding from child to parent.
• Descendant is any node that descends from a nodedirectly or indirectly, which is any node reachable byrepeated proceeding from parent to child.
• If there is a path from n1 to n2 , then n1 is an ancestor ofn2 and n2 is a descendant of n1 .
![Page 17: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/17.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 18: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/18.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Left and right are defined for nodes and sub-trees
• Sub-tree is a smaller tree ’rooted’ by some particularnode in the tree, which are descendants of that node.
![Page 19: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/19.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 20: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/20.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Internal nodes
• Internal node is a non-root node with at least one child.
![Page 21: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/21.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 22: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/22.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Edges are sometimes called branches
• That there are n− 1 edges follows from the fact that eachedge connects some node to its parent, and every nodeexcept the root has one parent• If a tree has n nodes, then it must have n − 1 edges, as
every node is connected to a parent, except for the root.
![Page 23: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/23.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 24: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/24.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
KQ and sketchpad
![Page 25: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/25.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 26: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/26.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 27: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/27.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees have levels
![Page 28: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/28.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees and nodes have depth and height
Depth• Level/depth of a node represents the familial generation
of a node, or the length of the path from the root to anode. The level of a node is defined by 1 + (the numberof levels between the node and the root(0)). If the rootnode is at level 0, then its next child node is at level 1, itsgrandchild is at level 2, and so on. In other words, thenumber of edges from the tree’s root node to the node.• Level/depth of tree is length of the longest path from
the root to the deepest leaf, or the maximum depth of anyleaf node The depth of a tree is equal to the depth of thedeepest leaf; this is always equal to the height of the tree.
Height• height (opposite concept of level/depth) of a node,ni is the length of the longest path from ni to a leaf. Thusall leaves are at height 0.• Height of a tree is equal to the height of the root.
![Page 29: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/29.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees have levels
• Depth: root is 0; its children are 1, etc.
• What is height of these nodes?
![Page 30: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/30.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 31: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/31.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees have paths
• Path is a sequence of nodes and edges connecting a nodewith a descendant (green or blue above for two differentleaf nodes)
• Only one path from the root to each node.
![Page 32: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/32.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Trees have paths
• A path from node n1 to nk is defined as a sequence ofnodes n1, n2, ..., nk such that ni is the parent of ni + 1for 1 ≤ i < k .
• The length of this path is the number of edges on thepath, namely, k − 1.
• There is a path of length zero from every node to itself.
• With the root is at depth 0, for any node ni , the depth ofni is the length of the unique path from the root to ni .
![Page 33: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/33.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 34: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/34.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Degree of nodes and trees
• Degree of a node is the number of subtrees of a node.• Degree of a tree is the largest degree of any node in a
tree.
![Page 35: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/35.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 36: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/36.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 37: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/37.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
These are all trees
• Even single nodes can be considered trees• Forest is a set of n ≥ 0 disjoint trees.• A list is trivially a tree:
![Page 38: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/38.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 39: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/39.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
These are NOT trees
• No loops or multi-parent children
![Page 40: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/40.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 41: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/41.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Example tree
![Page 42: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/42.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Example tree of characters
• Degree of tree• Degree of each node• Depth of tree• Depth of each node• Height?• Leaves, root, internals
![Page 43: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/43.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 44: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/44.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 45: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/45.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Binary trees
![Page 46: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/46.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Binary tree
• Set of nodes is either empty or consists of a node calledthe root together with two binary trees, called the left andright subtrees, which are disjoint from each other andfrom the root; disjoint means that they have no nodes incommon.
• Each node has at most two children, which are referred toas the left child and the right child.
• No node can have more than two children.
• The depth of an average binary tree is considerably smallerthan N, the average depth is O(sqrt(N)), and for a specialtype of binary tree, namely the binary search tree, theaverage value of the depth is O(log N).
![Page 47: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/47.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 48: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/48.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Perfect binary tree
• Binary tree in which all interior nodes have two childrenand all leaves have the same depth or same level.
![Page 49: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/49.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Full (left) and complete (right) binary trees
Full binary tree (left below)• Tree in which every node in the tree has either 0 or 2
children.• Each node in a full binary tree is either
(1) an internal node with exactly two non-empty children(2) a leaf.
Complete binary tree (right below)• Has a restricted shape obtained by starting at the root and
filling the tree by levels from left to right.• Every level, except possibly the last, is completely filled,
and all nodes in the last level are as far left as possible.• The bottom level has its nodes filled in from the left side.• Can be efficiently represented using an array.
![Page 50: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/50.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Full and complete binary trees
![Page 51: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/51.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Full and complete binary trees
![Page 52: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/52.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Balanced binary trees
• Has the minimum possible maximum height (a.k.a. depth)for the leaf nodes, because for any given number of leafnodes, the leaf nodes are placed at the greatest heightpossible.
• One common balanced tree structure is a binary treestructure in which the left and right subtrees of every nodediffer in height by no more than 1.
• One may also consider binary trees where no leaf is muchfarther away from the root than any other leaf.(Different balancing schemes allow different definitions of”much farther”.)
![Page 53: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/53.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Degenerate trees
Left A degenerate (or pathological) tree is where each parentnode has only one associated child node; performance willbehave like a linked list data structure.
Right Shape of the binary search tree depends entirely on theorder of insertions and deletions, and can becomedegenerate.
![Page 54: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/54.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 55: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/55.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Example application: expression trees
• Expression tree: The leaves are operands, such asconstants or variable names, and the other nodes containoperators.
![Page 56: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/56.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
Outline
1 DefinitionsContextDefinition
2 Parts of a treeRoot, leavesParents, childrenLeft, rightInternal nodesEdges
3 KQ
4 Features of trees and nodesLevels, depth, heightPathsDegree
5 ExamplesThese are treesThere are NOT treesExample trees
6 Binary treesADTVariationsExample application
7 KQ
![Page 57: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/57.jpg)
Definitions
Context
Definition
Parts of a tree
Root, leaves
Parents, children
Left, right
Internal nodes
Edges
KQ
Features oftrees andnodes
Levels, depth, height
Paths
Degree
Examples
These are trees
There are NOT trees
Example trees
Binary trees
ADT
Variations
Example application
KQ
KQ and sketchpad
![Page 58: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/58.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 59: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/59.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Traversals
• Order of list is obvious, tree not so much
• Any process for visiting all of the nodes in some order iscalled a traversal.
• Any traversal that lists every node in the tree exactly onceis called an enumeration of the tree’s nodes.
![Page 60: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/60.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 61: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/61.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Pre-order
• Pre-order traversal: parents is visited before the children
Pre-order: F, B, A, D, C, E, G, I, H
![Page 62: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/62.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Pre-order Recursive
1 Check if the current node is empty / null.2 Display the data part of the root (or current node).3 Traverse the left subtree by recursively calling the
pre-order function.4 Traverse the right subtree by recursively calling the
pre-order function.
Pre-order: F, B, A, D, C, E, G, I, H
![Page 63: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/63.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Pre-order Recursive
1 Process the root
2 Process the nodes in the left subtree with a recursive call
3 Process the nodes in the right subtree with recursive call
Pre-order: F, B, A, D, C, E, G, I, H.
![Page 64: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/64.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Pre-order recursive pseudocode algorithm
Assuming each node is referenced to via a pointer called‘node’, has a left and right pointer, and that leaves have twonull pointers:
p r e o r d e r ( node )i f ( node == n u l l )
returnv i s i t ( node )p r e o r d e r ( node . l e f t )p r e o r d e r ( node . r i g h t )
What is trace on this tree?
![Page 65: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/65.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Pre-order iterative pseudocode algorithm
i t e r a t i v e P r e o r d e r ( node )i f ( node == n u l l )
returns = empty s t a c ks . push ( node )whi le ( not s . i sEmpty ( ) )
node = s . pop ( )v i s i t ( node )// r i g h t c h i l d i s pushed f i r s t so tha t// l e f t i s p r o c e s s e d f i r s ti f ( node . r i g h t != n u l l )
s . push ( node . r i g h t )i f ( node . l e f t != n u l l )
s . push ( node . l e f t )
![Page 66: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/66.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 67: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/67.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Post-order
• Post-order traversal: children are visited before the parent
Post-order: A, C, E, D, B, H, I, G, F.
![Page 68: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/68.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Post-order recursive
1 Check if the current node is empty / null.2 Traverse the left subtree by recursively calling the
post-order function.3 Traverse the right subtree by recursively calling the
post-order function.4 Display the data part of the root (or current node).
Post-order: A, C, E, D, B, H, I, G, F.
![Page 69: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/69.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Post-order recursive
1 Process the nodes in the left subtree with a recursive call
2 Process the nodes in the right subtree with recursive call
3 Process the root
Post-order: A, C, E, D, B, H, I, G, F.
![Page 70: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/70.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Post-order recursive pseudocode algorithm
Assuming each node is referenced to via a pointer called‘node’, has a left and right pointer, and that leaves have twonull pointers:
p o s t o r d e r ( node )i f ( node == n u l l )
returnp o s t o r d e r ( node . l e f t )p o s t o r d e r ( node . r i g h t )v i s i t ( node )
What is trace on this tree?
![Page 71: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/71.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Post-order iterative pseudocode algorithm
i t e r a t i v e P o s t o r d e r ( node )s = empty s t a c kl a s t N o d e V i s i t e d = n u l lwhi le ( not s . i sEmpty ( ) or node != n u l l )
i f ( node != n u l l )s . push ( node )node = node . l e f t
e l s epeekNode = s . peek ( )// i f r i g h t c h i l d e x i s t s and t r a v e r s i n g node// from l e f t c h i l d , then move r i g h ti f ( peekNode . r i g h t != n u l l andl a s t N o d e V i s i t e d != peekNode . r i g h t )
node = peekNode . r i g h te l s e
v i s i t ( peekNode )l a s t N o d e V i s i t e d = s . pop ( )
![Page 72: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/72.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 73: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/73.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
In-order
• An inorder traversal first visits the left child (including itsentire subtree), then visits the node, and finally visits theright child (including its entire subtree).• Binary trees only
In-order: A, B, C, D, E, F, G, H, I.
![Page 74: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/74.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
In-order: recursive
1 Check if the current node is empty / null.
2 Traverse the left subtree by recursively calling the in-orderfunction.
3 Display the data part of the root (or current node).
4 Traverse the right subtree by recursively calling thein-order function.
Why just for binary?
In-order: A, B, C, D, E, F, G, H, I.
![Page 75: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/75.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
In-order: recursive
1 Process the nodes in the left subtree with a recursive call
2 Process the root
3 Process the nodes in the right subtree with recursive call
In-order: A, B, C, D, E, F, G, H, I.
![Page 76: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/76.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
In-order: recursive
Assuming each node is referenced to via a pointer called‘node’, has a left and right pointer, and that leaves have twonull pointers:
i n o r d e r ( node )i f ( node == n u l l )
returni n o r d e r ( node . l e f t )v i s i t ( node )i n o r d e r ( node . r i g h t )
What is trace on this tree?
![Page 77: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/77.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
In-order: iterative
i t e r a t i v e I n o r d e r ( node )s = empty s t a c kwhi le ( not s . i sEmpty ( ) or node != n u l l )
i f ( node != n u l l )s . push ( node )node = node . l e f t
e l s enode = s . pop ( )v i s i t ( node )node = node . r i g h t
![Page 78: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/78.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 79: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/79.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Backward in-order recursive
Useful for printing if indent each item to its depth in the tree
1 Process the nodes in the right subtree with a recursive call
2 Process the root
3 Process the nodes in the left subtree with a recursive call
What is trace on this tree?
![Page 80: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/80.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 81: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/81.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Generalization of traversal to non-binary
1 Perform pre-order operation.
2 For each i from 1 to the number of children do:
1 Visit i-th, if present.2 Perform in-order operation.
3 Perform post-order operation.
![Page 82: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/82.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 83: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/83.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Counting example
template <typename E>i n t count ( BinNode<E> ∗ r o o t ){
// i f Nothing to counti f ( r o o t == NULL)
return 0 ;return 1 + count ( root−> l e f t ( ) )
+ count ( root−>r i g h t ( ) ) ;}
What is trace on this tree?
![Page 84: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/84.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 85: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/85.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
KQ and sketchpad
![Page 86: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/86.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Outline
8 TraversalsPre-order
RecursivePre-order
Post-order
RecursiveIterative
In-order
RecursiveIterative
Backward in-order
RecursiveGeneralizationExamples
9 KQ
10 Apply
![Page 87: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/87.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Apply a function to a whole tree?
• Functions as parameters to functions.
• Pass as parameter: void func(int&)
![Page 88: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/88.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Whichever func used has to accept one int
void a p p l y ( void f u n c ( i n t &) , i n t data [ ] , i n t n ){
i n t i ;f o r ( i = 0 ; i < n ; i ++){
f u n c ( data [ i ] ) ;}
}
void s e v e n u p ( i n t &i ){
i += 7 ;}
a p p l y ( seven up , data , 1 0 ) ;
![Page 89: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/89.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
func() now can have different parameter types
template <typename T>void a p p l y ( void f u n c (T&) , i n t data [ ] , i n t n ){
i n t i ;f o r ( i = 0 ; i < n ; i ++){
f u n c ( data [ i ] ) ;}
}
// f u n c t i o n tha t t a k e s a c h a r a c t e r i n s t e a d
a p p l y ( c o n v e r t t o u p p e r , data , 1 0 ) ;
![Page 90: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/90.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Only requirement is a single parametef for func
template <typename T>void a p p l y (T func , i n t data [ ] , i n t n ){
i n t i ;f o r ( i = 0 ; i < n ; i ++){
f u n c ( data [ i ] ) ;}
}
. . .
a p p l y ( c o n v e r t t o u p p e r , data , 1 0 ) ;a p p l y ( seven up , data , 1 0 ) ;. . .
![Page 91: Comp Sci 1575 Data StructuresTree traversals and binary trees Comp Sci 1575 Data Structures. De nitions Context De nition Parts of a tree Root, leaves Parents, children Left, right](https://reader034.vdocument.in/reader034/viewer/2022042804/5f526198dc395358a915c454/html5/thumbnails/91.jpg)
Traversals
Pre-order
Recursive
Pre-order
Post-order
Recursive
Iterative
In-order
Recursive
Iterative
Backward in-order
Recursive
Generalization
Examples
KQ
Apply
Apply any operation to whole tree
template <typename T, typename E>void p r e o r d e r (T func , BTNode<E> ∗ n o d e p t r ){
i f ( n o d e p t r != NULL){
f u n c ( n o d e p t r−>data ) ;p r e o r d e r ( func , n o d e p t r−> l e f t ) ;p r e o r d e r ( func , n o d e p t r−>r i g h t ) ;
}}
void s e v e n u p ( i n t & i ){
i +=7;}
p r e o r d e r ( seven up , n o d e p t r ) ;