Unit-6 Trees
Unit-6 Trees
STRUCTURE- TREES
Unit-4
NON LINEAR DATA STRUCTURES
🞆 In non-linear data structure, the data values
are not arranged in order and a data item is
connected to several other data items.
🞆 It uses memory efficiently. Free contiguous
memory is not required for allocating data
items.
🞆 Examples-
⚫ Trees and Graphs.
TREES
🞆 Tree is a hierarchical data structure which stores
the information naturally in the form of hierarchy
style.
🞆 Tree is one of the most powerful and advanced
data structures.
🞆 It is a non-linear data structure compared to
arrays, linked lists, stack and queue.
🞆 It represents the nodes connected by edges.
🞆 Tree is a collection of elements called Nodes,
where each node can have arbitrary number of
children.
EXAMPLE Roo
t
A Level -
0
Level -
B C 1
Parent
Node D E F G Level -
2
Child
Node H I Sibling Level -
s 3
Subtre
e
Leaf
Node
🞆 The above figure represents structure of a tree.
Tree has 2 subtrees.
⚫ A is a parent of B and C.
⚫ B is called a child of A and
⚫ B is also parent of D and E
IMPORTANT TERMS
🞆 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 TREE
🞆 A tree whose elements have at most 2 children
is called a binary tree.
🞆 Since each element in a binary tree can have
only 2 children, we typically name them the left
child and right child.
🞆 A tree is represented by a pointer to the
topmost node in tree.
🞆 If the tree is empty, then value of root is NULL.
🞆 A Tree node contains following parts.
1. Data
2. Pointer to left child
3. Pointer to right child
STRUCTURE OF NODE IN TREES
struct node
{
int data;
struct node *left;
struct node *right;
};
TRAVERSAL IN TREES
🞆 Traversal is a process to visit all the nodes of
a tree print their values.
🞆 Because, all nodes are connected via edges
(links) we always start from the root (head)
node.
🞆 We cannot randomly access a node in a tree.
🞆 There are three ways to traverse a tree −
⚫ Pre-order Traversal
⚫ In-order Traversal
⚫ Post-order Traversal
PRE-ORDER TRAVERSAL
DATA- LEFT- RIGHT (DLR)
A→B→D→E→C→F→G
ALGORITHM &FUNCTION (RECURSIVE)
Algorithm
Until all nodes are traversed −
🞆 Step 1 − Visit root node.
Function
void preorder( struct node *tnode)
{
if(tnode)
{
printf(“%d”, & tnode->data);
preorder(tnode->leftchild);
preorder(tnode->rightchild);
}
}
NON RECURSIVE FUNCTION-PREORDER
void preorder(struct btreenode *root )
{
while(isempty(&stk))
{
push(&stk, root);
while(root!=NULL)
{
//Print data
printf(“%d”, root->data);
//Push right value into stack
push(&stk, root->right);
//Move to left
root=root->left;
}
if(isempty)
return;
root=pop(&stk);
}
IN-ORDER TRAVERSAL
LEFT- DATA- RIGHT (LDR)
D→B→E→A→F→C→G
ALGORITHM &FUNCTION (RECURSIVE)
Algorithm
Until all nodes are traversed −
🞆 Step 1 − Recursively traverse left subtree. (Inorder left-subtree)
Function
void inorder( struct node *tnode)
{
if(tnode)
{
inorder(tnode->leftchild);
printf(“%d”, & tnode->data);
inorder(tnode->rightchild);
}
}
NON RECURSIVE FUNCTION-INORDER
void inorder(struct btreenode *root )
{
while(isempty(&stk))
{
push(&stk, root);
while(root!=NULL)
{
push(&stk, root->left);
root=root->left;
}
if(isempty)
return;
root=pop(&stk);
printf(“%d”, root->data);
root=root->right;
}
}
POST-ORDER TRAVERSAL
LEFT – RIGHT - DATA (LRD)
D→E→B→F→G→C→A
ALGORITHM & FUNCTION (RECURSIVE)
Algorithm
Until all nodes are traversed −
🞆 Step 1− Recursively traverse left subtree. (Postorder left-subtree)
Function
void postorder( struct node *tnode)
{
if(tnode)
{
postorder(tnode->leftchild);
postorder(tnode->rightchild);
printf(“%d”, & tnode->data);
}
}
NON RECURSIVE FUNCTION-POSTORDER
void postorder(struct btreenode *root )
{
while(isempty(&stk)
{
while(root!=NULL)
{
//Push the right
push(&stk, -(root->right));
//Move to left
root=root->left;
push(&stk, root->left);
}
if(isempty)
return;
root=pop(&stk);
if (root>0)
printf(“%d”, root->data);
else
push(&stk, -(root));
}
TYPES OF BINARY TREES
🞆 Full Binary Tree
🞆 Complete Binary Tree
🞆 Binary Search Tree
🞆 Expression Tree
FULL BINARY TREE
🞆 A full binary tree (sometimes proper binary
tree or 2-tree) is a tree in which every node
other than the leaves has two children.
COMPLETE BINARY TREE
🞆 Case-1
⚫ Print the message “Data required is not present”
🞆 Case-2
⚫ Free the memory occupied by the node to be deleted
⚫ Either left or right link of parent node(where node to
be deleted is attached) is set to NULL
CONTD..
🞆 Case-3
⚫ Adjust the pointer of the parent of the node to be
deleted such that after deletion it points to the
child of the node
🞆 Case-4
⚫ The inorder successor of the node to be deleted
occupies its place and is deleted from its original
location
FUNCTION TO INSERT IN BINARY SEARCH TREE
void insert(struct btreenode*, int num)
{
struct node *new= malloc(sizeof (struct node));
new->left=NULL;
new->data=num;
new->right=NULL;
if(root==NULL)
root=new;
else
{
if(num<root)
insert(root->left, num)
else
insert(root->right, num)
}
}
FUNCTION TO DELETE A NODE IN BINARY
SEARCH TREE
void delete(struct btreenode*, int num)
{
struct btreenode *parent, *x, *xsucc;
if(root==NULL)
printf(“Tree is empty”);
else
search(int num);
3 4