Data Structures &
Algorithms
Trees, Binary Search Trees & AVL Trees
Trees
● A tree is a collection of elements (nodes)
● Each node may have 0 or more successors
○ (Unlike a list, which has 0 or 1 successor)
● Each node (except the starting / top node, called the root) has exactly one
predecessor
● Links from a node to its successors are called branches
● Successors of a node are called its children
● Predecessor of a node is called its parent
● Nodes with same parent are siblings
● Nodes with no children are called leaves
● For any node n, the depth of n is the length of the unique path from the root to
n
● Thus, the root is at depth 0
● The height of a node n is the length of the longest path from n to a leaf
● Thus all leaves are at height 0
● Applications of trees
○ Trees are used to store information that naturally forms a hierarchy. For example, the file
system on a computer
○ Animal kingdom, plant kingdom, family trees etc
● Subtree of a node:
○ A tree whose root is a child of that node
● Level of a node:
○ A measure of its distance from the root:
○ Level of the root = 1
○ Level of other nodes = 1 + level of parent
Binary Trees
● A tree whose nodes have at most
2 children is called a binary tree
● Since each node in a binary tree
can have only 2 children, we
typically name them the left and
the right child
Binary Search Trees (BST)
● A binary search tree is a
binary tree which satisfies
the binary search tree
property
● Binary Search Tree
property: The value stored
in a node is greater than all
the values in the node’s left
subtree and less than those
in its right subtree
Structure of a BST Node
● Every node has three components; data, and left & right pointers
The notations NULL, NIL &
None which you might
encounter during the
discussions are
equivalent/interchangeable.
A null pointer is a pointer
which points to nothing and
is used to mark the end of a
reference.
Insertion into a BST
1. Start
2. Let v be the value to be inserted
3. Create a node, say z, with value v and left and right as NULL
ie z.value = v, z.left = NULL & z.right = NULL
4. If the BST is initially empty(i.e root=NULL), then root=z, stop
5. Else, temp=root
a. While temp ≠ NULL
i. y = temp
ii. If z.value < temp.value, temp = temp.left
iii. Else, temp = temp.right
6. If z.value < y.value, y.left = z
7. Else, y.right = z
8. Stop
Insert the following elements in to a BST which is initially empty. 10, 6, 15, 2, 8, 20, 4
Searching for an Element in a BST
● The search for an element, say k, begins at the root.
● If the value at root is equal to k, the process stops.
● If the value at the root node is less than k, then the process continues with the
right subtree of root.
● If the value at the root node is greater than k, then the process continues with
the left subtree of root.
● To search for the key 13 in the tree, we
follow the path 15 -> 6 -> 7 -> 13 from
the root
● The search starts at root. 15 is greater
than 13. So, the left subtree is selected.
● 6 is less than 13, so the right subtree of
6 is selected, so on and so forth.
● The time taken for tree search is O(h)
where h is the height of the tree.
Deletion from Binary Search Trees
There are 4 different cases of deletion (a, b, c and d) and the cases should be
tested in the order (i.e from ‘a’ to ‘d’).
case (a): Let z be the node to be deleted
● If z has no left child, then we replace z by its right child
● When z’s right child is NIL , this case deals with the situation in which z has
no children
● When z’s right child is non- NIL, this case handles the situation in which z
has just one child, which is its right child.
● In the following figure, deletion
of nodes 10, 40, 22, 12, 28, 42
& 49 fall into case (a) of
deletion (i.e. nodes with no left
child).
● 22, 12, 28, 42 and 49 are
special cases of case (a) where
the right child also is NIL
● Case (a) example: Delete 40
from the tree.
● Case (a) says, node to be
deleted has no left child.
○ 40 is a node with no left child.
● Case (a) Example 2:
○ Deletion of 12 from the tree.
○ Here 12 is a node with no left child as in
the previous example.
○ But, unlike in the previous example, 12
has no right child either. This is a special
case of case(a)
case (b)
● If z has just one child, which is its left child, then we replace by its left child
● In the following figure deletion of
nodes 20 and 30 belong to case(b)
(i.e nodes with no right child).
Case c&d:
● Otherwise, z has both a left and a right child. We find z’s successor y, which is the smallest
element in z ’s right subtree (Remember, the smallest element in the right subtree will
not have a left child of its own. A node with a left child will not be the smallest
element as the left child will be smaller than the node itself.).
● We want to splice y out of its current location and have it replace z in the tree.
Case c:
○ If y is z’s right child, then we replace z by y, leaving y’s right child alone.
Case d:
○ Otherwise, y lies within z ’s right subtree but is not z’s right child. In this case, we first replace y by its own
right child, and then we replace z by y.
In the figure deletion of nodes 20 and 36
belongs to case(c) (i.e. smallest element
on the right subtree of 36 (40) is the direct
descendant of 36).
● In the figure deletion of node 36 belongs to
case(d)
● The smallest element on the right subtree of 36
is 38, which is not a direct descendant of 36. So
this is case(d)
● First right child (if any) of the replacing element
is made the left child of its parent (step 1).
● Then the node to be deleted is replaced (step 2)
BST/Binary Tree Traversals
● Traversal is a process of visiting all the nodes of a tree (optionally printing
their values)
● The traversal process starts at root
● Three types
○ In-order
○ Pre-order &
○ Post-order
In-order Traversal
● Before a node is visited, its left subtree is visited. Then
the node itself and the right subtree are visited
● So, the order of traversal is left child, node and right
child.
○ This order is recursively applied on every node being visited
● In-order traversal of the given tree will visit the nodes as
D→B→E→A→F→C→G
Pre-order Traversal
● The order of traversal is node, left child and
right child.
○ This order is recursively applied on every node being
visited
● In-order traversal of the given tree will visit the
nodes as
A→B→D→E→C→F→G
Post-order Traversal
● The order of traversal is left child, right child and
node itself.
○ This order is recursively applied on every node being
visited
● In-order traversal of the given tree will visit the
nodes as
D → E→ B → F → G → C → A
AVL Trees
● Adelson-Velskii and Landis tree
● AVL trees are binary search trees
● Height balancing binary search tree
● For every node in an AVL tree, the height of the left and right subtrees can
differ by at most 1
Why is Height Balancing Required?
● Consider inserting the following elements into a binary search tree which is
initially empty. 2, 5, 7, 8, 10, 12, 15
● The BST after insertion is
● Searching for an element in the above BST will cost O(n) on average and
worst cases which is same as that of searching in a list.
● That is, the BST does not have the expected search performance which is
O(log n)
● The reason is that it is skewed
● The solution to this problem is to keep the tree balanced
● In AVL trees, the balancing criterion is: for every node in an AVL tree, the
height of the left and right subtrees can differ by at most 1
● A parameter called balance factor has been defined
○ Balance factor of a node = height of its left subtree - height of its right subtree
○ The acceptable balance factor values are 0, 1 and -1
● In AVL trees, after every insertion, it is ensured that the balance factor of
every node remains within the allowed range (0, 1, or -1)
● If it is altered, adjustments (rotations) are made to keep the tree balanced
● The left tree is an AVL tree,
whereas the right one is not
● For the right tree, the
balance factor of node 7 is 2
which not an allowed value
Insertion in AVL Trees
● In AVL trees, after every insertion, it is ensured that the balance factor of every node
remains within the allowed range (0, 1, or -1)
● In insertion the first step is to insert a value following the rules of BST insertion
● After an insertion, only nodes that are on the path from the insertion point to the root
might have their balance altered because only those nodes have their subtrees
altered.
● As we follow the path up to the root and update the balancing information, we may
find a node whose new balance violates the AVL condition.
● We rebalance the tree at the first (along the path from newly inserted node to root)
such node, which guarantees that the entire tree satisfies the AVL property.
● Let α be the node whose balance is altered after an insertion
● So, α must be rebalanced
● There are 4 cases of rebalancing
1. An insertion into the left subtree of the left child of α
2. An insertion into the right subtree of the left child of α
3. An insertion into the left subtree of the right child of α
4. An insertion into the right subtree of the right child of α
● Cases 1 and 4 are mirror image symmetries. Similarly, cases 2 & 3 are also mirror
image symmetries
● Single rotation is done in cases 1&4; double rotation is required for cases 2&3
Single Rotation: Case (1)
● An insertion into the left subtree of
the left child of α (Here α is node 8)
Single Rotation: Case (4)
● An insertion into the right subtree
of the right child of α (Here α is
node 3)
Single Rotation: Case (4)
Example 2
● An insertion into the right subtree of the
right child of α (Here α is node 2)
● Unlike the previous example, here α has
a left child
● Similar example for case(1) is left as an
exercise.
Double Rotation: Case (3)
● An insertion into the left subtree of
the right child of α (Here α is node
7)
● k1, k2 & k3 are just names given to
distinguish between the nodes
Double Rotation: Case (3)
Example 2
● An insertion into the left subtree of
the right child of α (Here α is node 6)
● Unlike the previous example, here k1,
k2 and k3 have children
● k1, k2 & k3 are just names given to
distinguish between the nodes
Double Rotation: Case (2)
● An insertion into the right subtree of the
left child of α (Here α is node 10)
● Examples of case 2 with k1, k2 and k3
having children is left as an exercise.
B-Trees
● M-way search trees
● Every node in a B-Tree contains at most m children (i.e m-1 elements).
● Every node in a B-Tree except the root node should contain at least ⌊(m-1)/2⌋
values.
● The root node can have a minimum of 2 children (i.e 1 element)
● All leaf nodes must be at the same level
Insertion
● In our example assume that m=6
● That is, maximum number of keys per node = m-1 = 6-1 = 5
● Minimum number of keys required in nodes other than the root = ⌊(m-1)/2⌋ =
⌊5/2⌋ = 2
● Insertions are performed on the tree which is shown below
● (b) The result of inserting B into the initial tree; this is a simple insertion into a
leaf node
● The insertion does not cause the storage limit of the node to exceed
● (c) The result of inserting Q into the previous tree
○ Q goes to The node RSTUV which is already full
○ So, the node RSTUV splits into two nodes containing RS and UV
○ The key T moves up to the root, and
○ Q is inserted in the leftmost of the two halves (the RS node)
● (d) The result of inserting L into the previous tree.
○ L goes into the node JK
○ The parent of JK is GMPTX which is already full
○ If we insert to a node whose parent is at full capacity, the parent should be split thereby
updating the higher levels of the tree
○ The root splits right away, since it is full, and the B-tree grows in height by one.
○ Then L is inserted into the leaf containing JK.
● (e) The result of inserting F into the previous tree.
○ F goes into ABCDE which is already full
○ The node ABCDE splits into AB and DE
○ C is moved up
○ F is inserted into the rightmost of the two halves (the DE node)
Delete F
● Case 1: Deletion from a leaf node.
○ Even after the deletion there will be adequate number of elements in the node (i.e the node
will have greater than or equal to the minimum number of required elements)
○ Simply remove the element from the leaf, as shown in the example
Delete M
● Case (2): Deletion from internal
nodes
○ (2a): If there are more than the minimum
number of required elements in the node
pointed by the pointer to the left of the
element to be deleted, move the right
most element of that node to the position
of the element being deleted
(Replacement from left child)
○ (2b): If replacement from the left child (case a) is impossible due to lack of elements and if there are more than the
minimum number of required elements in the node pointed by the pointer to the right of the element to be deleted,
move the leftmost element of that node to the position of the element being deleted (Replacement from right child)
○ Moving up an element will result in recursive deletions which may fall into any of the cases. For example, moving L up
when deleting M will result in the case of deletion of L from the leaf node.
Delete G
● Case (2): Deletion from internal nodes
○ (2c): The nodes pointed by the pointers at the left and right of the element to be deleted have
the minimum number of required elements (i.e replacement from the children is not possible).
Then merge the children.
Delete D
● Case (3): Deletion from a leaf node
● (3a):The node is having minimum number of
elements required. i.e deleting an element
will result in deficiency of elements
○ This is the only difference from case 1. In case
1, deleting the element from a leaf node will
not result in deficiency)
○ Solution: Rotation involving parent element
and the right most element in the left sibling.
Or, rotation involving parent element and the
leftmost element in the right sibling.
○ If rotation is not possible do merging
● (3b): Parent of the node from which deletion is performed has minimum number of elements. If sibling of parent(left/right) has more than
the minimum number of required elements, do rotation with the sibling (parent level rotation). If rotation is not possible, then merge parent
with its immediate sibling. In this example merging is done as per case 3b, after which the deletion becomes case 1 (simple deletion from
leaf node).
Delete B
● Deletion of B will result in
deficiency of elements.
● The parent and right sibling have
more than minimum number of
required elements
● This belongs to case 3a (refer
previous slide for case 3a)
● So, rotation with parent and
leftmost element of right sibling is
done.