Algorithmic and Advanced Programming in Python - Syllabus in Computer Science, Decision Making & Data - Masterclass 5
Algorithmic and Advanced Programming in Python - Syllabus in Computer Science, Decision Making & Data - Masterclass 5
Programming in Python
• To learn the ins and outs of the essential data structure, experiencing in
practice has proved to be a much more powerful way to learn data
structures
• Question: is the order important? can you cite some linear data structures?
• The 𝑟𝑜𝑜𝑡 of a tree is the node with no parents. There can be at most
one root node in a tree (node 𝐴 in the above example).
• An 𝑒𝑑𝑔𝑒 refers to the link from parent to child (all links in the figure).
• The set of all nodes at a given depth is called the 𝑙𝑒𝑣𝑒𝑙 of the tree (𝐵,
𝐶 and 𝐷 are the same level). The root node is at level zero.
• The 𝑑𝑒𝑝𝑡ℎ of a node is the length of the path from the root to the node
• The set of all nodes at a given depth is called the 𝑙𝑒𝑣𝑒𝑙 of the tree (𝐵,
𝐶 and 𝐷 are the same level). The root node is at level zero.
• The 𝑑𝑒𝑝𝑡ℎ of a node is the length of the path from the root to the node
• Question: what is the depth of G?
• The set of all nodes at a given depth is called the 𝑙𝑒𝑣𝑒𝑙 of the tree (𝐵,
𝐶 and 𝐷 are the same level). The root node is at level zero.
• The 𝑑𝑒𝑝𝑡ℎ of a node is the length of the path from the root to the node
• Question: what is the depth of G?
• The depth of 𝐺 is 2, 𝐴 − 𝐶 − 𝐺
• The ℎ𝑒𝑖𝑔ℎ𝑡 of a node is the length of the path from that node to the
deepest node. The height of a tree is the length of the path from the
root to the deepest node in the tree. A (rooted) tree with only one node
(the root) has a height of zero.
• Question: what is the height of 𝐵 ?
• The ℎ𝑒𝑖𝑔ℎ𝑡 of a node is the length of the path from that node to the
deepest node. The height of a tree is the length of the path from the
root to the deepest node in the tree. A (rooted) tree with only one node
(the root) has a height of zero.
• Question: what is the height of 𝐵 ?
• The height of 𝐵 is 2 (𝐵 − 𝐹 − 𝐽).
• 𝐻𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒 is the maximum height among all the nodes in the
tree and 𝑑𝑒𝑝𝑡ℎ 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒 is the maximum depth among all the
nodes in the tree.
• For a given tree, depth and height returns the same value. But for
individual nodes we may get different results.
• 𝐻𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒 is the maximum height among all the nodes in the
tree and 𝑑𝑒𝑝𝑡ℎ 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒 is the maximum depth among all the
nodes in the tree.
• For a given tree, depth and height returns the same value. But for
individual nodes we may get different results.
• Full Binary Tree: A binary tree is called 𝑓𝑢𝑙𝑙 𝑏𝑖𝑛𝑎𝑟𝑦 𝑡𝑟𝑒𝑒 if each node
has exactly two children and all leaf nodes are at the same level.
• Question: can you draw a full binary tree of depth 3?
• Note: In trees, the default flow is from parent to children and it is not mandatory to show
directed branches. For our discussion, we assume both the representations shown below
are the same.
• If prev is 𝑐𝑢𝑟𝑟𝑒𝑛𝑡′𝑠 left child, we are traversing up the tree from the left.
We look at 𝑐𝑢𝑟𝑟𝑒𝑛𝑡′𝑠 right child. If it is available, then traverse down the
right child (i.e., push right child to the stack); otherwise print 𝑐𝑢𝑟𝑟𝑒𝑛𝑡′𝑠
value and pop it off the stack. If 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 is 𝑐𝑢𝑟𝑟𝑒𝑛𝑡′𝑠 right child, we are
traversing up the tree from the right. In this case, we print 𝑐𝑢𝑟𝑟𝑒𝑛𝑡′𝑠 value
and pop it off the stack.
Algorithmic and advanced Programming in Python 60
Electronic copy available at: https://ssrn.com/abstract=3955447
Level Order Traversal
• Level order traversal is defined as follows:
• Visit the root.
• While traversing level 𝑙, keep all the elements at level 𝑙 + 1 in queue.
• Go to the next level and visit all the nodes at that level.
• Repeat this until all levels are completed.