0% found this document useful (0 votes)
41 views30 pages

Binary Tree and BST

Uploaded by

samarthlike08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views30 pages

Binary Tree and BST

Uploaded by

samarthlike08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

ICS121 – Data Structures I

Module 2 – Binary Tree


Binary Trees – Insertion and Deletion of nodes, Tree
Traversals, Polish Notations, Red Black Trees, B-Trees,
Heaps, Priority Queues.

Optimal binary search tree, Application problems on


Optimal binary search Tree
Tree
Tree:
• A tree is a non-linear data structure in which data is
organized in branches.
• It imposes a hierarchical structure on the data
elements.
• A Tree is a collection of nodes.
• There is a specially designated node called Root
Node.
• The Remaining nodes are partitioned in sub-trees
like T1, T2,…Tn.
• The tree will have unique path from root node to its
children or leaf node.
• The tree does not have cycle.

• Construct tree:
• The root of the tree is the node 60 at the top.
• Node 29 and 44 are the successors of the
node 60.
• The nodes 6, 4, 12 and 67 are the terminal
nodes.
Applications:
• Implementing the hierarchical structures in computer systems like
directories and file systems.
• Implementing the navigation structure of a website.
• Code generation like Huffman’s code.
• Decision-making in gaming applications.
• Implementation of priority queues for priority-based OS
scheduling functions
• Parsing of expressions and statements in programming language
compilers
• For storing data keys for DBMS for indexing
• Spanning trees for routing decisions in computer and communications
networks
• Hash trees
• Path-finding algorithm to implement in AI, robotics and video games
Properties of the Tree:
• The root of each sub-tree is a child of a Root node.
• Every node except the root node has one parent node and all the parent nodes have at least
one child.
• The node which has no child called leaf node or terminal nodes.
• The nodes with the same parent are called siblings.
Properties of the Tree:
• The depth of a node n is the unique path from root node to the node n.
• The height of a node n is the unique path from the node n to the root node.
• The height of the tree must be equal to the depth of the tree. Therefore H(T)= D(T).
• All the leaves at height zero and the depth of the root is zero.
• Level- A layer of parent nodes corresponding to a given a node of the tree.
• A tree should not have multiple paths from node n1 to node n2.
Binary Tree:
• Binary Tree is defined as a tree data structure where each node has at most 2 children. Since
each element in a binary tree can have only 2 children, we typically name them the left and
right child.

Types:
Full Binary Tree/ Proper Binary Tree:
• A full Binary tree is a special binary tree in which every parent node/internal node has two or no children.

Complete Binary Tree:


• Every level must be completely filled (except possibly the last)
• All the leaf elements must lean towards the left.
• The last leaf element might not have the right sibling.
Perfect Binary Tree:
• All the internal nodes have two children and all leaf nodes are at the same level.

A degenerate (or pathological) tree:


• A Tree where every internal node has one child. Such trees are performance-wise same
as linked list.
• A degenerate or pathological tree is the tree having a single child either left or right.

Skewed Binary Tree:


• A skewed binary tree is a pathological/degenerate tree in which the tree is either
dominated by the left nodes or the right nodes.
Balanced Binary Tree:
• A binary tree is balanced if the height of the tree is O(Log n) where n is the number of
nodes.
Conversion of Tree to Binary tree:
• The left child of a node n will remain the left child of a node n and the right sibling will become the
right child of a node n.
1. The root of the Binary Tree is the Root of the N-array Tree.
2. The left child of a node in the N-array Tree is the Left child of that node in the Binary Tree.
3.The right sibling of any node in the N-array Tree is the Right child of that node in the Binary
Tree

Construction of Binary Tree:


1. Start with the first value, and become the root node.
2. The next value will be added at the last position of the tree.
3. Always first add the left child of the parent then the right child to the parent.
Properties of a binary tree
• The maximum number of nodes at level ‘l’ of a binary tree is 2l.
• The Maximum number of nodes in a binary tree of height ‘h’ is 2h+1 – 1.
• In a Binary Tree with N nodes, minimum possible height or the minimum number of levels
is
• Ceiling (Log2(N+1)) (if index from 1)
• Floor (Log2(N)) (if index from 0)

• A Binary Tree with L leaves has at least | Log2L |+ 1 levels.


• In Binary tree where every node has 0 or 2 children, the number of leaf nodes is always one
more than nodes with two children.
Binary Tree Representation:
• Array
• Linked list

Properties of a binary tree when implemented with array


• In a non empty binary tree, if n is the total number of nodes and e is the total number of
edges, then e = n-1.
• If child at position i, then parent can be found at position i-1/2
• If parent at position i, then
• Left child = 2i+1
• Right child = 2i+2
Binary Tree Representation using linked list

Node:
struct node
{
int data;
struct node *left,*right;
}
Basic Operations:
1. Insertion
2. Traversal
3. Deletion • Other operations:
4. Searching • Find height of the tree
• Find number of nodes in the tree.
• Find the level of the tree
1. Insertion:
Steps to construct Binary Tree:
struct BTnode* createNode(value)
//Root node : level 0 node
{ struct BTnode* rootNode = createNode(7);
//Left node of root: level 1 node
struct BTnode* newNode = malloc(sizeof(struct BTnode));
newNode->data = value; insertLeftNode(rootNode, 4);
newNode->leftNode = NULL; //Right node of root
newNode->rightNode = NULL;
insertRightNode(rootNode, 8);
return newNode;
} //level 2 nodes
insertLeftNode(rootNode->leftNode, 1);
struct BTnode* insertLeftNode(struct BTnode* rootNode, int insertRightNode(rootNode->rightNode, 5);
value) insertLeftNode(rootNode->leftNode, 6);
insertRightNode(rootNode->rightNode, 3);
{
rootNode->leftNode = createNode(value);
return rootNode->leftNode;
}

struct BTnode* insertRightNode(struct BTnode* rootNode, int


value)
{
rootNode->rightNode = createNode(value);
return rootNode->rightNode;
}
2. Tree Traversal:
• Tree traversal is to visit all the nodes exactly once.
• Tree traversal is possible only for Simple tree and all the Binary trees.
• The traverse will start from root node and covers all the nodes from left to
right.
Binary Tree Traversal:
• There are three types of binary tree traversals:

1. Pre-Order Traversal Root-L-R


2. In-Order Traversal L-Root-
3. Post-Order Traversal R
L-R-
Root
Tree traversal Algorithm:

preorder(struct BTnode* rootNode) {


if (rootNode != NULL) {
Write rootNode-
>data;
preorder(rootNode->leftNode);
preorder(rootNode-
>rightNode);
}
Tree traversal Algorithm:

inorder(struct BTnode* rootNode) {


if (rootNode != NULL)
{ inorder(rootNode->leftNode);
Write rootNode->data;
inorder(rootNode->rightNode);
}
}
Tree traversal Algorithm:

postorder(struct BTnode* rootNode) {


if (rootNode != NULL)
{ postorder(rootNode->leftNode);
postorder(rootNode->rightNode);
write rootNode->data;
}
}
Deletion operation:

1. If the node is leaf

2. If the node is non leaf

i. With one child

ii. With two children


Deletion operation:

deleteTree(struct BTnode*root){
if(root == NULL)
return;
/* Delete Left sub-tree */
deleteTree(root->left);

/* Delete right sub-tree */


deleteTree(root->right);

/* At last, delete root node */


write root->data;
free(root);
}
Binary Search Tree:
Special BT, in which searching is efficient
Binary Search Tree: else if (key < prev->key)
Insertion Operation: {
insert(BSTNode* &root, int key) prev->left = toinsert;
{ }
BSTNode* toinsert = newNode(key); else
BSTNode* curr = root; {
BSTNode* prev = NULL; prev->right = toinsert;
while (curr != NULL) }
{ }
prev = curr;
if (key < curr-
>key)
curr =
curr-
>left;
else
curr =
curr-
>right;
}
if (prev == NULL)
Binary Search Tree:
Search Operation:
SEARCH(x, root)
if(root != null)
if(root->data == x)
write “search element found”
else if(root->data > x)
return SEARCH(x, root->left)
else if(root->data < x)
return SEARCH(x, root-
>right)
else
write” Search element is
not found”
Binary Search Tree:
Finding Maximum/Minimum element Operation:

BSTNode* getmin( BSTNode* root)


{
BSTNode* curr = root;
while (curr && curr->left)
{
curr = curr->left;
}
return curr;
}
BSTNode*
getmax( BSTNode* root)
{
BSTNode* curr = root;
while (curr && curr->right)
{
curr = curr->right;
}
return curr;
}
Binary Search Tree:
Deletion Operation:
struct BSTNode* deleteNode(Node* root, int val)
{
//deletion at left subtree 2 2
if (val < root->data) 1 1
root->left = deleteNode(root->left, val);

//deletion at right subtree Delete


else if (val > root->data) 1
5
2
7
(7) 1
5
2
7
root->right = deleteNode(root->right, val);

//deletion at root
else if(root->data==val) 7 1 2 3 1 2 3
{ 9 3 5 Viewer does not support full
SVG 1.1
9 3 5

//no child
if(root->left == NULL && root->right == NULL)
{
free(root);
}
Binary Search Tree:
Deletion
21 21
Operation:
else ifchild
//one (root->left == NULL)
{
BSTNode* temp = root;
Delete
root=root->right; 15 27 (15) 7 27
free(temp);
}
else if (root->right ==
NULL) 7 23 35 23 35
{ Viewer does not support full
SVG 1.1
BSTNode* temp = root;
root=root->left;
free(temp)
}
Binary Search Tree:
Deletion Operation:
//two children
else
{
BSTNode* temp = getMin(root->right);
root->data=temp->data;
root->right=deleteNode(root-
>right,temp->data); //removing
duplicate
2 2
} root;
return 1 3
}

Delete
1 2 (21) 1 2
5 7 5 7

7 1 2 3 7 1 3
9 3 5 Viewer does not support full 9 5
Polish notation Conversion– Binary tree

Given In-order and Pre-order, we can find the Post-order


Unique Tree

Given In-order and Post-order, we can find the Pre-order


More than
one Tree Given Pre-order and Post-order, we cannot find the In-order

Given,

In-order - 4, 2, 5, 1, 3, 6

Pre-order- 1, 2, 4, 5, 3, 6

Post-order- ?
Polish notation Conversion– Binary search tree

In a binary search tree, in-order list will always be in a sorted or ascending order,
Given any one order, we can find the other one

Given,

In-order - ?

Pre-order- 5 3 1 2 4 6 8 7

Post-order- ?
Exercise

1. Given BT,

In-order – D, B, H, E, I, A, F, C, G

Pre-order- A, B, D, E, H, I, C, F, G

Post-order-

2. Given BST,

In-order - ?

Pre-order- ?

Post-order- 35, 32, 30, 120, 100, 90, 80, 40

You might also like