TREES
The notes only cover the basic definitions.
A tree is a special case of a digraph used to express hierarchical relationships or multiway decisions in
computing. For example game tree, company structure, family tree, language tree, expression tree,
sorting and searching trees etc.
Definitions
The common characterisitics of all these trees is that there is a single vertex (the root) that can be
identified as the top of the tree and from the root to any other vertex in the tree there is exactly one
path.
In a general tree there is no restriction about the out-degree and the vertex set may be infinite.
In most application of trees vertex set is finite and the vertices at the bottom have out-degree=0. These
vertices are called leaves or terminals.
A vertex which is neither root nor leaf is called a nonterminal vertex (node).
Every vertex at the destination end of an edge for which the root is the source is itself the root of
another smaller tree called a sub-tree.
The depth of a tree is the length of the longest path from the root.
CSE211 Lecture Notes – 2004/2005-II
The destination vertices are called children and the root of it is called parent. Children of the same
parent are called siblings.
A node n2 is a left descendant of node n1 if n2 is either the left child of n1 or a descendant of the left
child of n1. A right descendant may be similarly defined.
Binary Trees
A binary tree is a tree all of whose vertices have out-degree = 2. The subtrees of a binary tree are
ordered in the sense that there is a left child and a right child. If a vertex has only one child it should
Strictly Binary Tree: T is a strictly binary tree iff each of its vertices has out-degree = 0 or 2.
Complete Binary Tree: T is a complete binary tree of depth K iff each vertex of level K is a leaf and
each vertex of level less than K has non empty left and right children.
A complete binary tree depth K always has 2 k+1 -1 vertices.
Depth 0 -- single vertex
Depth 1 -- 3 vertices
Depth 2 -- 7 vertices
CSE211 Lecture Notes – 2004/2005-II
Almost complete: All K-1 level vertices are complete but level K vertices are missing (useful in some
sorting algorithms such as heap sort).
Balanced: T is balanced iff for each vertex t in T, the depth of t's left and right subtrees differ by at
most one.
1. A binary tree consisting of a single vertex is balanced.
2. A vertex with a single subtree is balanced iff that subtree is a leaf.
3. A binary tree is balanced iff, its leaf and right sub trees are balanced and their depth differ by only
one.
An almost complete binary tree can be represented as an array. 1st element of the array is the root, 2nd
and 3rd elements are children of the root , the nodes are stored level by level.
W R T M E G S A F D C E
W
R T
M E G S
A F D C E
CSE211 Lecture Notes – 2004/2005-II
Lemma: If a complete binary tree with n nodes (depth log 2 n + 1) is represented sequentially. Then
any node with index i 1≤ i ≤ n we have
1. parent(i) is at i/2, if i ≠ 1,
( when i=1, the root has no parent. )
2. leftChild(i) is at 2i, if 2i ≤ n,
(no left child, if 2i > n.)
3. rightChild(i) is at 2i+1, if 2i+1 ≤ n,
(no right child, if 2 i+1 > n.)
Since binary tree is a digraph we could implement it using one of the graph representations. However
since binary tree has right and left subtrees we can create a specialized structure.
Node
LCHILD DATA RCHILD
Data
lchild rchild
There are a number of primitive operations that can be applied to a binary tree. For example : Assume
that p is a pointer to a the node n1 of a binary tree, the following functions may be defined:
n info(p): returns the contents (data field) of n1.
n leftChild(p), rightChild(p) : return pointers to the left child and right child of n1 respectively.
n hasLeft(p), hasRight(p): return the value true if the node has leftchild or right child respectively.
Several other operations can be defined on a tree depend ing on the application requirements.
Let's say tp is a pointer to the root node of a tree (pointer type is tree node type) and we are searching
for number n in this tree for a duplicate, if there is not a duplicate then place it in the tree.
p=tp;
while ( n != info(tp) && p != null) {
if ( n < info(p) ) p = left(p);
else p = right(p);
}
HEAPSORT
Binary heap is a completely balanced binary tree satisfying the heap property.
Heap property: Given a binary heap A, A[PARENT(i)]≥A [i]
1
2 26 3
24 11
4
5 6 7
7 6
8 4
8
9 10
1 3 5 1 2 3 4 5 6 7 8 9 10
26 24 11 7 6 8 4 1 3 5
HEAPIFY(A, i)
1. L ← LEFT(i)
2. r ← RIGHT(i)
3. if l ≤heap-size[A] and A[l]>A[i]
4. then largest ← l
5. else largest ← r
6. if r ≤heap-size[A] and A[r]>A[largest]
7. then largest ← r
8. if largest≠ i
9. then exchange A[i]]↔ A[largest]
10. HEAPIFY(A, largest)
2 26 3
i
3 11
4
5 6 7
24 6
8 4
1
8
9 10 26
1 7 5 2 3
24 11
4
i 5 6 7
3 6
8 4
8
9 10
1 7 5
2 26 3
24 11
4
5 6 7
7 6
8 4
8
9 10
1 3 5
BUILD-HEAP(A)
1. heap-size[A] ←length[A]
2. for i ← length[A]/2 downto 1
3. do HEAPIFY(A, i )
Running time of BUILD-HEAP is O(nlgn) at first sight, but it is O(n). (studeied in CSE311)
CSE211 Lecture Notes – 2004/2005-II
h=0
Example Run: 1
2 3 3
1
5 4
4 2 3 3
5 6 7
1 i 26 5 4
8 11
4
8 5 6 7
9 10 i
1 26
24 7 6 8 11
8
9 10
1 24 7 6
2 3 3
5 i 4 1
4 3
5 6 7 2 3
24 26 i
8 11 5 11
8 4
9 10 5 6 7
1 7 6 24 26
8 4
8
9 10
1
i 1 7 6
2 3 3
26 11
1
4
5 6 7
2 26 3
24 6
8 4
24 11
8
9 10 4
1 7 5 5 6 7
7 6
8 4
8
9 10
1 3 5
HEAPSORT(A)
1. BUILD-HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1]↔ A [i]
4. heap-size[A] ← heap-size[A] −1
5. HEAPIFY(A, 1)
CSE211 Lecture Notes – 2004/2005-II
7 1 2 2
2 3 1 4 6
6 4
4 1
5 6 7
5 1 6
3 8 2 3
8 5 4
9 10
1 2 2 4
1 4 6 5 6 7
3 1
7 8
1 8
9 10
2 1 3 1 2 2
1 4 6
3 4
4
5 6 7
5 6 7 8
8
9 10
1 2 2
1 4 6
1 2 3 4 5 6 7 8 9 10
1 3 4 5 6 7 8 11 24 26
CSE211 Lecture Notes – 2004/2005-II
BINARY TREE TRAVERSAL: Many applications require traversing a tree so that all the nodes are
visited in a certain order. Traversal algorithms are useful in dealing with binary trees. Three different
forms of traversal is possible: preorder, postorder, inorder traversal.
4
5 6
7 8 9
}
POSTORDER TRAVERSAL: 7 4 5 2 8 9 6 3 1
Many algorithms that use binary trees proceed in two phases. The first phase builds a binary tree, and
the second phase traverses the tree.
Consider sorting algorithm: given a list of numbers in an input file, we wish to print them in ascending
order. As we read the numbers they can be inserted into a binary tree, however unlike the algorithm
used to find duplicates, duplicate values are also placed in the tree. When a number is compared with
the contents of a node in the tree, a left branch is taken if the number is smaller than the contents of the
node, and a right branch if it is greater or equal to the contents of the node.
Binary search trees can be used in the implementation of efficient insertions and deletions in tables.
Operations on a BST:
1. Traversing a BST
2. Inserting a record in a BST
3. Finding a record in a BST
4. Deleting a record from a BST
1. If a binary search tree is traversed in inorder and the contents of each node are printed as the node is
visited , the numbers are printed in ascending order.
• Assuming that tree is not empty, we first test the new key against the root key: if it is less than we
insert it in the left subtree, if it is equal to or greater insert it in the right subtree.
• After several recursive calls we will reach a point where the subtree into which the key to be
inserted is empty, at this point we create a new node and link it to the appropriate pointer in the
parent node.
3. Finding a node requires traversing the tree until yo u reach the searched node
4. Deletion algorithm:
Successor of 2: 4
7 1
Predecessor of 6: 5
2
4 8
6
4 7
4 5
Threading: As we build a BST, we fill empty pointer fields with pointers that help us move up the
tree as well as down. Moving up helps us to find the successor of a node during a traverse left-node-
right operation.
CSE211 Lecture Notes – 2004/2005-II
If the threads facilitate inorder traversal then the tree is called right- in threaded.
Where are the threads generated? If a node has a right child then its LNR successor is below it
somewhere in the right subtree. Otherwise its LNR successor is above in the tree. Since a node needs
a thread only if it has no right child, common practice is to store the thread in the right child field of
the node. A flag is also needed to indicate if it is a thread or not.
thread
G
D
EXPRESSION TREES
Another application of binary trees in interpreters and compilers. The statement is converted into tree.
For simplicity we will see expression trees. Expression tree is a tree that has operator as its root and
identifiers or constants as its leaves.
A node representing an operator is a nonleaf whereas a node representing an operand is a leaf.
Traversing an expression three in preorder gives its prefix form, traversing it in inorder gives infix
form and traversing in postorder produces postfix form.
/ *
+ sqrt
8 4
x z 3
AVL TREES
n BSTs carry a high risk of becoming unbalanced, resulting in expensive search and update
operations.
n Idea: Maintain a binary search tree that is almost completely balanced
n AVL Trees are BSTs satisfying AVL tree balance:
n For every node, the heights of its left and right subtrees differ at most 1.
n Depth of an AVL tree with N nodes is at most O(lgN )
Insertion
n After insertion, AVL tree might become unbalanced by a difference of at most 2 in the
subtrees.
n For the bottommost unbalanced node, S, there are 4 cases:
1. The extra node is the left child of the left child of S.
2. The extra node is the right child of the left child of S.
CSE211 Lecture Notes – 2004/2005-II
unbalaced
37 37
nodes
24 42 24 42
7 32 40 42 7 32 40 42
2 120 2 120
S C P
A
B B C
Double Rotation
G
P S
D
A S P G
C A C
B B D