SlideShare a Scribd company logo
1 of 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InsertHeap
{
class Heap
{
private int max_size;
private int size;
private string[] h;
public Heap(int m)
{
size = 0;
max_size = m+1;
h = new string[max_size];
}
public void insertItem(string item) //max heap
{
size++;
h[size] = item;
int current = size;
int parent = current / 2;
while (parent > 0)
{
if (string.Compare(h[parent], h[current]) < 0) //parent is smaller than
current
{
string temp = h[current];
h[current] = h[parent];
h[parent] = temp;
}
parent = parent / 2;
current = current / 2;
}
}
public void Extract_Max()
{
if (size < 1)
{
Console.WriteLine("Error Message");
}
int parent = 1; // start with root at index 1
string max = h[parent]; // extract max, the root is the biggest
h[parent] = h[size]; //swap
h[size] = max; //store the extracted max to the last position of array
size--; //decrease size by 1
int leftChild = 2 * parent; //left child
while (leftChild < size ) //if left child is less than size
{
int rightChild = leftChild + 1; //if right child exist
if (rightChild <= size)
{
if (string.Compare(h[leftChild], h[rightChild]) <= 0) // left child
is smaller than right child
{
string temp = h[rightChild];
h[rightChild] = h[parent];
h[parent] = temp;
parent = rightChild; //replace the rightchild with parent node
int a = 2 * parent;
leftChild = a;
}
else
{
string temp = h[leftChild];
h[leftChild] = h[parent];
h[parent] = temp;
parent = leftChild; //replace the leftchild with parent node
int a = 2 * parent;
leftChild = a;
}
}
else if (leftChild <= size && rightChild > size)
{
if (string.Compare(h[parent], h[leftChild]) <= 0)
{
string t = h[leftChild];
h[leftChild] = h[parent];
h[parent] = t;
parent = leftChild; //replace the leftchild with parent node
int a = 2 * parent;
leftChild = a;
}
}
}
}
public void printHeap()
{
for (int i = 1; i < h.Length; i++)
{
Console.Write(h[i] +" ");
}
}
}
}

More Related Content

What's hot

Sanjar Akhmedov - Joining Infinity – Windowless Stream Processing with Flink
Sanjar Akhmedov - Joining Infinity – Windowless Stream Processing with FlinkSanjar Akhmedov - Joining Infinity – Windowless Stream Processing with Flink
Sanjar Akhmedov - Joining Infinity – Windowless Stream Processing with FlinkFlink Forward
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
The Weather of the Century
The Weather of the CenturyThe Weather of the Century
The Weather of the CenturyMongoDB
 
The Weather of the Century Part 3: Visualization
The Weather of the Century Part 3: VisualizationThe Weather of the Century Part 3: Visualization
The Weather of the Century Part 3: VisualizationMongoDB
 
Apache avro and overview hadoop tools
Apache avro and overview hadoop toolsApache avro and overview hadoop tools
Apache avro and overview hadoop toolsalireza alikhani
 
Elk with Openstack
Elk with OpenstackElk with Openstack
Elk with OpenstackArun prasath
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Creating a Name seperator Custom Control using C#
Creating a Name seperator Custom Control using C#Creating a Name seperator Custom Control using C#
Creating a Name seperator Custom Control using C#priya Nithya
 
Repl internals
Repl internalsRepl internals
Repl internalsMongoDB
 
ATS Internals
ATS InternalsATS Internals
ATS InternalsChao Xu
 

What's hot (20)

Dapper performance
Dapper performanceDapper performance
Dapper performance
 
Codes
CodesCodes
Codes
 
Sanjar Akhmedov - Joining Infinity – Windowless Stream Processing with Flink
Sanjar Akhmedov - Joining Infinity – Windowless Stream Processing with FlinkSanjar Akhmedov - Joining Infinity – Windowless Stream Processing with Flink
Sanjar Akhmedov - Joining Infinity – Windowless Stream Processing with Flink
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Database Homework Help
Database Homework HelpDatabase Homework Help
Database Homework Help
 
The Weather of the Century
The Weather of the CenturyThe Weather of the Century
The Weather of the Century
 
The Weather of the Century Part 3: Visualization
The Weather of the Century Part 3: VisualizationThe Weather of the Century Part 3: Visualization
The Weather of the Century Part 3: Visualization
 
Fast track to lucene
Fast track to luceneFast track to lucene
Fast track to lucene
 
Heaps
HeapsHeaps
Heaps
 
Apache avro and overview hadoop tools
Apache avro and overview hadoop toolsApache avro and overview hadoop tools
Apache avro and overview hadoop tools
 
Storm real-time processing
Storm real-time processingStorm real-time processing
Storm real-time processing
 
Hashing gt1
Hashing gt1Hashing gt1
Hashing gt1
 
4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
Elk with Openstack
Elk with OpenstackElk with Openstack
Elk with Openstack
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Creating a Name seperator Custom Control using C#
Creating a Name seperator Custom Control using C#Creating a Name seperator Custom Control using C#
Creating a Name seperator Custom Control using C#
 
working with files
working with filesworking with files
working with files
 
Repl internals
Repl internalsRepl internals
Repl internals
 
Word games in c
Word games in cWord games in c
Word games in c
 
ATS Internals
ATS InternalsATS Internals
ATS Internals
 

Viewers also liked

ЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВ
ЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВ
ЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВukranewscom
 
Las vegas Entrepreneurship Presentation
Las vegas Entrepreneurship PresentationLas vegas Entrepreneurship Presentation
Las vegas Entrepreneurship PresentationJosselin Castillo
 
Doppler Academy: Estrategias de Marketing Online para PYMES
Doppler Academy: Estrategias de Marketing Online para PYMESDoppler Academy: Estrategias de Marketing Online para PYMES
Doppler Academy: Estrategias de Marketing Online para PYMESMaria Anna Molina
 
Doppler Academy: Los 7 pilares de la retención de clientes
Doppler Academy: Los 7 pilares de la retención de clientesDoppler Academy: Los 7 pilares de la retención de clientes
Doppler Academy: Los 7 pilares de la retención de clientesMaria Anna Molina
 
Декларация Бондаренко Е.В. за 2015
Декларация Бондаренко Е.В. за 2015Декларация Бондаренко Е.В. за 2015
Декларация Бондаренко Е.В. за 2015ukranewscom
 
Додаток 1 фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 1   фізичні особи, до яких застосовуються обмежувальні заходиДодаток 1   фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 1 фізичні особи, до яких застосовуються обмежувальні заходиukranewscom
 
Додаток 3 фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 3   фізичні особи, до яких застосовуються обмежувальні заходиДодаток 3   фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 3 фізичні особи, до яких застосовуються обмежувальні заходиukranewscom
 
Додаток 4 юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 4   юридичні особи, до яких застосовуються обмежувальні заходиДодаток 4   юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 4 юридичні особи, до яких застосовуються обмежувальні заходиukranewscom
 
Educación de la libertad
Educación de la libertadEducación de la libertad
Educación de la libertadliliana garcia
 
Додаток 2 юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 2   юридичні особи, до яких застосовуються обмежувальні заходиДодаток 2   юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 2 юридичні особи, до яких застосовуються обмежувальні заходиukranewscom
 
An introduction to Divest invest
An introduction to Divest invest An introduction to Divest invest
An introduction to Divest invest Tom Harrison
 
Розмір посадових окладів народних депутатів України
Розмір посадових окладів народних депутатів України Розмір посадових окладів народних депутатів України
Розмір посадових окладів народних депутатів України ukranewscom
 

Viewers also liked (17)

ЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВ
ЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВ
ЧЕРНАЯ БУХГАЛТЕРИЯ ПАРТИИ РЕГИОНОВ
 
Brochure AlphaDigiPrinting
Brochure   AlphaDigiPrintingBrochure   AlphaDigiPrinting
Brochure AlphaDigiPrinting
 
Las vegas Entrepreneurship Presentation
Las vegas Entrepreneurship PresentationLas vegas Entrepreneurship Presentation
Las vegas Entrepreneurship Presentation
 
Deliverable+1
Deliverable+1Deliverable+1
Deliverable+1
 
Doppler Academy: Estrategias de Marketing Online para PYMES
Doppler Academy: Estrategias de Marketing Online para PYMESDoppler Academy: Estrategias de Marketing Online para PYMES
Doppler Academy: Estrategias de Marketing Online para PYMES
 
Doppler Academy: Los 7 pilares de la retención de clientes
Doppler Academy: Los 7 pilares de la retención de clientesDoppler Academy: Los 7 pilares de la retención de clientes
Doppler Academy: Los 7 pilares de la retención de clientes
 
Greenhouse effect
Greenhouse effectGreenhouse effect
Greenhouse effect
 
Program
ProgramProgram
Program
 
Декларация Бондаренко Е.В. за 2015
Декларация Бондаренко Е.В. за 2015Декларация Бондаренко Е.В. за 2015
Декларация Бондаренко Е.В. за 2015
 
newsql
newsqlnewsql
newsql
 
Додаток 1 фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 1   фізичні особи, до яких застосовуються обмежувальні заходиДодаток 1   фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 1 фізичні особи, до яких застосовуються обмежувальні заходи
 
Додаток 3 фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 3   фізичні особи, до яких застосовуються обмежувальні заходиДодаток 3   фізичні особи, до яких застосовуються обмежувальні заходи
Додаток 3 фізичні особи, до яких застосовуються обмежувальні заходи
 
Додаток 4 юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 4   юридичні особи, до яких застосовуються обмежувальні заходиДодаток 4   юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 4 юридичні особи, до яких застосовуються обмежувальні заходи
 
Educación de la libertad
Educación de la libertadEducación de la libertad
Educación de la libertad
 
Додаток 2 юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 2   юридичні особи, до яких застосовуються обмежувальні заходиДодаток 2   юридичні особи, до яких застосовуються обмежувальні заходи
Додаток 2 юридичні особи, до яких застосовуються обмежувальні заходи
 
An introduction to Divest invest
An introduction to Divest invest An introduction to Divest invest
An introduction to Divest invest
 
Розмір посадових окладів народних депутатів України
Розмір посадових окладів народних депутатів України Розмір посадових окладів народних депутатів України
Розмір посадових окладів народних депутатів України
 

Similar to Heap

Data structures lab
Data structures labData structures lab
Data structures labRagu Ram
 
I need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdfI need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdfinfo998421
 
Apache Flink Training: DataSet API Basics
Apache Flink Training: DataSet API BasicsApache Flink Training: DataSet API Basics
Apache Flink Training: DataSet API BasicsFlink Forward
 
Java concurrency
Java concurrencyJava concurrency
Java concurrencyducquoc_vn
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxDIPESH30
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfarihantstoneart
 
Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageESUG
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programmingSonam Sharma
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7Ananda Kumar HN
 
Python Programming - Files & Exceptions
Python Programming - Files & ExceptionsPython Programming - Files & Exceptions
Python Programming - Files & ExceptionsOmid AmirGhiasvand
 
Hashingclass LinearProbingHashTable{    private int currentS.pdf
Hashingclass LinearProbingHashTable{    private int currentS.pdfHashingclass LinearProbingHashTable{    private int currentS.pdf
Hashingclass LinearProbingHashTable{    private int currentS.pdfkareemangels
 
IN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfIN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfaratextails30
 
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfpublic static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfarihanthtoysandgifts
 
Flink Batch Processing and Iterations
Flink Batch Processing and IterationsFlink Batch Processing and Iterations
Flink Batch Processing and IterationsSameer Wadkar
 

Similar to Heap (20)

Data structures lab
Data structures labData structures lab
Data structures lab
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
I need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdfI need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdf
 
Apache Flink Training: DataSet API Basics
Apache Flink Training: DataSet API BasicsApache Flink Training: DataSet API Basics
Apache Flink Training: DataSet API Basics
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
delegates
delegatesdelegates
delegates
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdf
 
Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta Language
 
ASP.NET.docx
ASP.NET.docxASP.NET.docx
ASP.NET.docx
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programming
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7
 
Python Programming - Files & Exceptions
Python Programming - Files & ExceptionsPython Programming - Files & Exceptions
Python Programming - Files & Exceptions
 
srgoc
srgocsrgoc
srgoc
 
Hashingclass LinearProbingHashTable{    private int currentS.pdf
Hashingclass LinearProbingHashTable{    private int currentS.pdfHashingclass LinearProbingHashTable{    private int currentS.pdf
Hashingclass LinearProbingHashTable{    private int currentS.pdf
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
IN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfIN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdf
 
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfpublic static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
 
Flink Batch Processing and Iterations
Flink Batch Processing and IterationsFlink Batch Processing and Iterations
Flink Batch Processing and Iterations
 

Heap

  • 1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InsertHeap { class Heap { private int max_size; private int size; private string[] h; public Heap(int m) { size = 0; max_size = m+1; h = new string[max_size]; } public void insertItem(string item) //max heap { size++; h[size] = item; int current = size; int parent = current / 2; while (parent > 0) { if (string.Compare(h[parent], h[current]) < 0) //parent is smaller than current { string temp = h[current]; h[current] = h[parent]; h[parent] = temp; } parent = parent / 2; current = current / 2; } } public void Extract_Max() { if (size < 1) { Console.WriteLine("Error Message"); }
  • 2. int parent = 1; // start with root at index 1 string max = h[parent]; // extract max, the root is the biggest h[parent] = h[size]; //swap h[size] = max; //store the extracted max to the last position of array size--; //decrease size by 1 int leftChild = 2 * parent; //left child while (leftChild < size ) //if left child is less than size { int rightChild = leftChild + 1; //if right child exist if (rightChild <= size) { if (string.Compare(h[leftChild], h[rightChild]) <= 0) // left child is smaller than right child { string temp = h[rightChild]; h[rightChild] = h[parent]; h[parent] = temp; parent = rightChild; //replace the rightchild with parent node int a = 2 * parent; leftChild = a; } else { string temp = h[leftChild]; h[leftChild] = h[parent]; h[parent] = temp; parent = leftChild; //replace the leftchild with parent node int a = 2 * parent; leftChild = a; } } else if (leftChild <= size && rightChild > size) { if (string.Compare(h[parent], h[leftChild]) <= 0) { string t = h[leftChild]; h[leftChild] = h[parent]; h[parent] = t; parent = leftChild; //replace the leftchild with parent node int a = 2 * parent; leftChild = a; } }
  • 3. } } public void printHeap() { for (int i = 1; i < h.Length; i++) { Console.Write(h[i] +" "); } } } }