You are on page 1of 39

BINARY TREE

DR. AMRITA SARKAR


BINARY TREE
LEVEL-ORDER TRAVERSAL
In level-order traversal, all the nodes at a level are accessed before going to the
next level. This algorithm is also called as the breadth-first traversal algorithm.
Consider the trees given in Figure and note the level order of these trees.
CONSTRUCTING A BINARY TREE FROM TRAVERSAL RESULTS

• We can construct a binary tree if we are given at least two traversal results.
The first traversal must be the in-order traversal and the second can be either
pre-order or post-order traversal.
• The in-order traversal result will be used to determine the left and the right
child nodes, and the pre-order/post-order can be used to determine the root
node. For example, consider the traversal results given below:

• In–order Traversal: D B E A F C G Pre–order Traversal: A B D E C F G


CONSTRUCTING A BINARY TREE FROM TRAVERSAL
RESULTS
• Follow the steps given below to construct the tree:
• Step 1: Use the pre-order sequence to determine the root node of the tree. The first element
would be the root node.
• Step 2: Elements on the left side of the root node in the in-order traversal sequence form the
left sub-tree of the root node. Similarly, elements on the right side of the root node in the in-
order traversal sequence form the right sub-tree of the root node.
• Step 3 Recursively select each element from pre-order traversal sequence and create its left
and right sub-trees from the in-order traversal sequence. Look at Fig. which constructs the
tree from its traversal results. Now consider the in-order traversal and post-order traversal
sequences of a given binary tree. Before constructing the binary tree, remember that in post-
order traversal the root node is the last node. Rest of the steps will be the same as
mentioned above
CONSTRUCTING A BINARY TREE FROM TRAVERSAL
RESULTS
BINARY SEARCH TREE
BINARY SEARCH TREE

• A binary search tree, also known as an ordered binary tree, is a variant of


binary trees in which the nodes are arranged in an order.
• In a binary search tree, all the nodes in the left sub-tree have a value less than
that of the root node.
• Correspondingly, all the nodes in the right sub-tree have a value either equal
to or greater than the root node.
BINARY SEARCH TREE
PROPERTIES OF BINARY SEARCH TREE

• To summarize, a binary search tree is a binary tree with the following


properties:
• The left sub-tree of a node N contains values that are less than N’s value.
• The right sub-tree of a node N contains values that are greater than N’s value.
• Both the left and the right binary trees also satisfy these properties and, thus,
are binary search trees.
ADVATAGES OF BINARY SEARCH TREE

• The average running time of a search operation is O(log2n), as at every step,


we eliminate half of the sub-tree from the search process.
• Nodes in a binary search tree are ordered, the time needed to search an
element in the tree is greatly reduced.
• Binary search trees also speed up the insertion and deletion operations. The
tree has a speed advantage when the data in the structure changes rapidly.
LEFT SKEWED, AND RIGHT SKEWED BINARY SEARCH TREE

(a) Left skewed, and (b)right skewed binary search trees


BINARY SEARCH TREE

State whether the binary trees in Fig. below are binary search trees or not.
BINARY SEARCH TREE
Create a binary search tree using the following data elements:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
BINARY SEARCH TREE
OPERATIONS ON BINARY SEARCH TREE
• Searching for a Node in a Binary Search Tree
• Inserting a New Node in a Binary Search Tree
• Deleting a Node from a Binary Search Tree
• Deleting a Node that has No Children
• Deleting a Node with One Child
• Deleting a Node with Two Children
• Determining the Height of a Binary Search Tree
• Determining the Number of Nodes
• Determining the Number of Internal Nodes
SEARCHING FOR A NODE IN A BINARY SEARCH TREE

• Searching a node with value 12 in the given binary search tree


SEARCHING FOR A NODE IN A
BINARY SEARCH TREE
• Searching a node with value 67 in the
given binary search tree
SEARCHING FOR A NODE IN A BINARY SEARCH TREE

• Searching a node with the


value 40in the given binary
search tree
SEARCHING FOR A NODE IN A BINARY SEARCH TREE

• Algorithm to search
for a given value in a
binary search tree
INSERTING A NEW NODE IN A BINARY SEARCH TREE

• Inserting nodes with values


12 and 55 in the given binary
search tree
INSERTING A NEW NODE IN A BINARY SEARCH TREE

• Algorithm to insert a given


value in a binary search tree
DELETING A NODE THAT HAS NO CHILDREN

• Deleting node 78 from the given binary search tree


DELETING A NODE WITH ONE CHILD

• Deleting node 54 from the given binary search tree


DELETING A NODE WITH TWO CHILDREN

• Deleting node 56 from the given binary search tree


DELETING A NODE WITH TWO CHILDREN

• Deleting node 56 from the given binary search tree


DELETING A NODE FROM A BINARY SEARCH TREE

• Algorithm to delete a node


from a binary search tree
DETERMINING THE HEIGHT OF A BINARY SEARCH TREE

• Calculate the height of the left sub-tree and the right


sub-tree.
• Whichever height is greater, 1 is added to it. For
example, if the height of the left sub-tree is greater
than that of the right sub-tree, then 1 is added to
the left sub-tree, else 1 is added to the right sub-
tree.
Binary search tree with height = 3
• In this Fig. Since the height of the right sub-tree is
greater than the height of the left sub-tree, the
height of the tree = height (right sub-tree) + 1= 2 + 1
= 3.
DETERMINING THE HEIGHT OF A BINARY SEARCH TREE

• Algorithm to determine
the height of a binary
search tree
DETERMINING THE NUMBER OF NODES

• To calculate the total number of elements/nodes in the tree,


we count the number of nodes in the left sub-tree and the
right sub-tree.
• Number of nodes = total Nodes(left sub–tree) + total
Nodes(right sub–tree) + 1
• Consider the tree given in Fig. shown here. The total number
of nodes in the tree can be calculated as:
• Total nodes of left sub–tree = 1
• Total nodes of right sub–tree = 5
• Total nodes of tree = (1 + 5) + 1= 7
DETERMINING THE NUMBER OF NODES

• Number of internal nodes = total Internal Nodes(left sub–tree) + total


Internal Nodes(right sub–tree) + 1
DETERMINING THE NUMBER OF INTERNAL NODES

• Number of internal nodes = total Internal Nodes(left sub–


tree) + total Internal Nodes(right sub–tree) + 1
• The total number of internal nodes in the tree can be
calculated as:
• Total internal nodes of left sub–tree = 0
• Total internal nodes of right sub–tree = 3
• Total internal nodes of tree = (0 + 3) + 1= 4
DETERMINING THE NUMBER OF INTERNAL NODES

• Algorithm to calculate the total


number of internal nodes in a
binary search tree
DETERMINING THE NUMBER OF EXTERNAL NODES

• Number of external nodes = totalExternalNodes(left sub–


tree) + totalExternalNodes (right sub–tree)
• The total number of external nodes in the given tree can
be calculated as:
• Total external nodes of left sub–tree = 1
• Total external nodes of left sub–tree = 2
• Total external nodes of tree = 1 + 2 = 3
DETERMINING THE NUMBER OF EXTERNAL NODES

• Algorithm to calculate
the total number of
external nodes in a
binary search tree
FINDING THE SMALLEST AND LARGEST NODE IN A BINARY
SEARCH TREE
FINDING THE SMALLEST NODE IN A BINARY
SEARCH TREE
• Algorithm to find the
smallest node in a
binary search tree
search tree
FINDING THE LARGEST NODE IN A BINARY
SEARCH TREE
• Algorithm to find the largest
node in a binary search tree
THANK YOU

You might also like