Professional Documents
Culture Documents
Week 5
Week 5
OVERVIEW
Is not the main objective to present you with the theorems and proofs on data structures
and algorithms. We have followed a pattern of improving the problem solutions with
different complexities (for each problem, you will find multiple solutions with different,
and reduced, complexities). Basically, it’s an enumeration of possible solutions. With
this approach, even if you get a new question, it will show you a way to think about the
possible solutions. You will find these topics useful for interview preparation, competitive
exams preparation, and campus interview preparations.
LEARNING OBJECTIVES
1. Describe the Tree ADT;
2. Design algorithms that operate on Tree;
3. Compare the efficiencies of algorithms that implement list operations using binary
trees;
4. Discuss some problems and solutions when implementing binary tree in ;
The root of a tree is the node with no parents. There can be at most one root
node in a tree (node A in the above example).
An edge refers to the link from parent to child (all links in the figure).
A node with no children is called leaf node (E,J,K,H and I).
Children of same parent are called siblings (B,C,D are siblings of A, and E,F are
the
siblings of B).
A node p is an ancestor of node q if there exists a path from root to q and p
appears on the path. The node q is called a descendant of p. For example, A,C
and G are the ancestors of if.
The set of all nodes at a given depth is called the level of the tree (B, C and D
are the same level). The root node is at level zero.
Binary Trees
A tree is called binary tree if each node has zero child, one child or two children. Empty
tree is also a valid binary tree. We can visualize a binary tree as consisting of a root and
two disjoint binary trees, called the left and right subtrees of the root.
Generic Binary Tree
Complete Binary Tree: Before defining the complete binary tree, let us assume that the
height of the binary tree is h. In complete binary trees, if we give numbering for the
nodes by starting at the root (let us say the root node has 1) then we get a complete
sequence from 1 to the number of nodes in the tree. While traversing we should give
numbering for NULL pointers also. A binary tree is called complete binary tree if all leaf
nodes are at height h or h – 1 and also without any missing number in the sequence.
The number of nodes n in a full binary tree is 2 h+1 – 1. Since, there are h levels
we need to add all nodes at each level [2 0 + 2 1+ 2 2 + ··· + 2 h = 2 h+1 – 1].
The number of nodes n in a complete binary tree is between 2 h (minimum) and
2 h+1 – 1 (maximum). For more information on this, refer to Priority Queues
chapter. • The number of leaf nodes in a full binary tree is 2 h . • The number of
NULL links (wasted pointers) in a complete binary tree of n nodes is n + 1.
below:
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.
Operations on Binary Trees Basic Operations
Inserting an element into a tree
Deleting an element from a tree
Searching for an element
Traversing the tree Auxiliary Operations
Finding the size of the tree
Finding the height of the tree
Finding the level which has maximum sum
Finding the least common ancestor (LCA) for a given pair of nodes, and many
more.
InOrder Traversal
In Inorder Traversal the root is visited between the subtrees. Inorder traversal is defined
as follows:
Traverse the left subtree in Inorder.
Visit the root.
raverse the right subtree in Inorder.
The nodes of tree would be visited in the order: 4 2 5 1 6 3 7
PostOrder Traversal
In postorder traversal, the root is visited after both subtrees. Postorder traversal is
defined as follows:
Traverse the left subtree in Postorder.
Traverse the right subtree in Postorder.
Visit the root.
The nodes of the tree would be visited in the order: 4 5 2 6 7 3 1