Professional Documents
Culture Documents
Introduction
Arrays, linked list ,stacks and queues are
used to represent the linear and tabular data.
These structure are not suitable for
representing hierarchical data.
In hierarchical data we have
◦ ancestor, descendant
◦ Superior, subordinate etc.
TREE DEFINED
A tree T is a finite non empty set of elements.
One of these elements is called the root, and
the remaining elements, if any, are portioned
into trees, which are called the sub trees of T.
Introduction to Tree
Fundamental data storage structures used in
programming.
Combines advantages of an ordered array
node
g e
e d
Tree Terminology
Root: node without parent
(A) • Subtree: tree consisting
Internal node: node with at of a node and its
least one child (A, B, C, F) descendants
External node (a.k.a. leaf ):
A Level 0
node without children (E, I, J,
K, G, H, D)
Ancestors of a node: parent,
grandparent etc. B C D
height of a tree is the
number of edges on the
E F G H
longest simple downward
path from the root to a leaf
Descendant of a node: child, subtree
I J K
grandchild etc.
Degree of an element: no. of
children it has
Height of trees??
+
2 - 3 b
a 1
Compiling arithmetic expressions
We can represent an arithmetic expression such as
(A + B) * (C + D) * 2 – X / Y
as a binary tree, in which the leaves are constants
or variables and the nodes are operations:
–
* /
* 2 X Y
+ +
A B C D
Binary Search Trees
Binary Search Trees:
A binary search tree T is a binary tree that may be
empty. A non empty binary search tree satisfies
the following properties:
1. Every element has a key (or value) and no two
elements have the same key i.e. all keys are
unique.
2. The keys, if any, in the left subtree of the root are
smaller than the key in the node.
3. The keys, if any, in the right subtree of the root
are larger than the key in the node.
4. The left and right sub trees of the root are also
binary search trees.
BST
70 40 70
60 80 10 55 80
30 62 75 92 5 12 75 92
1 2 3 4 5 6 7
70 60 80 30 62 75 92
72
80
85
Fig. (d)
1 2 3 4 5 6 7 8 9 10 11 12 1314 15
70 72 80 85
Disadvantages of array
representation
This scheme of representation leads to
wastage of space when binary tree is skewed
or number of elements are small as compared
to its height.
Array representation is useful only when the
70
60 80
X 30 X X 62 X X 75 X X 92 X
40
10 X 55 X
X 5 X X 12 X
X 70
80
X 75 X X 92 X
X 70
X 72
X 80
X 85 X
Linked Representation
In linked representation, each element is
represented by a node that has exactly two link
fields.
Field names are left and right.
Each node has data field called info.
1: y ← x
2: while y != nil do
3: if key[y] == k then return y
4: else if key[y] < k then y ← right[y]
5: else y ← left[y]
6: return (“NOT FOUND”)
Example
Traversing a Binary Tree
Preorder (NLR Traversal)
◦ Process the root R.
◦ Traverse the left subtree of R in preorder.
◦ Traverse the right subtree of R in preorder.
Inorder (LNR Traversal)
◦ Traverse the left subtree of R in inorder.
◦ Process the root R.
◦ Traverse the right subtree of R in inorder.
Traversing a Binary Tree(contd..)
Postorder (LRN Traversal)
◦ Traverse the left subtree of R in postorder.
◦ Traverse the right subtree of R in postorder.
◦ Process the root R.
Department of CSE 34
Preorder, Postorder and Inorder
Traversal Example
PreOrder - 8, 5, 9, 7, 1,
12, 2, 4, 11, 3
InOrder - 9, 5, 1, 7, 2,
12, 8, 4, 3, 11
PostOrder - 9, 1, 2, 12,
7, 5, 3, 11, 4, 8
LevelOrder - 8, 5, 4, 9,
7, 11, 1, 12, 3, 2
36
BST Inorder Traversal
56
26 200
18 28 190 213
12 24 27
Sorted Order:
12,18,24,26,27,28,56,190,200,213
37
Example
Pre-order: A B D E G H C F
In order: D B G E H A C F
Pre- Order: A B D E G H C F
In- Order: B D G E H A C F
RTA
LTA
D B C F
G E H
Pre- Order: B D E G H
In- Order: D B G E H
Example
Pre- Order: B D E G H
In- Order: D B G E H
B
RTA
C F
D
G RTB
E H
Pre- Order: E G H
In- Order: G E H
Example
Pre- Order: E G H
In- Order: G E H
B
RTA
C F
D E
G H
Pre- Order: C F
In- Order: C F
Example
Pre- Order: C F
In- Order: C F
B
C
D E F
G H
Algo for Pre-order Traversal
Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
Tree-Minimum(xx))
Tree-Minimum( Tree-Maximum(xx))
Tree-Maximum(
1. while left
1. while left[[xx]] NIL
NIL 1. while right
1. while right[[xx]] NIL
NIL
2.
2. do do xx left
left[[xx]] 2. do xx
2. do right
right[[xx]]
3. return xx
3. return 3. return xx
3. return
Successor
Successor of node x is the node y such that
key[y] is the smallest key greater than key[x].
The successor of the largest key is NIL.
Search consists of two cases.
26 200
Code for predecessor is symmetric.
18 28 190 213
Running time: O(h)
12 24 27
Deletion in BST
The overall strategy for deleting a node z from a binary
search tree T has three basic cases: