Вы находитесь на странице: 1из 14

UNIT-IV

Trees: Binary Trees - Operations - Recursive Tree


Traversals.
Trees
A binary tree, T, is either empty or such that
i. T has a special node called the root node.
ii. T has two sets of nodes, LT and RT, called the left subtree and right subtree of
T, respectively.
iii. LT and RT are binary trees

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.

The following struct defines the node of a binary tree:


template <class elemType>
struct binaryTreeNode
{
elemType info;
binaryTreeNode<elemType> *llink;
binaryTreeNode<elemType> *rlink;
};
From the definition of the node, it is clear that for each node,
• The data is stored in info.
• A pointer to the left child is stored in llink.
• A pointer to the right child is stored in rlink.
TERMINOLOGY OF TREE
Definition: The level of a node in a binary tree is the number of branches on the path from the
root to the node. Clearly, the level of the root node of a binary tree is 0, and the level of the
children of the root node is 1.

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.

OPERATIONS ON BINARY TREE


Traversals
• Traversal refers to visiting all the nodes of a binary tree exactly once in a systematic way.
Insertion
• Refers to inserting a new element into the tree
Deletion
• Refers to removing an element from the tree
Searching
• Search operation checks whether the given data is present in the tree or not.
INSERTION ROUTINE

treeNode * Insert(treeNode *node,int data)


{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
temp -> left = temp -> right = NULL;
return temp;
}
if(data >(node->data))
{
node->right = Insert(node->right,data);
}
else if(data < (node->data))
{
node->left = Insert(node->left,data);
}
/* Else there is nothing to do as the data is already in the tree. */
return node;
}

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;
}

REPRESENTATION OF BINARY TREES


 Array Representation
 Linked Representation
 Threaded Representation

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

Applications of Binary tree


• Expression Evaluation (Expression tree)
• Data Searching
• Data sorting

Properties of Binary Tree


• A tree with n nodes has exactly (n-1) edges or branches.
• In a tree every node except the root has exactly one parent (and the root node does not have a
parent).
• There is exactly one path connecting any two nodes in a tree.
Binary Tree Traversal
The item insertion, deletion, and lookup operations require that the binary
tree be traversed. Thus, the most common operation performed on a binary tree is
to traverse the binary tree, or visit each node of the binary tree. As you can see
from the diagram of a binary tree, the traversal must start at the root node because
there is a pointer to the root node. For each node, we have two choices:
• Visit the root node first.
• Visit the subtrees first.
These choices lead to three different traversals of a binary tree—
Inorder, preorder, and postorder.
Inorder Traversal
The listing of the nodes produced by the inorder traversal of a binary tree is
called the inorder sequence.
In an inorder traversal, the binary tree is traversed as follows:
1. Traverse the left subtree.
2. Visit the ROOT node.
3. Traverse the right subtree.
Preorder Traversal
The listing of the nodes produced by the preorder traversal of a binary tree is
called the preorder sequence.
In a preorder traversal, the binary tree is traversed as follows:
1. Visit the ROOT node.
2. Traverse the left subtree.
3. Traverse the right subtree.

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

Вам также может понравиться