2. A tree is a finite sets of one or more nodes such that:
There is a special node called the root of the tree.
And its remaining nodes are partitioned into number of disjoint
sets, where each set is a tree.
A tree is a non-linear data structure in which items are arranged
in a sorted sequence. It is used to represent hierarchical
relationship existing amongst nodes. Figure 1 shows an
example of such tree.
3. A binary tree T is defined as a finite set of elements, called nodes,
such that:
T is empty (called the null tree or empty tree), or
T contains a distinguished node R, called the root of T, and the
remaining nodes of T from two disjoint binary trees T1 and T2
which are called left sub-tree and the right-subtree.
Binary Trees
6. Level: Level is the rank of the hierarchy and root node is
termed as in level 0. If a node is at level l then its child is at
level l + 1 and parent is at level l – 1. This is true for all nodes
except the root node. Level of the root node is zero. In Figure
5, levels of various nodes are shown.
Height: Maximum number of nodes that is possible in a path
starting from root node to a leaf node is called the height or
depth of tree. For example, in the Figure 5, the longest path is
A-C-H-K and hence the height of this tree is 4. It can be easily
obtained that h = lmax + 1, where h is the height and lmax is
the maximum level of the tree and the maximum number of
nodes possible in binary tree of height h is nmax = 2h
– 1.
Degree: Maximum number of children that is possible for a
node is known as the degree of a node. For example, the
degree of each node of the tree as shown in the Figure 5 is 2.
Sibling: The nodes which have the same parent are called
siblings. For example, in the Figure 5, D and E are siblings
7. Example 1: Consider any algebraic expression E involving only binary operations,
such as
E= (a – b) / ((c*d) + e)
8. A binary tree is said to be complete binary tree, if all its levels,
except possibly the last, have the maximum number of
possible nodes and all the nodes at the last level appear as far
as left as possible.
Complete Binary Trees
9. A binary tree T is said to be a 2-tree or an extended binary tree if each
node N has either 0 or 2 children. In such a case, the nodes with 2 children
are called internal nodes, and the nodes with 0 children are called external
nodes. The nodes are usually shown in diagrams by using circles for
internal nodes and squares for external nodes.
Extended Binary Trees
10. A binary tree represent a hierarchical relationship between a
parent node and child nodes. There are two methods used for
representing this structure.
◦ By using array
◦ By using linked list
Representing Binary Trees in Memory
11. Consider a binary tree T as shown in the Figure 9(a), the corresponding
array/sequential representation is shown in Figure 9(b). We require 7
locations in the array TREE even though binary tree T has only 6 nodes.
Sequential representation is usually inefficient unless, the binary tree T is
complete or nearly complete. For example, if the tree T consists of 11
nodes and depth 5, which means it would require an array with
approximately 26
– 1 = 63 elements.
Array/Sequential Representation of
Binary Trees
13. Tree traversal is one of the most common operations performed on tree data
structure. It is a way in which each node in the tree is visited exactly once. There
are three ways of traversing a binary tree T with root R. These are called preorder,
inorder and postorder, are as follows:
a) Preorder (Root – Left Sub-tree – Right Sub-tree)
◦ Process/Visit the root R.
◦ Traverse the left sub-tree of R in preorder.
◦ Traverse the right sub-tree of R in preorder.
b) Inorder (Left Sub-tree – Root – Right Sub-tree)
Traverse the left sub-tree of R in inorder.
Process/Visit the root R.
Traverse the right sub-tree of R in inorder.
c) Postorder (Left Sub-tree –Right Sub-tree – Root)
Traverse the left subtree of R in postorder.
Traverse the right subtree of R in postorder.
Process/Visit the root R
Traversing Binary Trees
14. A) The preorder traversal of T processes A, traverses left sub-tree
and traverses right sub-tree. The preorder traversal of left sub-tree
processes the root B and the preorder traversal of right sub-tree
processes the root C. Hence ABC is the preorder traversal of T.
B) The inorder traversal of T traverses left sub-tree, processes A
and traverses right sub-tree. The inorder traversal of left sub-tree
processes B and the inorder traversal of right sub-tree processes C.
Hence BAC is the inorder traversal of T.
C) The postorder traversal of T traverses left sub-tree, traverses
right sub-tree, and processes A. The postorder traversal of left sub-
tree processes B, and the postorder traversal of right sub-tree
processes C. Hence BCA is the postorder traversal of T.
15. Let E denote the following algebraic expression:
[A + (B – C)] * [(D – E) / ( F + G – H)]
16. a) Preorder Traversal
Step 1: Proceed/Traverse down the left-most path
rooted at PTR, processing each node N on the path
and pushing each right child RIGHTC(N), if any onto
STACK, The traversing ends after a node N with no
left child LEFTC(N) is processed. PTR is updated
using the assignment PTR := LEFTC|PTR], and the
traversing stops when LEFTC[PTR] = NULL.
Step 2: Pop and assign to PTR the top element on
STACK. If .PTR ≠ NULL, then return to Step 1;
otherwise Exit;
Traversal Algorithms Using Stacks
18. Inorder Traversal
Step 1: Proceed down the left-most path rooted at
PTR, pushing each node N onto STACK and stopping
when a node N with no left child is pushed onto
STACK.
Step 2: Pop and process the nodes on STACK. If
NULL is popped, then Exit. If a node N with a right
child RIGHTC(N) is processed, set PTR = RIGHTC(N)
and return to Step 1.
20. Step 1: Proceed down the left-most path rooted at
PTR. At each node N of the path, push N onto STACK
and, if N has a right child RIGHTC(N), push –
RIGHTC(N) onto STACK.
Step 2: Pop and process positive nodes on STACK. If
NULL is popped, then Exit. If a negative node is
popped, i.e. If PTR = –N for some node N, then set
PTR = N and return to Step 1.
Postorder Traversal
22. Suppose a binary tree T is maintained in memory by means of a linked list
representation. Sometimes an extra node, called a header node, is added at
the beginning of T. When this extra node is used, the tree pointer variable,
which we will call HEAD instead of ROOT, will point to the header node,
and the left pointer of the header node will point to the root of T. Figure
18(b) shows a linked representation with a header node of binary tree T
shown in Figure 18(a).
Header Nodes
23. Consider the linked list representation of a binary tree T
shown in the Figure 18(b). Half of the entries in the pointer
fields LEFTC and RIGHTC will contain null elements. This
space may be more efficiently used by replacing the null
entries by some other type of information.
We will replace certain null entries by special pointers which
point to nodes higher in the tree. These special pointers are
called threads, and binary trees with such pointers are called
threaded trees
Threads
24. There are many ways to thread a binary tree T, but each
threading will correspond to a particu
lar traversal of T. The
threading will correspond to the inorder traversal of T. There
are two types of threading:
One way threading
Two way threading
Types of Threading
26. If the value at node N is greater than every value in the
left subtree of node N and is less than every value in
the right subtree of node N, then the binary tree is
termed binary search tree.
Binary Search Trees
27. Example 12: Consider the binary search tree T shown
in the Figure 22(a). Suppose ITEM = 20 is given.
Example 13: Consider the following six numbers, which are
inserted in order, into an empty binary search tree:
40, 60, 50, 33, 55, 11
28. Case 1: Node N has no children. Then N is deleted
from T by simply replacing the pointer of N in the
parent node PAR(N) by the NULL value.
Case 2: Node N has exactly one child. Then N is
deleted from T by simply replacing the pointer of N in
PAR(N) by the location of the only one child of N.
Case 3: Node N has two children. Then N is deleted
from T by first deleting SUCC(N) from T (by using
Case 1 or Case 2) and then replacing node N in T by
the node SUCC(N).
Deleting in a Binary Search
Tree
31. Suppose H is a complete binary tree with n elements. Then it will be called
as heap tree if it satisfies the following properties.
1. For each node N in H, the value at N is greater than or equal to the value
at each of the children of N.
2. N has the value which is greater than or equal to the value of every
successor of N.
Such a heap tree is called as max heap. Any node N has value less than or
equal to the value of any of the successors of N is called as min heap. In a
max heap the root node contains the largest data value whereas in a min
heap it contains the smallest data value.
Heap Sort
35. An extended binary tree or a 2-tree is a binary tree T in
which each node has either 0 or 2 children. The nodes
with zero children are called external nodes, and the
nodes with two children are called internal nodes.
Path Length
36. A solution to the problem of finding a binary tree with
minimum weighted external path length was given by D.
Huffman. The tree that can be constructed with the Huffman's
algorithm is guaranteed to be a minimum weighted binary tree
such trees and also called as Huffman tree.
Huffman Algorithm