Binary Search Trees, Selection Trees Forests Representation of Disjoint Sets Counting Binary Trees
Binary Search Trees, Selection Trees Forests Representation of Disjoint Sets Counting Binary Trees
Fig(a) is not a Binary search tree fig(b) and (c) are Binary search trees
Creating a Binary Search Tree
• Now, let's see the creation of binary search tree using an example.
• Suppose the elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
• First, we have to insert 45 into the tree as the root of the tree.
• Then, read the next element; if it is smaller than the root node, insert it as
the root of the left subtree, and move to the next element.
• Otherwise, if the element is larger than the root node, then insert it as the
root of the right subtree.
• Step 1 - Insert 45
Step 2 - Insert 15.
• As 15 is smaller than 45, so insert it as the root node of the left subtree.
• Next, a ‘+’ is read, so two pointers to trees are popped, a new tree is
formed, and a pointer onto the stack.
• Next, c, d, and e are read, and for each one-node tree is created and a
pointer to the corresponding tree is pushed onto the stack.
• Finally, the last symbol is read, two trees are merged, and a pointer to the
final tree is left on the stack.
Construct expression tree for
a. 6 3 2 – 5 * + 2 $ 3 +
b. 3 4 + 5 6 ^ 7 / *
Building binary tree from traversal pair
• Sometimes it is required to construct a binary tree if its traversals are
known.
• From a single traversal it is not possible to construct unique binary tree.
• However any of the two traversals are given then the corresponding tree
can be drawn uniquely:
• Inorder and preorder,
• Inorder and postorder,
• Inorder and level order
• The basic principle for formulation is as follows:
– If the preorder traversal is given, then the first node is the root node.
– If the postorder traversal is given then the last node is the root node.
– Once the root node is identified, all the nodes in the left sub- trees
and right sub-trees of the root node can be identified using inorder.
– Same technique can be applied repeatedly to form sub-trees.
Example:
(A) Construct a binary tree from a given Preorder and Inorder
sequence
Inorder : 4, 2, 7, 5, 1, 8, 6, 3
Preorder: 1, 2, 4, 5, 7, 3, 6, 8
Node structure in C:
typedef enum {not, and, or, true, false} logical;
typedef struct node *treePointer;
typedef struct
{
treePointer leftChild;
logical data;
short int value;
treePointer rightChild;
}node;
• Assume that node->data contains the current value of the variable
represented at the leaf node. In the above tree, data field in x1, x2 and x3
contains either TRUE or FALSE.
• The first version of Satisfiability algorithm
The C function that evaluates the tree is modified version of postorder
traversal, given below:
void postordereval(treepointer node)
{
if( node)
{
postordereval(node->leftchild);
postordereval(node->rightchild);
switch( node->data) {
case not : node->value = ! node->rightchild->value;
break;
case and : node->value = node->rightchild->value &&
node->leftchild->value ; break;
case or : node->value = node->rightchild->value ||
node->leftchild->value ; break;
case true : node->value = TRUE; break;
case false : node->value = FALSE; break;
} } }
Selection Tree
• The Tournament tree is a complete binary tree with n external nodes and n
– 1 internal nodes.
• The external nodes represent the players, and the internal nodes are
representing the winner of the match between the two players. This tree is
also known as Selection tree.
• There are two types of Tournament Trees −
• Winner Tree
• Looser Tree
Winner Tree:
• Winner tree is a complete binary tree, in which each node is representing
the smaller or greater of its two children, is called winner tree.
• The root is holding the smallest or greatest node of the tree.
• The winner of the tournament tree is the smallest or greatest n key in all the
sequences.
• It is easy to see that winner tree can be formed in O(log n) time.
• Example − Suppose there are some keys, 3, 5, 6, 7, 20, 8, 2, 9
Winner Tree
• Suppose we have k ordered sequences, called runs, are to be merged into
single ordered sequence.
fig.5.32
• The record pointed to by the root has the smallest key and so may be output.
• Now, the next record from run 4 enters the winner tree. It has a key value
15.
• To restructure the tree, the tournament has to be replayed only along the
path from node 11 to the root.
Looser Tree
• Looser Trees are complete binary tree for n players where n external nodes
and n – 1 internal nodes are present.
• The looser of the match is stored in the internal nodes. But in this overall
winner is stored at tree[0].
• The looser is an alternative representation, that stores the looser of a match
at the corresponding node.
• An advantage of the looser is that, to restructure the tree after winner tree
been output, it is sufficient to examine node on the path from leaf to root
rather than the sibling of the nodes on this path.
• To form a looser tree, we have to create winner tree at first.
• Suppose there are some keys, 10, 2, 7, 6, 5, 9, 12, 1.
• So we will create minimum winner tree at first.
Fig (b): Store looser of the
Fig (a): Minimum winner tree
match in each internal node
Loser tree corresponding to winner tree of fig 5.32
Forests
• A forest is a set of n>=0 disjoint trees.
• The concept of a forest is very close to that of a tree because if we remove
the root of a tree we obtain a forest.
• Ex: removing the root of any binary tree produces a forest of two trees.
PREORDER TRAVERSAL:
• The preorder traversal of T is equivalent to visiting the nodes of F in forest
preorder, which is defined as follows:
1. If F is empty, then return.
2. Visit the root of the first tree of F.
3. Traverse the subtrees of the first tree in tree preorder.
4. Traverse the remaining trees of F in preorder.
INORDER TRAVERSAL:
• Inorder traversal of T is equivalent to visiting the nodes of F in tree inorder,
which is defined as:
1. If F is empty, then return.
2. Traverse the subtrees of the first tree in tree inorder.
3. Visit the root of the first tree.
4. Traverse the remaining trees in tree inorder.
POSTORDER TRAVERSAL:
• There is no natural analog for the postorder traversal of the corresponding
binary tree of a forest. Nevertheless, we can define the postorder traversal
of a forest, F, as follows:
1. If F is empty, then return.
2. Traverse the subtrees of the first tree of F in tree postorder.
3. Traverse the remaining trees of F in tree postorder.
4. Visit the root of the first tree of F.
LEVEL ORDER TRAVERSAL:
• In a level order traversal of a forest, nodes are visited by level,
beginning with the roots of each tree in the forest.
• Withing each level, nodes are visited from left to right.
• The level order traversal of a forest and that of its associated
binary tree do not necessarily yield the same result.
Representation of Disjoint Sets:
• We study the use of trees in the representation of sets.
• We also assume that the sets being represented are pairwise disjoint, that is,
if and Si and Sj; are two sets and i ¹ j, then there is no element that is in
both Si, and Sj.
• For example, if we have 10 elements numbered 0 through 9, we may
partition them into three disjoint sets, S1={0,6,7,8), S2={1,4,9} and
S3={2,3,5}. Set Representation 239
s s2 s3
1
(1) Disjoint set union. If S/ and Sy are two disjoint sets, then their union S, u Sy = {all
• The minimal operations that we can perform on these sets are:
• Disjoint set union: If Si and Sj are two disjoint sets, then their union Si È
Sj = {all elements x, such that x is in Si or Sj).
• Thus, Si È Sj={0, 6, 7, 8, 1, 4, 9}. Since we have assumed that all sets are
disjoint, following the union of Si and Sj we can assume that the sets Si and
Sj no longer exist independently. That is, we replace them by Si È Sj .
• Find(i): Find the set containing the element, i. For example, 3 is in set S3
and 8 is in set S1
5.10.1 Union And Find Operations
Union And Find Operations
• Let us consider
Suppose union
thatthewe operation
wish first. Suppose
to obtain that we
the union of wish to obtain
S1 and S2. the
Since of 5i
unionwe have
and 52- Since we have linked the nodes from children to parent, we simply make one of
linked the nodes from children to parent, we simply make one of the trees a
the trees a subtree of the other. Si u S2 could have either of the representations of Fig-
5.40. of the other. S1 È S2 could have either of the representations of
subtree
ure
figure.
u s2 or s2 u s1
Figure 5.40 : Possible representation of S j u S2
Possible representation of S1 È S2
Union – find functions
int SimpleFind (int i)
{
for(; parent [i] >= 0; i = parent [i] )
return i;
}
void SimpleUnion (int i, int j)
{
parent [i] = j;
}
o Find element i by simply following the parent value starting at i and continue until
we reach a negative parent parent value.
Ex: To find 5, we start at 5, and then move to 5’s parent, 2. since this node has a
negative parent value we have reached the root.
o The operation union (i, j) is equally simple. We pass in two trees with roots i and
j.
o Assuming that we adopt convention that the first tree becomes a subtree of the
second, the statement parent[i] = j accomplishes the union.
Weighting rule for union
Definition: Weighting rule for union(i, j). If the number of nodes in tree i is less
than the number in tree j then make j the parent of i; otherwise make i the parent of
j. The value count[i] will have the total number of nodes in the tree. Assign this
value to the root node and make it as negative to identify the root node of the tree.
void union2(int i, int j)
{
/* union the sets with roots i and j, i != j, using the weighting rule, parent[i] = -
count[i] and parent[j] = -count[j] */
int temp = parent[i] + parent[j];
if (parent[i] > parent[j]) {
parent[i] = j; /* make j the new root */
parent[j] = temp;
}
else {
parent[j] = i; /*make i the new root */
parent[i] = temp;
}
}
Consider the behaviour of weightedUnion on the following sequence of unions starting
from the initial configuration of parent[i] = -count[i]=-1, 0<=i<n=23.
Union(0,1) Union(2,3) Union(4,5) Union(6,7),Union(0,2) Union(4,6) Union(0,4).
The maximum level can be [log2m] +1, if the tree has m nodes.
Collapsing rule
Definition : If j is a node on the path from i to its root and parent[i] ≠
𝑟𝑜𝑜𝑡 𝑖 , then set parent[j] to root(i).
The following function incorporates the collapsing rule into the find operation.
The new function roughly doubles the time for an individual find. However, it
reduces the worst case time over a sequence of finds.
int find2(int i)
{
/* find the root of the tree containing element i. Use the
collapsing rule to collapse all nodes from i to root */
int root, trail, lead;
for (root=i; parent[root]>=0; root = parent[root])
for (trail = i; trail != root; trail=lead)
{
lead = parent[trail];
parent[trail] = root;
}
return root;
}
Application of Equivalence Classes
• Definition: A relation ≡ , over a set S, is said to be an equivalence relation
over S iff it is symmetric, reflexive and transitive over S.
• If an equivalence pair i ≡ j, is to be processed, we must first determine the
sets containing i and j.
• If these are different, then the two sets are to be replaced by their union.
• If the two sets are the same, then nothing is to be done, as the relation i ≡ j
is redundant; i and j are already in the same equivalence class.
• To process each equivalence pair we need to perform two finds and atmost
one union.
Counting Binary Trees:
Distinct Binary Trees:
We know that if n=0 or n=1,there is only one binary tree. If n=2, then there are
two distinct trees (Figure 5.45) and if n = 3, there are five such trees (Figure
5.46)
Stack Permutation:
• Using the concept of an inorder permutation, we can show that the number
of distinct permutations obtainable by passing the numbers 1 to n through a
stack and deleting in all possible ways is equal to the number of distinct
binary trees with n nodes.