Tree Notes
Tree Notes
Topics to Cover
1. Tree Basics [Class 1]
1.1 Introduction to Tree Data Structure
1.2 Terminologies of a Tree
1.2.1 Root/Leaf/Non-Leaf Node
1.2.2 Parent/Child
1.2.3 Siblings
1.2.4 Edge/Path
1.2.5 Degree
1.2.6 Depth
1.2.7 Height
1.2.8 Level
1.2.9 Subtree
1.3 Characteristics of a Tree
1.4 Tree Coding
1.4.1 Tree Construction using Linked List (Dynamic Representation)
Practice Problems on Tree Basics
Here we can see that node A is the root node, Nodes E, H, G, and D are the leaf nodes. Nodes A,
B, F, and C are non-leaf nodes. The root node and the non-leaf nodes can be considered as
internal nodes.
Trees | Data Structures | Student Version
1.2.2 Parent/Child:
The direction of the tree is from top to bottom. Which means Node B is the immediate successor
of node A, and Node A is the immediate predecessor of node B. Therefore, node B is the child of
A, whereas, A is the parent of B.
1.2.3 Siblings:
All the nodes having the same parent are known as siblings. Therefore, B, C, and D are siblings,
and F and G are siblings.
1.2.4 Edge/Path:
The link between two nodes is called an edge. A path is known as the consecutive edges from the
source node to the destination node. So, if we asked what is the path from node A to E? The
answer would be A→B→E. A tree having n number of nodes will have (n-1) number of edges.
Here, we have 8 nodes and 7 edges in total.
1.2.5 Degree:
The number of children of a node is known as degree. The degree of node A is 3, node B is 1 and
Node E is 0.
1.2.6 Depth:
The length of the path from a node to the root node is known as the depth. The depth of nodes E,
F, and G is 2; depth of B, C, and D is 1; depth of A is 0; depth of H is 3.
1.2.7 Height:
The length of the longest path from a node to one of its leaf nodes is known as the height. From
node A to the leaf nodes there are four paths: A→B→E, A→C→F→H, A→C→G, and A→D. Of
these four paths, A→C→F→H is the longest path. Hence, the height of Node A is 3.
1.2.8 Level:
Each hierarchy starting from the root is known as the level.
From the above figure, we can see that the level of node A is 0; level of nodes B, C, and D is 1;
level of nodes E, F, and G is 2; and level of node H is 3.
Points to remember:
- The depth and height of a node may not be the same. The depth of A is 0, whereas, the height of A is 3.
- Level of a node == Depth of that node.
Trees | Data Structures | Student Version
1.2.9 Subtree:
A tree that is the child of a Node.
Any tree can be further divided into subtrees with respect to a particular node. Here node A has
three subtrees that are shown on the right side.
1. Find the level, depth, height and degree of the specified nodes of the following tree.
2. Identify which of the following trees are full, complete, perfect and balanced.
3. Write the code to construct a tree of height 3 and minimum number of 9 nodes. Use your
imagination while designing the tree.
Trees | Data Structures | Student Version
2. Binary Trees
A tree is a binary tree if every single node of the tree has at most 2 child nodes.
In-order
Whenever a node is visited for the second time, its element is printed. We start from the root and traverse
its left subtree. If a node does not have a left child, we return to that node for the second time and print its
element. Then we check if it has any right child, and if it does not have a right child either, we again
return to that node for the third time and then go towards its parent node. After all the nodes of its entire
left subtree have been traversed thrice, we head back to the root node for the second time and print its
element. The root is printed after all the nodes of its left subtree are printed. After that, its right subtree is
traversed. After all the nodes of its entire right subtree have been traversed thrice, we head back to the
root node for the third time. In-order traversal also sorts the data in ascending order.
Trees | Data Structures | Student Version
Post-order
Whenever a node is visited for the third time, its element is printed. We start from the root and traverse
its left subtree. If a node does not have a left child, we return to that node for the second time and check if
it has any right child, and if it does not have a right child either, we again return to that node for the third
time, print its element and then go towards its parent node. After all the nodes of its entire left subtree
have been traversed thrice, we head back to the root node and then traverse its right subtree. After all the
nodes of its entire right subtree have been traversed thrice, we head back to the root node for the third
time and print its element. The root is printed at the last.
Here, in the leftmost tree, the no of internal nodes is 3 and the no of leaf nodes is 4. Again in the
rightmost tree, the no of internal nodes is 7 and the no of leaf nodes is 8.
In the leftmost tree, all internal nodes have two children. Therefore, it is a complete binary tree. In the
rightmost tree, all internal nodes except one have two children. The only internal node that has one child,
has its child residing on its left side. Therefore, it is also a complete binary tree.
In the tree in the middle, all the leaf nodes are not on the same level. In the rightmost tree, not all internal
nodes have two children.
In the leftmost tree, the height of Node A’s left subtree is 2 and right subtree is 0. Therefore, the difference
between these two is 2. On the other hand, in the rightmost tree, no nodes have a height difference of
more than 1 in between their left and right subtrees.
Points to remember:
● Every perfect binary tree is also a full binary tree, but every full binary tree is not a perfect binary tree.
Trees | Data Structures | Student Version
2.4 Binary Tree Coding
2.4.1 Tree Construction using Array (Sequential Representation)
Have you ever thought about how to represent binary trees in your program? If you can recall, we have
been using linked lists so far to represent trees. So, there are two ways to represent binary trees:
- Dynamic Representation (Using Linked List)
- Sequential Representation (Using Array)
We have already covered the dynamic representation of trees. Now let us look at how to sequentially
represent binary trees.
1. Traverse the following trees in pre-order, in-order and post-order and print the elements. Show
both simulation and code.
Pre-order:
In-order:
Post-order:
2. Consider the following array and convert it into a binary tree. Show simulation and code.
3. Write a Python function isSymmetric(root) that takes the root of a binary tree, check
whether it is a mirror of itself (i.e., symmetric around its center).
4. Write a Python function isIdentical(root1, root2) that takes the roots of two binary trees,
check whether they are identical or not).
Trees | Data Structures | Student Version
3. Binary Search Trees (BST)
3.1 Characteristics of a BST
Binary Search Tree is a binary tree data with the following fundamental properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
- Each node must have a distinct key, which means no duplicate values are allowed.
The goal of using BST data structure is to search any element within O(log(n)) time complexity.
Here deletion of 99 falls under case 1, deletion of 40 falls under case 2, and deletion of 90, 70 and 75 fall
under case 3. While deleting 75, we replaced 75 with its leftmost child from its right subtree, 80. After
that 85 was put in the 80's previous place.
Note that, after deletion, a balanced BST may become unbalanced and therefore any searching
operation done on it may take a lot longer than O(log(n)). We have to balance the tree if it
becomes unbalanced.
Trees | Data Structures | Student Version
3.3 Balanced vs Unbalanced BST
If the height difference between left and right subtree of any node in the BST is more than one, it is an
unbalanced BST. Otherwise, it is a balanced one. In a balanced BST, any searching operation can take
upto O(log(n)) time complexity. In an unbalanced one, it may take upto O(n) time complexity, which
renders the usage of BST obsolete. Therefore, we should only work with balanced BST and after
conducting any operation on a BST, we must first check if it became unbalanced or not. If it does become
unbalanced, we have to balance it.
1. Convert the following unbalanced BSTs into balanced BSTs. Show simulation.
2. Insert keys 65, 105, 69 into the following BST and show the steps. Show simulation and code.
3. Delete keys 20, 95, 50, 70, 75 into the following BST and show the steps. Show simulation and
code..
4. How can you print the contents of a tree in descending order with and without using stack? Solve
using code.
Trees | Data Structures | Student Version
5. Write a python program that takes the root of a tree and finds its inorder successor and
predecessor.
6. Given a sorted array, write a function that creates a Balanced Binary Search Tree using array
elements. Follow the steps mentioned below to implement the approach:
a. Set The middle element of the array as root.
b. Recursively do the same for the left half and right half.
i. Get the middle of the left half and make it the left child of the root created in step 1.
ii. Get the middle of the right half and make it the right child of the root created in step 1.
c. Print the preorder of the tree.