Chapter 4 (1)
Chapter 4 (1)
CONTENT:
4.1 INTRODUCTION
4.2 DEFINITION OF A BINARY TREE &
ITS MEMORY REPRESENTATION
4.3 TRAVERSING A BINARY TREE
4.4 PREORDER
4.5 INORDER
4.6 POSTORDER TRAVERSAL
4.7 THREADED BINARY TREE. 1
6
4.2 BINARY TREE & IT’S MEMORY REPRESENTATION
7
A) LINKED REPRESENTATION OF BINARY TREE
• Consider a binary tree T. T will be maintained in memory by means of a
linked representation which uses three parallel arrays, INFO, LEFT and RIGHT
and a pointer ROOT as follows. In Binary Tree each node N of T will
correspond to a location k such that
• LEFT [k] contains the location of the left child of node N.
• INFO [k] contains the data at the node N.
• RIGHT [k] contains the location of right child of node N.
• Representation of a node:
8
REPRESENTATION OF A NODE:
• In this representation of binary tree root will contain the location of the
root R of T. If any one of the
• subtree is empty, then the corresponding pointer will contain the null value if
the tree T itself is empty, the ROOT will contain the null value.
9
EXAMPLE
• Consider the binary tree T in the figure. A schematic diagram of the linked list
representation of Tappears in the following figure. Observe that each node is
pictured with its three fields, and that the empty subtree is pictured by using x
for null entries.
10
LINKED REPRESENTATION OF THE BINARY TREE
11
REPRESENTATION OF BINARY TREE IN LINKED LIST IS AS
FOLLOWS
12
B) SEQUENTIAL REPRESENTATION OF BINARY TREE
14
ITS SEQUENTIAL REPRESENTATION IS AS FOLLOW:
15
BINARY SEARCH TREE OPERATIONS-
16
1. SEARCH OPERATION-
• Rules-
• For searching a given key in the BST,
• Compare the key with the value of root node.
• If the key is present at the root node, then return the root node.
• If the key is greater than the root node value, then recur for the root node’s
right subtree.
• If the key is smaller than the root node value, then recur for the root node’s
left subtree.
17
EXAMPLE-
CONSIDER KEY = 45 HAS TO BE SEARCHED IN THE GIVEN
BST-
• We start our search from the root node 25.
• As 45 > 25, so we search in 25’s right subtree.
• As 45 < 50, so we search in 50’s left subtree.
• As 45 > 35, so we search in 35’s right subtree.
• As 45 > 44, so we search in 44’s right subtree but
44 has no subtrees.
• So, we conclude that 45 is not present in the above
BST.
18
INSERTION OPERATION
• Rules-
• The insertion of a new key always takes place as the child of some leaf node.
• For finding out the suitable leaf node,
• Search the key to be inserted from the root node till some leaf node is
reached.
• Once a leaf node is reached, insert the key as child of that leaf node.
19
EXAMPLE-
CONSIDER THE FOLLOWING EXAMPLE WHERE KEY = 40
IS INSERTED IN THE GIVEN BST-
• We start searching for value 40 from the root
node 100.
• As 40 < 100, so we search in 100’s left subtree.
• As 40 > 20, so we search in 20’s right subtree.
• As 40 > 30, so we add 40 to 30’s right subtree.
20
3. DELETION OPERATION-
21
CASE-01: DELETION OF A NODE HAVING NO CHILD
(LEAF NODE)-
• Just remove / disconnect the leaf
node that is to deleted from the tree.
• Example-
• Consider the following example
where node with value = 20 is
deleted from the BST-
22
CASE-02: DELETION OF A NODE HAVING ONLY ONE
CHILD-
23
CASE-03: DELETION OF A NODE HAVING TWO CHILD-
• Just make the child of the deleting node, the child of its
grandparent.
• Example-
• This is the most complex case. To solve it, let us see one useful BST
property first. We are going to use the idea, that the same set of
values may be represented as different binary-search trees. For
example those BSTs: 24
EXAMPLE. REMOVE 12 FROM A BST.
25
Find minimum element in the right subtree of
the node to be removed. In current example it is
19.
Remove 19 from the left subtree.
27
PreOrder (T)
If T < > Null
then print (T.data)
else print(‘empty tree’)
If T.lp < > null
then PreOrder(T.lp)
If T.rp < > null
then preorder (T.rp)
end.
28
INORDER
29
InOrder (T)
If T < > null
print (‘empty tree’)
If T.lp < > null
then InOrder(T.lp)
print (T.data)
If T.rp < > null
then InOrder (T.lp)
end.
30
POSTORDER
• A post order traversal prints the contents of a sorted tree, in post order. In
other words, the contents of the left subtree are printed first, followed by right
subtree and finally the root node. So in Figure 1.1, a post order traversal
would result in the following string: ADCIHKJF.
31
PostOrder (T)
If T = null
then print (‘empty tree’)
If T.lp < > null
then PostOrder(T.lp)
If T.rp < > null
then PostOrder(T.lp)
Print(T.data)
end.
32
4.4 THREADED BINARY TREE.
TREE TRAVERSALS (INORDER)
40
41