Documente Academic
Documente Profesional
Documente Cultură
STRUCTURES
MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com
The first subset contains a single element called the root of the tree.
The other two subsets are themselves binary trees called the left and
right sub trees.
A 0 Level 0
B 1 C 1 Level 1
D 2 E 2 F 2 Level 2
G 3 H 3 I 3 Level 3
Suppose a binary tree T of depth d then at most 2 d-1 nodes can be there in T.
So the array of size “SIZE” is used to represent the binary tree.
For depth 3,
SIZE = (23-1)= 7, so A[7] is enough to store the tree
A 0
1 B 2 C
3 D E 4 5 G 6
A[ ] A B C D E G
[0] [1] [2] [3] [4] [5] [6]
The left child of a node having index n can be obtained by (2n + 1). For example to find the
left child of C, index of C = 2
= (2*2 + 1)
=5, i.e A[5] is Left child of C, that is NULL
The Right child of a node having index n can be obtained by (2n + 2). For example to find
the Right child of B, index of B = 1
= (2*1 + 2)
=4, i.e A[4] is Left child of C, that is E
Left and Right brother (Siblings) of node having index n are at location n-1 and n+1
respectively.
B NULL
C
LChild RChild
X A
LEFT RIGHT
Y SKEWED SKEWED B
A C
Z
B C
BINARY
D E TREE F
G H I
root
B C
D E F
G H I
Recursive Definition
A
root
B C
D E F
Recursive Definition
B C
root
D E F
G H I
Recursive Definition
B C
root
D E F
G H I
B C
D E F
G H I
B C
D E F
G H I
B C
D E F
G H I
There are a number of operations that can be defined for a binary tree.
setLeft(p,x) creates the left child node of p. The child node contains the info ‘x’.
setRight(p,x) creates the right child node of p. The child node contains the info ‘x’.
The first number in the list is placed in a node that is designated as the root
of a binary tree.
Initially, both left and right sub trees of the root are empty.
We take the next number and compare it with the number placed in the root.
Otherwise, we create a new tree node and put the new number in it.
The new node is made the left child of the root node if the second number is
less than the one in the root.
The new node is made the right child if the number is greater than the one in
the root.
If the list of numbers is large and is growing, this procedure involves a large
number of comparisons.
A linked list could handle the growth but the comparisons would still be large.
14
15 14
14
15
4 14
15
14
4 15
9 14
4 15
14
4 15
7 14
4 15
14
4 15
18 14
4 15
14
4 15
9 18
3 14
4 15
9 18
14
4 15
3 9 18
5 14
4 15
3 9 18
14
4 15
3 9 18
16 14
4 15
3 9 18
14
4 15
3 9 18
7 16
4 14
4 15
3 9 18
7 16
20 14
4 15
3 9 18
7 16
14
4 15
3 9 18
7 16 20
p
17 q 14
4 15
3 9 18
7 16 20
17, 9, 14, 5
(C) GOYANI MAHESH 41
TRACE OF INSERT
p
17 14
4 q 15
3 9 18
7 16 20
17, 9, 14, 5
(C) GOYANI MAHESH 42
TRACE OF INSERT
17 14
4 p 15
q
3 9 18
7 16 20
17, 9, 14, 5
(C) GOYANI MAHESH 43
TRACE OF INSERT
17 14
4 p 15
3 9 q 18
7 16 20
17, 9, 14, 5
(C) GOYANI MAHESH 44
TRACE OF INSERT
17 14
4 15
3 9 p 18
q
7 16 20
17, 9, 14, 5
(C) GOYANI MAHESH 45
TRACE OF INSERT
17 14
4 15
3 9 p 18
7 q 16 20
17, 9, 14, 5
(C) GOYANI MAHESH 46
TRACE OF INSERT
17 14
4 15
3 9 18
7 p 16 20
q
5
17, 9, 14, 5
(C) GOYANI MAHESH 47
TRACE OF INSERT
17 14
4 15
3 9 18
7 p 16 20
5 q
17, 9, 14, 5
(C) GOYANI MAHESH 48
TRACE OF INSERT
14
4 15
3 9 18
7 p 16 20
5 node 17
Preorder: (N,L,R)
Inorder: (L,N,R)
Postorder: (L,R,N)
left right
L subtree subtree R
2 8
1 4 7 9
3 5
14
4 15
3 9 18
7 16 20
5 17
Inorder: 3 4 5 7 9 14 15 16 17 18 20
1
Make Money Fast!
2 5 9
1. Motivations 2. Methods References
6 7 8
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery
14
4 15
3 9 18
7 16 20
5 17
Preorder: 14 4 3 9 7 5 15 18 16 17 20
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
14
4 15
3 9 18
7 16 20
5 17
Postorder: 3 5 7 9 4 17 16 20 18 15 14
Binary Search Tree (BST) is a binary tree, which is either empty or satisfies
following properties :
Every node has a value and no two nodes have the same property
If there exists a LEFT CHILD then its value is less than the value of
ROOT.
If there exists a RIGHT CHILD then its value is greater than the value
of ROOT.
50
25 75
20 40 80
30 77 90