Data Structure and Algorithms - Tree: Important Terms
Data Structure and Algorithms - Tree: Important Terms
Data Structure and Algorithms - Tree: Important Terms
Tree represents the nodes connected by edges. Binary Tree is a special data structure
used for data storage purposes. A binary tree has a special condition that each node can
have a maximum of two children. A binary tree has the benefits of both an ordered array
and a linked list as search is as quick as in a sorted array and insertion or deletion
operation are as fast as in linked list.
Important Terms
Following are the important terms with respect to tree.
Path − Path refers to the sequence of nodes along the edges of a tree.
Root − The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
Parent − Any node except the root node has one edge upward to a node called
parent.
Child − The node below a given node connected by its edge downward is called its
child node.
Leaf − The node which does not have any child node is called the leaf node.
Subtree − Subtree represents the descendants of a node.
Visiting − Visiting refers to checking the value of a node when control is on the
node.
Traversing − Traversing means passing through nodes in a specific order.
Levels − Level of a node represents the generation of a node. If the root node is at
level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
keys − Key represents a value of a node based on which a search operation is to
be carried out for a node.
Binary Search tree exhibits a special behavior. A node's left child must have a value less
than its parent's value and the node's right child must have a value greater than its parent
value.
Tree Node
The code to write a tree node would be similar to what is given below. It has a data part
and references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
In a tree, all nodes share common construct.
Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be
inserted, first locate its proper location. Start searching from the root node, then if the data
is less than the key value, search for the empty location in the left subtree and insert the
data. Otherwise, search for the empty location in the right subtree and insert the data.
Algorithm
If root is NULL
then create root node
return
endwhile
insert data
end If
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
Algorithm
If data found
return node
endwhile
end if
The implementation of this algorithm should look like this.
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//go to left tree
//not found
if(current == NULL) {
return NULL;
}
return current;
}
}
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 subtree 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.
o 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 −
o D→B→E→A→F→C→G
orithm
Until all nodes are traversed −
o Step 1 − Recursively traverse left subtree.
o Step 2 − Visit root node.
o 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.
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.
The output of pre-order traversal of this tree will be −
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.
We start from A, and following Post-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. The output of post-
order traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Example Tree
In case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order. To get nodes of BST in non-increasing order, a variation
of Inorder traversal where Inorder itraversal s reversed, can be used.
Example: Inorder traversal for the above given figure is 4 2 5 1 3.
Preorder Traversal:
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Uses of Preorder
Postorder Traversal:
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
Uses of Postorder
Postorder traversal is used to delete the tree.
return(node);
}
getchar();
return 0;
}
Output:
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231
Threaded Binary Tree
Inorder traversal of a Binary tree is either be done using recursion or with the
use of a auxiliary stack. The idea of threaded binary trees is to make inorder
traversal faster and do it without stack and without recursion. A binary tree is
made threaded by making all right child pointers that would normally be NULL
point to the inorder successor of the node (if it exists).
There are two types of threaded binary trees.
Single Threaded:
Where a NULL right pointers is made to point to the inorder successor (if
successor exists)
Double Threaded:
Where both left and right NULL pointers are made to point to inorder
predecessor and inorder successor respectively. The predecessor threads
are useful for reverse inorder traversal and postorder traversal.
The threads are also useful for fast accessing ancestors of a node.
Following diagram shows an example Single Threaded Binary Tree. The
dotted lines represent threads.
Node *left, *right;
bool rightThread;
}
Since right pointer is used for two purposes, the boolean variable rightThread is used to
indicate whether right pointer points to right child or inorder successor. Similarly, we can
add leftThread for a double threaded binary tree.
return n;
}
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to
either of its children.
Max-Heap − Where the value of the root node is greater than or equal to
either of its children.
Both trees are constructed using the same input and order of arrival.
.
We will soon be discussing insertion and deletion in threaded binary trees.
Sources:
http://en.wikipedia.org/wiki/Threaded_binary_tree
www.cs.berkeley.edu/~kamil/teaching/su02/080802.ppt