Unit-Iv Trees: Binary Trees - Operations - Recursive Tree Traversals
Unit-Iv Trees: Binary Trees - Operations - Recursive Tree Traversals
A tree is a non-linear data structure in which item is arranged in a sorted sequence. It is used to
represent hierarchical relationship existing among several data items.
Types of trees:
Binary Tree: Each node has zero, one, or two children. This assertion makes
many tree operations simple and efficient.
Binary Search Tree: A binary tree where any left child node has a value less
than its parent node and any right child node has a value greater than or equal to
that of its parent node.
A binary tree can be shown pictorially. Suppose that T is a binary tree with a
root node A. Let LA denote the left subtree of A and RA denote the right subtree of A.
Now LA and RA are binary trees. Suppose that B is the root node of LA and C is the
root node of RA. B is called the left child of A; C is called the right child of A.
Furthermore, A is called the parent of B and C.
In the diagram of a binary tree, each node of the binary tree is represented as
a circle and the circle is labeled by the node. The root node of the binary tree is
drawn at the top. The left child of the root node (if any) is drawn below and to the
left of the root node. Similarly, the right child of the root node (if any) is drawn
below and to the right of the root node. Children are connected to the parent by an
arrow from the parent to the child. An arrow is usually called a directed edge or a
directed branch (or simply a branch).
The diagram in Figure -1 is an example of a binary tree. The root node of this
binary tree is A. The left sub tree of the root node, which we denoted by LA, is the
set LA = {B, D, E, G} and the right subtree of the root node, which we denote by RA,
is the set RA = {C, F, H}. The root node of the left subtree of A—that is, the root node
of LA—is node B. The root node of RA is C, and so on. Clearly, LA and RA are binary
trees. Because three lines at the end of an arrow mean that the subtree is empty, it
follows that the left
subtree of D is empty.
In Figure -1, the left child of A is B and the right child of A is C. Similarly, for node
F, the left child is H and node F has no right child.
In the binary tree of Figure -2(a), the root node is A, LA = empty, and RA = empty.
In the binary tree of Figure -2(b), the root node is A, LA = {B}, and RA = empty. The
root node of LA = B, LB = empty, and RB = empty.
In the binary tree of Figure -2(c), the root node is A, LA = empty, RA = {C}. The root
node of RA = C, LC = empty, and RC = empty.
In the binary tree of Figure -2(d), the root node is A, LA = {B}, RA = {C}. The root
node of LA = B, LB = empty, RB = empty. The root node of RA = C, LC = empty, RC =
empty.
Examples, every node in a binary tree has at most two children. Thus, every node,
other than storing its own information, must keep track of its left subtree and right
subtree. This implies that every node has two pointers, llink and rlink. The pointer
llink points to the root node of the left subtree; the pointer rlink points to the root
node of the right subtree.
Definition: The height of a binary tree is the number of nodes on the longest path from the root
to a leaf.
Child: Any node may have one or more lines running downward to other nodes. Nodes below
are children.
Leaf: A node that has no children.
Visiting: A node is visited when program control arrives at the node, usually for processing.
Traversing: To traverse a tree means to visit all the nodes in some specified order.
Levels: The level of a particular node refers to how many generations the node is from the root.
Root is assumed to be level 0.
Keys: Key value is used to search for the item or perform other operations on it.
SEARCHING
DELETION ROUTINE
treeNode * Delete(treeNode *node, int data)
{
treeNode *temp;
if(node==NULL)
{
write("Element Not Found");
}
else if(data < node->data)
{
node->left = Delete(node->left, data);
}
else if(data > node->data)
{
node->right = Delete(node->right, data);
}
else
{
/* Now We can delete this node and replace with either minimum element
in the right sub tree or maximum element in the left subtree */
if(node->right && node->left)
{
/* Here we will replace with minimum element in the right sub tree */
temp = FindMin(node->right);
node -> data = temp->data;
/* As we replaced it with some other node, we have to delete that node */
node -> right = Delete(node->right,temp->data);
}
else
{
/* If there is only one or zero children then we can directly
remove it from the tree and connect its parent to its child */
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp); /* temp is longer required */
}
}
return node;
}
Array Representation
• A binary tree may be represented using an array.
• The key concept is that, if a parent is stored in location k, then its left and right child are
located in locations 2k and 2k+1 respectively.
• An Example tree and its array representation is given below.
Linked Representation
Postorder Traversal
The listing of the nodes produced by the postorder traversal of a binary tree
is called the postorder sequence.
In a postorder traversal, the binary tree is traversed as follows:
1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the ROOT node.
1