Skip to content

Commit 152ac22

Browse files
Add files via upload
1 parent 829e55d commit 152ac22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3436
-0
lines changed

Diff for: AP-HW3/Q1/Q1.html

+587
Large diffs are not rendered by default.

Diff for: AP-HW3/Q1/asxz.html

+554
Large diffs are not rendered by default.

Diff for: AP-HW3/Q1/jquery-1.9.1.min.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: AP-HW3/Q2/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CXX = g++
2+
CXXFLAGS = -std=c++17 -Wall -c
3+
LXXFLAGS = -std=c++17
4+
OBJECTS = main.o maxheap.o
5+
TARGET = main
6+
7+
$(TARGET): $(OBJECTS)
8+
$(CXX) $(LXXFLAGS) $(OBJECTS) -o $(TARGET)
9+
main.o: main.cpp
10+
$(CXX) $(CXXFLAGS) main.cpp
11+
maxheap.o: maxheap.cpp maxheap.h
12+
$(CXX) $(CXXFLAGS) maxheap.cpp
13+
clean:
14+
rm -f $(TARGET) $(OBJECTS)
15+
16+

Diff for: AP-HW3/Q2/main

18.8 KB
Binary file not shown.

Diff for: AP-HW3/Q2/main.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include"maxheap.h"
2+
#include <iostream>
3+
4+
int main()
5+
{
6+
Maxheap h1{};
7+
h1.add(25);
8+
h1.add(32);
9+
h1.add(17);
10+
h1.add(23);
11+
h1.add(101);
12+
13+
std::cout << h1 << std::endl;
14+
/*
15+
101,
16+
32, 17,
17+
23, 25
18+
*/
19+
std::cout << h1.parent(2) << std::endl; // 101
20+
std::cout << h1.LeftChild(0) << std::endl; // 32
21+
std::cout << h1.RightChild(0) << std::endl; // 17
22+
23+
24+
int arr[7] {23, 1, 7, 52, 11, 10, 75};
25+
Maxheap h2{arr, 7};
26+
//h2.add(14);
27+
28+
std::cout << h2 << std::endl;
29+
std::cout << h2.getHeight() << std::endl; // 2
30+
31+
32+
Maxheap h3{h2};
33+
34+
h2.Heapsort();
35+
h2.printArray(); // 75, 52, 23, 11, 10, 7, 1
36+
37+
38+
std::cout << h3.max(); //75
39+
40+
h3[0] = h3[0] - 25; //////22222
41+
42+
std::cout << h3.max(); //52
43+
h3.Delete();
44+
std::cout << h3.max(); //50
45+
46+
47+
48+
return 0;
49+
}

Diff for: AP-HW3/Q2/main.o

6.41 KB
Binary file not shown.

Diff for: AP-HW3/Q2/maxheap.cpp

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#include "maxheap.h"
2+
#include <cmath>
3+
4+
Maxheap::Maxheap(int cap)
5+
{
6+
heap_size = 0;
7+
capacity = cap;
8+
harr = new int[cap];
9+
}
10+
//Defaule Constructor
11+
Maxheap::Maxheap():Maxheap(10){
12+
std::cout<<"Defaule Constructor"<<std::endl;
13+
}
14+
//Copy Constructor
15+
Maxheap::Maxheap(const Maxheap& h){
16+
std::cout<<"Copy Constructor"<<std::endl;
17+
heap_size=h.heap_size;
18+
capacity=h.capacity;
19+
harr=new int[capacity];
20+
for (int i = 0; i < heap_size; ++i)
21+
{
22+
harr[i]=h.harr[i];
23+
24+
25+
}
26+
27+
}
28+
29+
Maxheap::Maxheap(int* arr,int num){
30+
heap_size=num;
31+
capacity=num;
32+
harr=new int[capacity];
33+
int i{};
34+
for (int j = 0; j < heap_size; ++j)
35+
{
36+
harr[j]=arr[j];
37+
i=j;
38+
while (i != 0 && harr[Parent(i)] < harr[i])
39+
{
40+
swap(&harr[i], &harr[Parent(i)]);
41+
i = Parent(i);
42+
}
43+
44+
}
45+
46+
47+
}
48+
49+
// add a new key 'k'
50+
void Maxheap::add(int k)
51+
{
52+
capacity++;
53+
if (heap_size == capacity)
54+
{
55+
std::cout << "\nOverflow: Could not insertKey\n";
56+
return;
57+
}
58+
59+
// First insert the new key at the end
60+
heap_size++;
61+
int i = heap_size - 1;
62+
harr[i] = k;
63+
64+
// Fix the min heap property if it is violated
65+
while (i != 0 && harr[Parent(i)] < harr[i])
66+
{
67+
swap(&harr[i], &harr[Parent(i)]);
68+
i = Parent(i);
69+
}
70+
}
71+
72+
// Decreases value of key at index 'i' to new_val. It is assumed that
73+
// new_val is smaller than harr[i].
74+
void Maxheap::decreaseKey(int i, int new_val)
75+
{
76+
harr[i] = new_val;
77+
while (i != 0 && harr[Parent(i)] > harr[i])
78+
{
79+
swap(&harr[i], &harr[Parent(i)]);
80+
i = Parent(i);
81+
}
82+
}
83+
84+
// Method to remove maximum element (or root) from min heap
85+
int Maxheap::extractMax()
86+
{
87+
if (heap_size <= 0)
88+
return INT_MIN;
89+
if (heap_size == 1)
90+
{
91+
heap_size--;
92+
return harr[0];
93+
}
94+
95+
// Store the maximum value, and remove it from heap
96+
int root = harr[0];
97+
harr[0] = harr[heap_size-1];
98+
heap_size--;
99+
Maxheapify(0);
100+
101+
return root;
102+
}
103+
104+
105+
// This function deletes key at index i. It first reduced value to minus
106+
// infinite, then calls extractMin()
107+
void Maxheap::Delete()
108+
{
109+
//decreaseKey(0, INT_MAX);
110+
//extractMax();
111+
if (heap_size <= 0)
112+
return;
113+
if (heap_size == 1)
114+
{
115+
heap_size--;
116+
return;
117+
}
118+
119+
//remove maximuu from heap
120+
harr[0] = harr[heap_size-1];
121+
heap_size--;
122+
Maxheapify(0);
123+
}
124+
125+
126+
127+
// A recursive method to heapify a subtree with the root at given index
128+
// This method assumes that the subtrees are already heapified
129+
void Maxheap::Maxheapify(int i)
130+
{
131+
//std::cout<<"Heapify"<<std::endl;
132+
int l = left(i);
133+
int r = right(i);
134+
int smallest = i;
135+
if (l < heap_size && harr[l] > harr[i])
136+
smallest = l;
137+
if (r < heap_size && harr[r] > harr[smallest])
138+
smallest = r;
139+
if (smallest != i)
140+
{
141+
swap(&harr[i], &harr[smallest]);
142+
Maxheapify(smallest);
143+
}
144+
}
145+
146+
// A utility function to swap two elements
147+
void swap(int *x, int *y)
148+
{
149+
int temp = *x;
150+
*x = *y;
151+
*y = temp;
152+
}
153+
154+
std::ostream& operator<<(std::ostream& os,const Maxheap& h){
155+
int a{};
156+
int b{};
157+
for (int i = 0; i < h.heap_size; ++i)
158+
{
159+
a++;
160+
os<<h.harr[i]<<" ";
161+
b=std::log(a+1) / std::log(2);
162+
if(b == ( std::ceil( std::log(a+1)/std::log(2) )))
163+
if(a !=(h.heap_size))
164+
std::cout<<std::endl;
165+
166+
}
167+
168+
return os;
169+
}
170+
171+
int Maxheap::getHeight(){
172+
return (std::ceil(std::log(heap_size+1)/log(2))-1);
173+
}
174+
175+
void Maxheap::printArray(){
176+
for (int i = 0; i < heap_size; ++i)
177+
{
178+
std::cout<<harr[i]<<" ";
179+
}
180+
std::cout<<std::endl;
181+
}
182+
183+
184+
void Maxheap::Heapsort(){
185+
int* arr_new=new int[heap_size];
186+
int temp= heap_size;
187+
for (int i = 0; i < temp; ++i)
188+
{
189+
arr_new[i]=extractMax();
190+
191+
}
192+
harr=arr_new;
193+
arr_new=nullptr;
194+
heap_size=temp;
195+
this->printArray();
196+
}
197+
198+
199+
200+
201+
202+
int Maxheap::operator+(int a){
203+
return 1;
204+
205+
}
206+
int Maxheap::operator-(int a){
207+
return 1;
208+
209+
}
210+
211+
212+

Diff for: AP-HW3/Q2/maxheap.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef MAXHEAP_H
2+
#define MAXHEAP_H
3+
4+
#include <iostream>
5+
#include <climits>
6+
#include <vector>
7+
8+
class Maxheap;
9+
std::ostream& operator<<(std::ostream& os,const Maxheap& h);
10+
11+
12+
void swap(int *x, int *y);
13+
14+
class Maxheap
15+
{
16+
/* int *harr; // pointer to array of elements in heap
17+
int capacity; // maximum possible size of max heap
18+
int heap_size; // Current number of elements in max heap*/
19+
public:
20+
21+
int *harr; // pointer to array of elements in heap
22+
int capacity; // maximum possible size of max heap
23+
int heap_size; // Current number of elements in max heap
24+
25+
// Constructor
26+
Maxheap(int capacity);
27+
Maxheap();
28+
Maxheap(const Maxheap& );
29+
Maxheap(int*,int num);
30+
31+
// to heapify a subtree with the root at given index
32+
void Maxheapify(int );
33+
int parent(int i){return harr[(i-1)/2];} //small
34+
int Parent(int i) { return (i-1)/2; } //capital
35+
36+
// to get index of left child of node at index i
37+
int LeftChild(int i){ return harr[(2*(i) + 1)];}
38+
int left(int i) { return (2*i + 1); }
39+
40+
// to get index of right child of node at index i
41+
int RightChild(int i){ return harr[(2*(i) + 2)];}
42+
int right(int i) { return (2*i + 2); }
43+
44+
// to extract the root which is the minimum element
45+
int extractMax();
46+
47+
// Decreases key value of key at index i to new_val
48+
void decreaseKey(int i, int new_val);
49+
50+
// Returns the minimum key (key at root) from min heap
51+
int getMax() { return harr[0]; }
52+
53+
// Deletes a key stored at index i
54+
void Delete();
55+
//int Delete();
56+
57+
// add a new key 'k'
58+
void add(int k);
59+
int getHeight();
60+
void printArray();
61+
int max(){return harr[0];}
62+
void Heapsort();
63+
64+
//operator
65+
int& operator[](const int& i){return harr[i]; }
66+
int operator+(int);
67+
int operator-(int);
68+
69+
70+
71+
72+
73+
};
74+
75+
#endif

Diff for: AP-HW3/Q2/maxheap.o

9.93 KB
Binary file not shown.

Diff for: AP-HW3/Q3/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CXX = g++
2+
CXXFLAGS = -std=c++17 -Wall -c
3+
LXXFLAGS = -std=c++17
4+
OBJECTS = main.o vec.o
5+
TARGET = main
6+
7+
$(TARGET): $(OBJECTS)
8+
$(CXX) $(LXXFLAGS) $(OBJECTS) -o $(TARGET)
9+
main.o: main.cpp
10+
$(CXX) $(CXXFLAGS) main.cpp
11+
vec.o: vec.cpp vec.h
12+
$(CXX) $(CXXFLAGS) vec.cpp
13+
clean:
14+
rm -f $(TARGET) $(OBJECTS)
15+
16+

Diff for: AP-HW3/Q3/main

18.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)