Lecture 8 - Trees
Lecture 8 - Trees
DATA
STRUCTURES
WHAT IS A TREE?
• Tree is a non-linear data structure unlike linked lists, stacks and
queues.
• If a tree is a collection of N
nodes, then it has N-1 edges.
T2
...
T1 Tk
MOTIVATION
• Trees are one of the most
important and extensively
used data structures in
computer science
• Several examples:
– File systems of several
operating systems
– Expression Trees
– Layout of a webpage
Program 1. A simple HTML document.
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>
<p>This is a paragraph with some <u>underlined</u> text.</p>
</body>
</html>
THE TREE
STRUCTURE
OF XML
A rendering of the HTML in Program 1
TREE TERMINOLOGY
Root: The first node from where the tree originates.
The only node in the tree with no parent.
Subtree:
General
tree
• The binary tree is a useful data structure for rapidly storing sorted data and rapidly
retrieving stored data.
TYPES OF BINARY TREE
• Different types of binary trees are possible. Following are common types of binary
trees:
– Strictly Binary Tree
– Extended Binary Tree
– Complete Binary Tree
– Full Binary Tree
– Skewed Binary Tree
– Binary Expression Tree
– Balanced Binary Tree
– Threaded Binary Tree An ancestry chart which maps to a perfect
– Binary Search Trees depth-4 binary tree
BINARY TREE REPRESENTATIONS
• Binary Trees can be represented in two ways:
– Array Representation
– Linked List Representation
BINARY TREE REPRESENTATIONS
Array Representation of Binary Tree
• One-dimensional array is used to represent a binary tree.
• To represent a binary tree of height ‘h' using array representation, we need one
dimensional array with a maximum size of
• Indexes of other nodes for the ith node, i.e., Arr[i]:
– Arr[(i-1)/2] Returns the parent node
– Arr[(2*i)+1] Returns the left child node
– Arr[(2*i)+2] Returns the right child node
return(node); return 0;
} }
// Insert on the left of the node
struct node* insertLeft(struct node* root, int
value) {
root->left = createNode(value);
return root->left;
}
insertLeft(root->left, 5);
insertRight(root->left, 6);
Return 0
}
TREE TRAVERSAL
• Traversal is a process to visit all the nodes of a tree.
• 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.
• Linear data structures like arrays, stacks, queues, and linked list have only one way to read the
data. But a hierarchical data structure like a tree can be traversed in different ways.
– 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.
ALGORITHM
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
D→B→E→A→F→C→G
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
PRE-ORDER TRAVERSAL
ALGORITHM
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree. A→B→D→E→C→F→G
Step 3 − Recursively traverse right subtree.
POST-ORDER TRAVERSAL
ALGORITHM
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree. D→E→B→F→G→C→A
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Inorder
Preorder
Postorder
?
Inorder
10 11 12 13 14 15 17 20 24 26 Postorder
11 13 12 10 17 15 24 26 20 14
Preorder
14 10 12 11 13 20 15 17 26 24
Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->
EXAMPLE:
return (node);
}
void NthInorder(struct Node* node, int n){
static int count = 0;
int main()
if (node == NULL)
{
return;
struct Node* root = newNode(10);
root->left = newNode(20);
if (count <= n) {
root->right = newNode(30);
root->left->left = newNode(40);
/* first recur on left child */
root->left->right = newNode(50);
NthInorder(node->left, n);
count++;
int n = 4;
// when count = n then print element
NthInorder(root, n);
if (count == n)
return 0;
printf("%d ", node->data);
}
/* now recur on right child */
NthInorder(node->right, n);
}
}
EXAMPLE
find sum of all the elements
//Find the minimum tree node if //Find the maximum tree node if
available. available.
FindMin(T) FindMax(SearchTree T)
{ {
if(T!=NULL) if(T==NULL)
{ return NULL;
while(T->Left!=NULL) else if(T->Right==NULL)
T=T->Left; return T;
} else
return T; return FindMax(T->Right);
} }
DELETION
• There are three situations of deleting a node from binary search tree.
– The node to be deleted is a leaf node
– The node to be deleted has only one child.
– The node to be deleted has two children.
THE NODE TO BE DELETED IS A LEAF
NODE
• Replace the leaf node with the NULL and simple free the allocated space.
THE NODE TO BE DELETED HAS ONLY
ONE CHILD.
• Replace the node with its child and delete the child node, which now contains the
value which is to be deleted. Simply replace it with the NULL and free the allocated
space
THE NODE TO BE DELETED HAS TWO
CHILDREN.
• The node which is to be deleted, is replaced with its in-order successor or predecessor
recursively until the node value (to be deleted) is placed on the leaf of the tree. After the
procedure, replace the node with NULL and free the allocated space.
SEARCHING
while(current->data != data){
if(current != NULL) {
printf("%d ",current->data);
return current;
}
BINARY SEARCH TREE VISUALIZATION
• http://btv.melezinek.c
z/binary-search-
tree.html
TREE APPLICATIONS
• Binary Search Trees(BSTs) are used to quickly check whether an element is present in
a set or not.
• A modified version of a tree called Tries is used in modern routers to store routing
information.
• Most popular databases use B-Trees and T-Trees, which are variants of the tree
structure we learned above to store their data
• Compilers use a syntax tree to validate the syntax of every program you write