You are on page 1of 37

Binary Tree Representation

Table Of Contents
• Binary Tree
• Binary Tree Representation
• Components of Binary Tree
• Basic Operations on Binary Tree
• Binary Search Tree
• Properties of BST
• Representation of BST
• Operations on BST
• Summary
Binary Tree
• Tree data structure
• Each node has at most 2 children
• Children are typically named as left and right
child.
Binary Tree Representation
• Represented by a pointer to the topmost
node (commonly known as the “root”)
• If the tree is empty, then the value of the
root is NULL.
• Edge connects one node to the other
Binary Tree Representation
Components of Binary Tree
Each node of a Binary Tree contains the
following parts:
• Data
• Pointer to left child
• Pointer to right child
Basic Operations On Binary Tree
• Traversing the tree
• Inserting an element.
• Removing an element.
• Searching for an element.
Basic Operations On Binary Tree(Cont.)
Traversing the tree
• Process by which we can visit and access each and
every element present in any data structure like an
array, linked list, or tree.
• Binary tree traversal can be done in following ways
 Inorder
 Preorder
 Postorder
Basic Operations On Binary Tree(Cont.)
In-Order
• Traverse the left subtree, i.e., call
Inorder(left->subtree)
• Visit the root.
• Traverse the right subtree, i.e., call
Inorder(right->subtree)
Basic Operations On Binary Tree(Cont.)
Pre-Order
• Visit the root.
• Traverse the left subtree, i.e., call
Preorder(left->subtree)
• Traverse the right subtree, i.e., call
Preorder(right->subtree) 
Basic Operations On Binary Tree(Cont.)
Post-Order
• Traverse the left subtree, i.e., call
Postorder(left->subtree)
• Traverse the right subtree, i.e., call
Postorder(right->subtree)
• Visit the root
Basic Operations On Binary Tree(Cont.)
Basic Operations On Binary Tree(Cont.)
Inserting An Element
• Insert function is used to add a new element
in a binary search
tree at appropriate
location.
Basic Operations On Binary Tree(Cont.)
Inserting An Element(Algo.)
• Step 1: IF TREE = NULL
    Allocate memory for TREE
   SET TREE -> DATA = ITEM
  SET TREE -> LEFT = TREE -> RIGHT = NULL
  ELSE
   IF ITEM < TREE -> DATA
    Insert(TREE -> LEFT, ITEM)
  ELSE
   Insert(TREE -> RIGHT, ITEM)
  [END OF IF]
  [END OF IF]
• Step 2: END
Basic Operations On Binary Tree(Cont.)
Deleting an Element
• Delete function is used to delete the
specified node from a binary search tree.
• Deletion in binary tree has three cases
Basic Operations On Binary Tree(Cont.)
Case 1:The node to be deleted is a leaf node
Basic Operations On Binary Tree(Cont.)
Case 2:The node to be deleted has only one
child.
Basic Operations On Binary Tree(Cont.)
Case 3:The node to be deleted has two
children.
Basic Operations On Binary Tree(Cont.)
Deleting An Element(Algo.)
Delete (TREE, ITEM)
• Step 1: IF TREE = NULL
   Write "item not found in the tree" ELSE IF ITEM < TREE -> DATA
  Delete(TREE->LEFT, ITEM)
  ELSE IF ITEM > TREE -> DATA
   Delete(TREE -> RIGHT, ITEM)
  ELSE IF TREE -> LEFT AND TREE -> RIGHT
  SET TEMP = findLargestNode(TREE -> LEFT)
  SET TREE -> DATA = TEMP -> DATA
   Delete(TREE -> LEFT, TEMP -> DATA)
  ELSE
   SET TEMP = TREE
   IF TREE -> LEFT = NULL AND TREE -> RIGHT = NULL
   SET TREE = NULL
  ELSE IF TREE -> LEFT != NULL
  SET TREE = TREE -> LEFT
  ELSE
    SET TREE = TREE -> RIGHT
  [END OF IF]
  FREE TEMP
[END OF IF]
• Step 2: END
Basic Operations On Binary Tree(Cont.)
Searching in Binary Tree
• Search and check if the given node exists in
the binary tree or not.
• Can be done using any type of tree
traversals.
Binary Search Tree
Binary search tree is a data structure that quickly
allows us to maintain a sorted list of numbers.
• It is called a binary tree because each tree
node has a maximum of two children.
• It is called a search tree because it can be
used to search for the presence of a number
in O(log(n)) time.
Properties of Binary Search Tree
A node-based BST has following properties:
• The left subtree of a node contains only nodes with keys lesser
than the node’s key.
• The right subtree of a node contains only
nodes with keys greater than the node’s
key.
• The left and right subtree each must also
be a binary search tree.
Representation of BST
A BST can be represented in such a way that it
must follow all the properties that are
summarized as:
left_subtree (keys) < node (key) ≤ right_subtree (keys)
Operations in BST
BST primarily offers the following three types
of operations:
• Search: searches the element from the
binary tree
• Insert: adds an element to the binary tree
• Delete: delete the element from a binary
tree
Operations in BST
Operations in BST
Algo. For Searching
search(element, root)
if !root
return -1
if root.value == element
return 1
if root.value < element
search(element, root.right)
else
search(element, root.left)
Operations in BST
Operations in BST
Algo. For Insertion
insert (element, root)
Node x = root
Node y = NULL
while x:
y=x
if x.value < element.value
x = x.right
else
x = x.left
if y.value < element
y.right = element
else
y.left = element
Operations in BST
Operations in BST
Operations in BST
Operations in BST
Operations in BST
Algo. For Deletion
delete (value, root):
Node x = root
Node y = NULL
# searching the node
while x:
y=x
if x.value < value
x = x.right
else if x.value > value
x = x.left
else if value == x
break
Operations in BST
Algo. For Deletion
# if the node is not null, then replace it with successor
if y.left or y.right:
newNode = GetInOrderSuccessor(y)
root.value = newNode.value
# After copying the value of successor to the root #we're deleting the
successor
free(newNode)
else
free(y)
SUMMARY
THANK YOU

You might also like