Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Trees

By Prerna Rai
Introduction: Tree
• Tree represents the nodes connected by edges.
• One of the Tree is Binary Tree.
Binary Tree
• 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.
Binary Tree
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.
Important Terms
• 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.
Types of Binary Tree
• Complete Binary Tree
• Perfect Binary Tree
• Balanced Binary Tree
Complete Binary Tree
• A Binary Tree is a Complete Binary Tree if all the levels are
completely filled except possibly the last level and the last level
has all keys as left as possible.
• A complete binary tree is just like a full binary tree, but with two
major differences:
• Every level except the last level must be completely filled.
• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a
complete binary tree doesn’t have to be a full binary tree.
Perfect Binary Tree
• A Binary tree is a Perfect Binary Tree in which
all the internal nodes have two children and all
leaf nodes are at the same level.
The following are examples of Perfect Binary
Trees.
• A perfect binary tree is a type of binary tree in
which every internal node has exactly two
child nodes and all the leaf nodes are at the
same level.
Representation of Binary Tree

• Each node in the tree contains the following:


• Data
• Pointer to the left child
• Pointer to the right child
Balanced Binary Tree
A balanced binary tree is a binary tree that
follows the 3 conditions:
• The height of the left and right tree for any
node does not differ by more than 1.
• The left subtree of that node is also
balanced.
• The right subtree of that node is also
balanced.
Basic Operations On Binary Tree
• Inserting an element.
• Removing an element.
• Searching for an element.
• Deletion for an element.
• Traversing an element.
Binary Tree Traversals:
• Tree Traversal algorithms can be classified broadly into two
categories:
• Depth-First Search (DFS) Algorithms
• Breadth-First Search (BFS) Algorithms
Tree Traversal using Depth-First Search
(DFS) algorithm
• Preorder Traversal (current-left-right):
• Visit the current node before visiting any nodes inside the left or right subtrees.
• Here, the traversal is root – left child – right child.
• It means that the root node is traversed first then its left child and finally the right child.
• Inorder Traversal (left-current-right):
• Visit the current node after visiting all nodes inside the left subtree but before visiting any node
within the right subtree.
• Here, the traversal is left child – root – right child.
• It means that the left child is traversed first then its root node and finally the right child.
• Postorder Traversal (left-right-current):
• Visit the current node after visiting all the nodes of the left and right subtrees.
• Here, the traversal is left child – right child – root.
• It means that the left child has traversed first then the right child and finally its root node.
Tree Traversal using Breadth-First Search (BFS)
algorithm
• Level Order Traversal:
• Visit nodes level-by-level and left-to-right fashion at the same level.
• Here, the traversal is level-wise.
• It means that the most left child has traversed first and then the other
children of the same level from left to right have traversed.
Tree Traversal
Pre-order Traversal
1-2-4-5-3-6-7
In-order Traversal
4-2-5-1-6-3-7
Post-order
4-5-2-6-7-3-1
Level-order Traversal
1-2-3-4-5-6-7
Algorithm for Binary Tree
1. A new binary tree is created and values are assigned
2. Write a function insert() in such a way that node and key will be two parameters and check for below
conditions,
a. If rootNode == NULL, then return new node to calling function.
b. If rootNode => data < keyValue, then call insert() with rootNode => rightNode and assign return value in
rootNode => rightNode.
c. If rootNode => data > keyValue, then call insert() with rootNode => leftNode and assign return value in
rootNode => leftNode
3. Then finally, we can return original rootNode pointer to calling function.
Implementation of Binary Tree
Implementation of Binary Tree
int main(){
struct node* root = newNode(1);
#include <stdio.h> /* following is the tree after above statement
#include <stdlib.h>
1
struct node { /\
int data; NULL NULL */
struct node* left; root->left = newNode(2);
struct node* right;
root->right = newNode(3);
};
/* 2 and 3 become left and right children of 1
// newNode() allocates a new node 1
// with the given data and NULL left /\
// and right pointers.
23
struct node* newNode(int data)
{
/\/\
// Allocate memory for new node NULL NULL NULL NULL
struct node* node = (struct node*)malloc(sizeof(struct node)); */
// Assign data to this node
root->left->left = newNode(4);
node->data = data;
// Initialize left and
/* 4 becomes left child of 2
// right children as NULL 1
node->left = NULL; /\
node->right = NULL; 23
return (node);
/\/\
}
4 NULL NULL NULL
/\
NULL NULL*/ }

You might also like