You are on page 1of 25

Tree

CHAPTER 5 LEC 2
Tree
 Trees are well suited to represent the hierarchical structure
of a certain domain
 The search process is much faster using trees instead of
linked list, but this doesn’t hold always.
Balancing A Tree
 A binary tree is height-balanced or simply balanced if the
difference in height of both subtrees of any node in the
tree is either zero or one.
Techniques to balance a binary tree
 constantly restructuring the tree when elements arrive and
lead to an unbalanced tree
 reordering the data themselves and then building a tree
Reordering the data themselves
Algorithm
1. Store all the data in an array and sort
2. Designate for the root the middle element in the array.
3. The left child of the root is taken from the middle of the
first subarray
4. The right child of the root is taken from the middle of the
second subarray
5. Repeat 3 and 4 until end
Balancing using reordering (example)
Balancing using reordering (code)

Drawbacks
1. All data must be put in an array before the tree can be
created
2. Require a sorting algorithm
Balancing - The DSW Algorithm
 The building block for tree transformations in this
algorithm is the rotation.
Balancing - The DSW Algorithm
Step 1
 transfigures an arbitrary binary search tree into a linked
list like tree called a backbone or vine
Balancing - The DSW Algorithm
Step 2
the backbone is transformed into a tree
Balancing - The DSW Algorithm
Complexity analysis
Step 1
 In the worst case, the while loop is executed 2n – 1 times
with n – 1 rotations performed the run time is O(n).
Step 2

O(n)
 Total O(n)
AVL Trees
 Tree rebalancing, however, can be performed locally if
only a portion of the tree is affected when changes are
required after an element is inserted into or deleted from
the tree.
 An AVL tree (originally called an admissible tree) is one
in which the height of the left and right subtrees of every
node differ by at most one.
AVL Trees
 Numbers in the nodes indicate the balance factors that are
the differences between the heights of the left and right
subtrees.
 For an AVL tree, all balance factors should be +1, 0, or –1.
 If the balance factor of any node in an AVL tree becomes
less than –1 or greater than 1, the tree has to be balanced.
 An AVL tree can become out of balance in four situations,
but only two of them need to be analyzed; the remaining
two are symmetrical.
Case 1 - inserting a node in the
right subtree of the right child

Solution
 can be easily rectified by rotating node Q about its parent P

Example
Insert and balance { 4,3,8,5,12,13}/{ 4,3,8,5,12,9}
Case -2- inserting a node in the
left subtree of the right child,.
 is more complex

Solution
Need double rotation
Example
Insert and balance {4,3,8,5,12,6}
An example of inserting a new node (b) in an AVL tree (a),
which requires one rotation (c) to restore the height balance.
In an AVL tree (a) a new node is inserted (b) requiring no
height adjustments.
Heap
max heap has the following two properties:
 The value of each node is greater than or equal to the
values stored in each of its children.
 The tree is perfectly balanced, and the leaves in the last
level are all in the leftmost positions.
If “greater” in the first property is replaced with “less,” then
the definition specifies a min heap.
Heap - example
Select heaps
Heaps as Priority Queues
 two procedures have to be implemented to enqueue and
dequeue elements on a priority queue.

enqueue
dequeuing
Organizing Arrays as Heaps
Organizing Arrays as Heaps
 For a node stored at index i,
It’s parent is at: (𝑖−1)/2
It’s left child is at: 2𝑖+1
It’s right child is at: 2𝑖+2

 Enables fast access of elements

You might also like