KEMBAR78
Binary Tree in Data Structure | PPTX
PRESENTED BY:
MEGHAJ KUMAR MALLICK
MCA 1ST YEAR ,2ND SEMESTER
(MCA/25017/18)
Binarytree
Binary tree
A binary tree is simply a tree in which each node can have at
most two children. It may be 0, 1 or 2.
Root node
left sub tree
Right sub tree
Binary Tree
Binary Tree
A
B
G
C
D E F
Left Child Right Child
Root
Parent
Left
subtree
CONTINUED
Left Child: The node on the left of any node is called left child.
Right child: The node on the right of any node is called right
child.
Left Sub tree: sub tree attached to left side of root node is called
left sub tree.
Right Sub tree: sub tree attached to right side of root node is
called right sub tree.
The node of a binary tree is divided into three parts :
Left childAddress Left Info Right Right child Address
Linked Representation
Ptr -> Info = Data
Ptr -> Left = Left Child
Ptr -> Right = Right Child
A
B C
11
32
D E
54
F G
76
Left Info Right
Binary tree Representation using Linked List
y
From General Tree
Binary Tree
(Linked List)
A
B
D
E F
C
I G
H
Head
Binary Tree
A
B
H
CD
E F
GI
Types of Binary Tree
Full Binary Tree Complete Binary Tree
A
B
G
C
D E F
A
B C
D E
• All nodes (except leaf)
have two children.
• Each subtree has same
length of path.
• All nodes (except leaf)
have two children.
• Each subtree can has
different length of path.
Binary search tree
A binary Search Tree is special kind if tree that
satisfied the following conditions
• If value of inserted node is bigger than parent
then it will be right subtree.
• If value of inserted node is smaller than parent
then it will be left subtree.
• This tree is known as binary search tree or
ordered binary tree.
• It used for searching
Construction of binary Search Tree (BST)
1. Construction of binary Search Tree (BST)
Step.1: Initially tree is empty ,place the first element at the root.
Step.2: Compare the next element with root
if element is grater than or equal to root then place it in
right child position.
else
place it in the left child position.
Step.3: Repeat the process (step 2) for the next element until
end of elements or nodes.
ALGORITHM FOR SEARCHING IN BST
Algorithm :
Search BST(key,targetkey)
1) START
2) If root==NULL
3) Return value not found
4) End if
5) If(target key<root)
6) Return searchBST (leftsubtree, targetkey)
7) else if(targetkey > root)
8) Return searchBST (rightsubtree, targetkey)
9) ELSE
10)FOUND target key
11)Return root
12)End if
13)End SearchBST
Example of searching in BST
5220 35
12
E.g. if we have to search 12 than go to left sub
tree
23
18 44
Binary SEARCHTREE(INSERTION)
• For insert data in
binary tree two conditions we have
to faced
1. IF tree is empty than insert to a new node make them root node.
2. If tree is not empty than all inserts take place at a leaf or at a
• leaf like node (a tree that has only one NULL subtree
Example of insertion in BST
23
4418
5220 35
12
23
4418
5220 35
12
Before insertion
19
After insertion
Binary SEARCHTREE(INSERTION)
Algorithm
1. START
2. If root==NULL
3. Root=newnode
4. If (newnode<root)
5. Return addBST(leftsubtree,newnode)
6. ELSE
7. Return addBST(rightsubtree,newnode)
8. End if
9. End addBST
 Traversal of a binary tree is to accessevery node of binary
tree at most once
• Tree traversal ways
a) Pre 0rder
b) In Order
c) Post Order
BST Traversal
1. start
2. Visit root
3. Visit left sub-tree
4. Visit right sub-tree
5. stop
5
2 1
3 8
97
5 2 1 3 7 9 8
Preorder
start
Left sub
tree
Right sub
tree
1. Visit left sub-tree
2. Visit root
3. Visit right sub-tree
5
2
2 1 3 5 7 9 8
Inorder
1
3 8
97
Right sub
tree
1. Visit left sub-tree
2. Visit right sub-tree
3. Visit root
5
2 1
3 8
97
2 1 3 7 9 8 5
Postorder
start
When we delete a node, we need to consider
how we take care of the children of thedeleted
node.
DELETE NODE
Three cases:
(1) the node is a leaf
 Delete it immediately
(2) the node has one child
 Adjust a pointer from the parent to bypass that node
Delete Cont…..
3)the node has 2 children
 replace the key of that node with the
minimum element at the right sub tree
 delete the minimum element
 Has either no child or only right child because
if it has a left child, that left child would be
smaller and would have been chosen. So
invoke case 1or 2.
Binary tree Applications
 File System(storing naturally hierarchical
data)
 Organize data(searching,insertion,deletion
Binary search tree used for this purpose
 Telephone dictionary
 Arithmetic operations
 Network routing etc
Binary Tree in Data Structure

Binary Tree in Data Structure

  • 1.
    PRESENTED BY: MEGHAJ KUMARMALLICK MCA 1ST YEAR ,2ND SEMESTER (MCA/25017/18) Binarytree
  • 2.
    Binary tree A binarytree is simply a tree in which each node can have at most two children. It may be 0, 1 or 2. Root node left sub tree Right sub tree Binary Tree
  • 3.
    Binary Tree A B G C D EF Left Child Right Child Root Parent Left subtree
  • 4.
    CONTINUED Left Child: Thenode on the left of any node is called left child. Right child: The node on the right of any node is called right child. Left Sub tree: sub tree attached to left side of root node is called left sub tree. Right Sub tree: sub tree attached to right side of root node is called right sub tree. The node of a binary tree is divided into three parts : Left childAddress Left Info Right Right child Address
  • 5.
    Linked Representation Ptr ->Info = Data Ptr -> Left = Left Child Ptr -> Right = Right Child A B C 11 32 D E 54 F G 76 Left Info Right Binary tree Representation using Linked List y
  • 6.
    From General Tree BinaryTree (Linked List) A B D E F C I G H Head Binary Tree A B H CD E F GI
  • 7.
    Types of BinaryTree Full Binary Tree Complete Binary Tree A B G C D E F A B C D E • All nodes (except leaf) have two children. • Each subtree has same length of path. • All nodes (except leaf) have two children. • Each subtree can has different length of path.
  • 8.
    Binary search tree Abinary Search Tree is special kind if tree that satisfied the following conditions • If value of inserted node is bigger than parent then it will be right subtree. • If value of inserted node is smaller than parent then it will be left subtree. • This tree is known as binary search tree or ordered binary tree. • It used for searching
  • 9.
    Construction of binarySearch Tree (BST) 1. Construction of binary Search Tree (BST) Step.1: Initially tree is empty ,place the first element at the root. Step.2: Compare the next element with root if element is grater than or equal to root then place it in right child position. else place it in the left child position. Step.3: Repeat the process (step 2) for the next element until end of elements or nodes.
  • 10.
    ALGORITHM FOR SEARCHINGIN BST Algorithm : Search BST(key,targetkey) 1) START 2) If root==NULL 3) Return value not found 4) End if 5) If(target key<root) 6) Return searchBST (leftsubtree, targetkey) 7) else if(targetkey > root) 8) Return searchBST (rightsubtree, targetkey) 9) ELSE 10)FOUND target key 11)Return root 12)End if 13)End SearchBST
  • 11.
    Example of searchingin BST 5220 35 12 E.g. if we have to search 12 than go to left sub tree 23 18 44
  • 12.
    Binary SEARCHTREE(INSERTION) • Forinsert data in binary tree two conditions we have to faced 1. IF tree is empty than insert to a new node make them root node. 2. If tree is not empty than all inserts take place at a leaf or at a • leaf like node (a tree that has only one NULL subtree
  • 13.
    Example of insertionin BST 23 4418 5220 35 12 23 4418 5220 35 12 Before insertion 19 After insertion
  • 14.
    Binary SEARCHTREE(INSERTION) Algorithm 1. START 2.If root==NULL 3. Root=newnode 4. If (newnode<root) 5. Return addBST(leftsubtree,newnode) 6. ELSE 7. Return addBST(rightsubtree,newnode) 8. End if 9. End addBST
  • 15.
     Traversal ofa binary tree is to accessevery node of binary tree at most once • Tree traversal ways a) Pre 0rder b) In Order c) Post Order BST Traversal
  • 16.
    1. start 2. Visitroot 3. Visit left sub-tree 4. Visit right sub-tree 5. stop 5 2 1 3 8 97 5 2 1 3 7 9 8 Preorder start Left sub tree Right sub tree
  • 17.
    1. Visit leftsub-tree 2. Visit root 3. Visit right sub-tree 5 2 2 1 3 5 7 9 8 Inorder 1 3 8 97 Right sub tree
  • 18.
    1. Visit leftsub-tree 2. Visit right sub-tree 3. Visit root 5 2 1 3 8 97 2 1 3 7 9 8 5 Postorder start
  • 19.
    When we deletea node, we need to consider how we take care of the children of thedeleted node. DELETE NODE
  • 20.
    Three cases: (1) thenode is a leaf  Delete it immediately (2) the node has one child  Adjust a pointer from the parent to bypass that node Delete Cont…..
  • 21.
    3)the node has2 children  replace the key of that node with the minimum element at the right sub tree  delete the minimum element  Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1or 2.
  • 22.
    Binary tree Applications File System(storing naturally hierarchical data)  Organize data(searching,insertion,deletion Binary search tree used for this purpose  Telephone dictionary  Arithmetic operations  Network routing etc