-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvJava_24_Heaps.java
More file actions
56 lines (40 loc) · 1.96 KB
/
AdvJava_24_Heaps.java
File metadata and controls
56 lines (40 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package AdvancedJava;
public class AdvJava_24_Heaps {
public static void main(String[] args) {
/* Trees are distributed in
1. Root
2. Leaves where,
- Each root is connected to its child or leaves where, the child have no relationship with each others
- Types of Heaps:
1. Min-Heap: The root value is smaller compared to its children's value.
- Children could be anything, but greater than its root.
- Note: The same structure is followed throughout the branches ahead.
Ex. 20 20
| | == | |
100 105 100 105
- But for a third branch,
20
| |
105 90
|
100 {100 Cannot be attached to 105 as 100<105 and the min-head condition becomes false!
2. Max-Heap: Same as min heap but the value of root is greatest this time!
Ex. 10
/ \
8 5
/ \ / \
5 4 3 1 Note: 5 could also be attached to the 5 at second root as max condition is : >= */
/* If we number max heap according to there order:
10 = 1
8 = 2
5 = 3
5(under 8) = 4
4 = 5
3 = 6 and
1 = 7
can be aligned as [ 1 2 3 4 5 6 7 ]
= [ 10 8 5 5 4 3 1 ] and can be
!! Now to access child of a tree -> { Parent's(index) * 2 } and add as many child's you want
:: parent of a tree -> {Child's(index) / 2 } */
}
}