Professional Documents
Culture Documents
Structures
WMSU
A
G D
E C G
Depth of a Binary Tree
This example has a binary depth = 0
A
Complete Binary Trees
• Given a complete binary tree of N nodes, what is the
depth?
When N = 1,
then D = 0 When N = 2, When N = 3,
then D = 1 then D = 1 When N = 4, When N = 7,
then D = 2 then D = 2
D = floor(log N) = O(log N)
Complete Binary Trees
• Given a binary tree of N nodes, what is the maximum
possible depth?
When N = 1,
then D = 0 When N = 2, When N = 7,
then D = 1 When N = 3, then D = 6
then D = 2 When N = 4,
then D = 3
D = O(N)
Binary Tree: Summary
• Binary trees contain nodes.
• Each node may have a left child and a right child.
• If you start from any node and move upward, you will
eventually reach the root.
• Every node except the root has one parent. The root has
no parent.
• Complete binary trees require the nodes to fill in each
level from left-to-right before starting the next level.
Python Implementation of Binary Tree
• Each Node in a Binary Tree Contains 3 Pieces of
Information:
– The Left Node
– The Right Node
– Data
• We just create a Node class and add assign a value to
the node. This becomes tree with only a root node.
Python Implementation of Binary Tree
• Binary Trees when
implemented are Often 10
Sorted to a degree.
• In our example, The Left
Child is Less than the 8 14
Parent, while the Right
Child is Greater than the
Parent. 5 9 12
Python Implementation of Binary Tree
• Following this Pattern,
Let’s try to Insert: 10
11
8 14
5 9 12
Python Implementation of Binary Tree
10 11 ?
8 14
5 9 12
Python Implementation of Binary Tree
10
8 14 11 ?
5 9 12
Python Implementation of Binary Tree
10
8 14
5 9 12 11 ?
Python Implementation of Binary Tree
10
8 14
5 9 12
11
Python Implementation of Binary Tree: Create
10
Output:
10
Python Implementation of Binary Tree: Insert
• To insert into a tree we use the same Node class created
above and add a insert method to it.
• The insert class compares the value of the node to the
parent node and decides to add it as a left node or a right
node.
• Finally the PrintTree class is used to print the tree.
Python Implementation of Binary Tree: Insert
12
Python Implementation Example
12 6 ?
True?
Python Implementation Example
12 6 ?
True?
Python Implementation Example
12
6
Python Implementation Example
12
6 14
Python Implementation Example
12
6 14
3
Python Implementation Example
3 ?
12
True?
6 14
3
Python Implementation Example
3 ?
12
True?
6 14
3
Python Implementation Example
3 ?
12
6 14
Python Implementation Example
3 ?
12
True? 6 14
Python Implementation Example
3 ?
12
6 14
True?
Python Implementation Example
12
6 14
3
Python Implementation Example
12
6 14
True? 3
Python Implementation Example
12
6 14
3
Python Implementation Example
12
6 14
True? 3
Python Implementation Example
12
6 14
3
Python Implementation Example
12
6 14
3
True?
Python Implementation Example
12
6 14
3
Python Implementation Example
12
6 14
True?
3
Python Implementation Example
12
6 14
3 6
Python Implementation Example
12
6 14
True?
3 6
Python Implementation Example
12
6 14
3 6 12
Python Implementation Example
12
6 14
True?
3 6 12
Python Implementation Example
12
6 14
True? 3
3 6 12
Python Implementation Example
12
6 14
3 6 12 14
Python Implementation Example
12
6 14
True?
3 6 12 14
Python Implementation Example
12
6 14
3 6 12 14
Traversing a Tree
• The tree can be traversed by deciding on a sequence to
visit each node.
• We can start at a node then visit the left sub-tree first and
right sub-tree next, or we can also visit the right sub-tree
first and left sub-tree next.
Traversing a Tree
• Accordingly there are different names for these tree
traversal methods
• Traversal is a process to visit all the nodes of a tree and
may print their values too.
• Because, all nodes are connected via edges (links) we
always start from the root (head) node.
• That is, we cannot randomly access a node in a tree
Traversing a Tree
• There are three ways which we use to traverse a tree:
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
In-Order Traversal
• In this traversal method, the left subtree is visited first,
then the root and later the right sub-tree.
• Always remember that every node may represent a
subtree itself.
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• Add Data of This Node
to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27 31
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27 31 35
and add return to Result
• Return Result
In-Order Traversal
27
Traverse()
• Initialize Result [ ]
14 35
• If there is Left Child
– Traverse() Left Child and
add return to Result
10 19 31 42
• Add Data of This Node
to Result
• If there is Right Child
– Traverse() Right Child
10 14 19 27 31 35 42
and add return to Result
• Return Result
In-Order Traversal Output
27
14 35
10 19 31 42
10 14 19 27 31 35 42
Pre-Order Traversal
• In this traversal method, the root node is visited first, then
the left subtree and finally the right subtree.
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31 42
• Return Result
Pre-Order Traversal
Traverse()
• Initialize Result [ ] 27
• Add Data of This Node
to Result
• If there is Left Child 14 35
– Traverse() Left Child and
add return to Result
• If there is Right Child 10 19 31 42
– Traverse() Right Child
and add return to Result
27 14 10 19 35 31 42
• Return Result
In-Order Traversal Output
27
14 35
10 19 31 42
27 14 10 19 35 31 42
Post-Order Traversal
• In this traversal method, the root node is visited last,
hence the name.
• First, we traverse the left subtree, then the right subtree
and finally the root node.
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42 35
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42 35
• Return Result
Post-Order Traversal
Traverse()
• Initialize Result [ ] 27
• If there is Left Child
– Traverse() Left Child and
add return to Result 14 35
• If there is Right Child
– Traverse() Right Child
and add return to Result 10 19 31 42
• Add Data of This Node
to Result
10 19 14 31 42 35 27
• Return Result
Euler Tour Traversal
• Is the Generic Traversal of a
binary tree, includes special
cases which are the pre-order,
post-order, and in-order
traversals.
• “Walk” around the tree and visit
each node
Euler Tour Traversal
Output: 1 5 4 2 4 3 4 5 1
Euler Tour Traversal
Output: 1 5 4 2 4 3 4 5 1
Euler Tour Traversal
Euler Tour Traversal
Breadth First Traversals
• Breadth First Traversals will
look through all nodes for a
certain depth of the Binary
Tree before moving to the
next depth
Output: 0 1 2 3 4 5 6
Depth First Traversals
• Depth First Traversals will
look through the left
subtrees first, then move on
the right subtrees.
• Moves similarly to Pre-
Order Traversals, as we
Check the Data First, before
checking the Left Subtree,
then finally checking the
Right Subtree Output: 0 1 3 4 2 5 6