Chapter -5 Tree
Chapter -5 Tree
Introduction
Linked lists provide greater flexibility than arrays but they are linear structures so that it
is difficult to use them to organize a hierarchical representation of objects.
Stacks and queues reflect some hierarchy but limited to one dimension
Unlike stacks, queues, linked lists and arrays, Trees are a non linear data structures.
In linked list each node has a link which points to another node but in tree each node may
point to several other nodes.
Trees are very flexible, versatile and powerful non-liner data structure that can be
used to represent data items possessing hierarchical relationship between the grand father
and his children and grand children as so on.
A tree is a set of nodes and edges that connect pairs of nodes. It is an abstract model of
a hierarchical structure.
Terminologies
Root: node with no parent
Leaf node (external node): node with no child
Internal node: a node with at least one child.
Degree of a node: is the number of sub trees of a node in a given tree.
Degree of a tree: the highest number of sub tree in the tree
Ancestors of a node: all parents and Grand.parents of a node
Descendants of a node: all children and Grand. children of a node
Depth of a node: number of ancestors or length of the path from the root to the node.
Height of a tree: depth of the deepest node
Sub tree: a tree consisting of a node and its descendants.
Example:-
Binary tree: a tree in which each node has at most two children called left child and right child.
Binary search tree (ordered binary tree): a binary tree that may be empty, but if it is not
empty it satisfies the following.
o Every node has a key and no two elements have the same key.
o The keys in the right sub tree are larger than the keys in the root.
o The keys in the left sub tree are smaller than the keys in the root.
o The left and the right sub trees are also binary search trees.
Example:-
1. 10,15,7,3,8,20,25 4. 14,15,4,9,7,18,3,5,16,4,20,17,9,14,5
2. 85,80,90,82,70,100,87 5. 90,95,100
3. 20,10,30,15,25,50,40,1,5,79
Basic Tree Operations (binary tree)
Consider the following definition of binary search tree.
struct Node
{
int Num;
Node * Left, *Right;
};
Node *RootNodePtr=NULL;
Basic Operations
Following are the basic operations of a tree −
Tree Traversing
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.
In-order Traversal
In this traversal method, the
left sub tree is visited first, then
the root and later
the right sub-tree.
We should always remember that every node may represent a subtree itself. If a binary tree is
traversed in-order, the output will produce sorted key values in an ascending order. Used to
generate mathematical expression in infix notation.
We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree. From the above tree diagram
We start from A, and following pre-order traversal, we first visit A itself and then move to its
left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited.
Used to generate mathematical expression in prefix notation.
A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node. From the above given tree diagram
We start from A, and following pre-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. Used to generate
mathematical expression in postfix notation.
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Exercise