-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompleteTree.java
130 lines (123 loc) · 2.33 KB
/
CompleteTree.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class CompleteTree{
int arr[];
int Maxsize,size;
CompleteTree(int maxsize){
size=0;
this.
Maxsize=maxsize;
arr = new int [maxsize];
}
public void insert(int element){
if(size==Maxsize)
System.out.println("Over-flow");
else
{
size++;
arr[size]=element;
Siftup(size);
}
}
public int Parent(int index){
return index/2;
}
public int leftchild(int index)
{
return 2*index;
}
public int rightchild(int index)
{
return 2*index+1;
}
public void Siftup(int index){
int temp=0;
while(index>1 && arr[index/2]<arr[index])
{
temp = arr[index/2];
arr[index/2]=arr[index];
arr[index]=temp;
index = index/2;
}
}
public void Siftdown(int index){
int temp=0;
int maxIndex = index;
int l = (2*index);
if (l <= size && arr[l] > arr[maxIndex])
{
maxIndex =l;
}
int r = (2*index)+1;
if (r <= size && arr[r] > arr[maxIndex])
maxIndex =r;
if (index != maxIndex)
{
temp = arr[index];
arr[index] = arr[maxIndex];
arr[maxIndex]=temp;
Siftdown(maxIndex);
}
}
public int ExtractMax(){
int result = arr[1];
arr[1]=arr[size];
size--;
Siftdown(1);
return result;
}
public void Remove(int index){
arr[index]=Integer.MAX_VALUE;
Siftup(index);
ExtractMax();
}
public void changeP(int index , int data){
int oldp= arr[index];
arr[index] = data;
if (data > oldp)
{
Siftup(index);
}
else
Siftdown(index);
}
public void display(){
for(int i = 1 ; i <=size ;i++)
System.out.print(" "+arr[i]);
}
public void inorder(int index){
if(index>size)
return ;
inorder(leftchild(index));
System.out.print(" "+arr[index]);
inorder(rightchild(index));
}
public void preorder(int index){
if(index>size)
return ;
System.out.print(" "+arr[index]);
preorder(leftchild(index));
preorder(rightchild(index));
}
public void postorder(int index){
if(index>size)
return ;
postorder(leftchild(index));
postorder(rightchild(index));
System.out.print(" "+arr[index]);
}
public static void main(String args[]){
CompleteTree a = new CompleteTree(13);
a.insert(42);
a.insert(29);
a.insert(18);
a.insert(14);
a.insert(7);
a.insert(18);
a.insert(12);
a.insert(11);
a.insert(5);
System.out.println();
a.display();
System.out.println();
a.inorder(4);
}
}