Documentation

advertisement
Heap applet
(Animates heap insertions/deletions and heap sort algorithm)
User documentation:
1. Introduction:
Sorting algorithms are among the most fundamental algorithms of computer science. A
sorting algorithm takes an array of data and rearranges it so that it is in ascending, or
sometimes descending order according to some sort key.
There are many different algorithms that can be used to sort an array of data. The heap
sort algorithm is one of the most widely used sorting algorithms. The primary advantage
of the heap sort is its efficiency. The execution time efficiency of the heap sort is O (nlog
n). The memory efficiency of the heap sort, unlike the other nlogn sorts, is constant, O
(1), because the heap sort algorithm is not recursive.
Heaps basically have the same complexity as a balanced binary tree with some
advantages.

They can be easily kept in an array.

They are much simpler than a balanced binary tree.

They are cheaper to run in practice.
The difference in the binary trees and heaps is that, in a heap the root is always greater
than any value in a sub-tree and it’s a complete tree with height always being log (n).
The heap sort algorithm has two major steps.
The first step involves transforming a binary tree into a heap.
The second step is to perform the actual sort by extracting the largest element from the
root and transforming the remaining tree into a heap.
Hence the steps of the heap sort algorithm are:
1. Use data to form a heap
2. Remove highest priority item from heap (largest)
3. Reform heap with remaining data
Repeat steps 2 & 3 until all the data is finished.
The applet is divided into 2 parts.

The first part explains the insertion and deletion of nodes from a heap.

The second part explains the heap sort algorithm.
Users interact with the system using check boxes, combo box, a text input field and push
buttons. The controls are explained together with the applet animation and user tasks.
User feedback is given, using the display area and the text field.
A detailed description of the tasks that a user needs to perform and the applet animation
is given below.
2. Applet Animation and User Tasks:
When a user starts the applet, the text display as shown below, instructs the user to select
the algorithm that the user wants to implement using the checkboxes. The user needs to
check the box near the algorithm that needs to be implemented.
Figure shows the interface that allows the user to select the algorithm to implement.
Suppose the user selects to implement the heap algorithm.
Figure shows the interface for the heap algorithm.
The interface for the heap algorithm is shown in the above figure. The user can insert a
node or delete a node from the heap. Since the heap is initialy empty, the user has to start
by inserting a node in the heap. To insert a number the user has to type a number in the
text field and press the Insert button.
Figure shows a heap with one element.
The number will be inserted in the heap as shown in the figure above.
If the user intends, more numbers can be entered in the text field and inserted in the heap
by clicking on the insert key.
To perform animation, the comparison of the nodes will be shown in yellow color.
Figure shows a comparision between two nodes.
The exchanges are shown in Magenta color.
Figure shows an exchange between two nodes.
The user can trace through the algorithm using the Next button.
To delete a node from the heap, the user has to click on the node that is to be deleted and
press the delete button. The node clicked on is highlighted using green color.
Figure shows that a node to be deleted is highlighted in green.
To return to the main menu i.e. to choose the Heap Sort algorithm, the user has to press
the main menu button. Pressing the main menu button takes the user to the initial screen,
and then the user can select the check box for heap sort algorithm.
The users can refer to the help manual to understand the interface. The help manual is
displayed on pressing the ‘Help’ button.
Figure shows the help manual.
The interface for the heap sort algorithm is shown in the figure below.
Figure shows the interface of the heap sort algorithm.
The display area is divided into two parts. The tree shows the logical representation of the
numbers used by the algorithm, and the array shows the physical representation of the
numbers in the array.
As displayed in the text field the user needs to start the algorithm by entering numbers in
the text field. The user enters the number in the text field
Figure shows the interface after user has entered a few numbers.
Once the user has completed entering the numbers, he presses the ‘Start’ button to begin
the animation.
Figure shows comparison between two nodes.
Figure shows exchange between two nodes.
The user can go to the next step in the algorithm animation by pressing the ‘Next’ button.
To restart the current animation the user needs to press the ‘Restart’ button.
Pressing the reset button gets rid of old input, and the user can start the algorithm with
another set of input.
The function of MainMenu button is same as in the heap interface explained above.
In addition to the instructions in the text area and the feedback messages, the user can
refer to the help manual if there is any confusion with the interface and the graphical
display
The help manual is displayed when the help button is clicked.
Figure shows the help manual.
Software Documentation:
3. Code Explanation:
The following classes are used to implement this applet. The explanation for each of the
classes is given below.
a) HeapMain:
The HeapMain class is written in the HeapMain.java file. This class contains the main ()
function. To compile the system you need to type in the following command:
“javac HeapMain.java”
To run the system you need to type in the following command:
“java HeapMain”
The class “HeapMain” calls the SetUpclass.
The control flow is shown below.
Calls
HeapMain
SetUp
The HeapMain has only one function main () where the program execution begins.
b) SetUp:
The SetUp class written in HeapMain.java file, controls the user interface and interaction
of the entire system.
The following Java swing classes are used to create the user interface.
-
JFrame
-
JPanel
-
JTextPane
-
JScrollPane
-
JButton
-
JComboBox
-
JtextField
-
Container
Other java classes used are as follows:
-
String
-
Style
-
StyleConstants
Besides java classes, the following classes that have been written as a part of this thesis
are also imported to create the user interface and show graph animation.
-
DrawArray
-
DrawHeaps
-
HeapAlgorithm
-
HeapSortAlgorithm
-
InstructionHeapSort
-
InstructionHeap
Each of the above class is explained in detail below.
Apart from these classes, Java Action Listeners have been used for creating the buttons,
text field and the combo box. Document interface has been used for the JTextPane class.
The constructor of the class i.e. BufferPool () creates the display area that draws both the
memories, the text area that displays the messages during run time and the buttons, text
field and the combobox that allows the user to interact with the system.
The method summary of the SetUp class:
a) ActionPerformed ()
This method is called when the user presses any of the buttons, combo box or
enters a number in the text field. Depending on the button pressed or the
algorithm selected in the combo box or the page entered in the text field,
appropriate action is taken.
b) ItemStateChanged ()
This method is called when either of the checkboxes is checked.
c) EnterElements ()
This method is called each time the user enters a number in the text field.
d) WhenStopPressed ()
This method initializes the parameters required to start the heap creation process
in case of the heap sort algorithm.
e) CreateHeap ()
This function animates the creation of the initial heap in case of the heap sort
algorithm.
f) SortElements ()
This function animates sorting of the elements after the creation of initial element
for the heap sort algorithm.
g) RunInsert ()
This method animates the ‘Insertion’ process for the heap algorithm.
h) DeleteAlgorithm ()
This method animates the ‘Delete’ process for the heap algorithm.
i) Removes ()
This method replaces the element selected by the user to be deleted with the last
element of the heap.
j) Message (String color)
This method is called to display a message in the text field.
The control flow for the SetUp class is given below:
SetUp
drawArray
drawHeaps
HeapSortAlgorithm
InstructionHeap
HeapAlgorithm
InstructionHeapSort
c) drawArray
The drawArray class written in drawArrray.java file performs the graphics operation to
animate heap sort algorithm. The class inherits its method from the swing class
JComponent.
The method summary for the drawBuffer class:
a) Paint ()
This function implements the graphics routines.
b) InputArray ()
This function receives the array of the numbers that are being sorted.
c) NodeCds ()
This function sets the co-ordinates to draw each node on the display area.
c) SortElementsValue ()
This function is used to mark the start of the animation.
d) ColorVertex ()
This function highlights the nodes to perform the animation.
e) ResetNodes ()
This function is used to stop the highlighting of the nodes.
f) ArrayLength ()
This function is used to pass the arraylength.
The control flow for drawBuffer is shown below:
calls
drawArray
SetUp
d) drawHeaps class:
The drawHeaps class written in drawHeaps.java file performs the graphics operation to
animate insertion and deletion of nodes from a heap. The class inherits its method from
the swing class JComponent.
The method summary for the drawBuffer class:
a) Paint ()
This function implements the graphics routines.
b) InputArray ()
This function receives the array of the numbers that are being sorted.
c) NodeCds ()
This function sets the co-ordinates to draw each node on the display area.
c) ColorVertex ()
This function highlights the nodes to perform the animation.
d) ResetNodes ()
This function is used to stop the highlighting of the nodes.
e) ArrayLength ()
This function is used to pass the arraylength.
f) SetNodeSelected ()
This function deduces the node clicked by the user to delete from the heap.
g) SetNodeEditable ()
This function decides whether the user can change the node selected to delete
from the heap.
h) SelectedNode ()
This function returns the node selected by the user.
i) SetShowNode ()
This function is used to highlight the node selected by the user to delete from the
heap.
j) SetMarkTodelete ()
Once the deletion of a node is completed from the heap, this function allows the
user to select another node to delete.
The control flow for this class is shown below:
drawHeaps
Calls
SetUp
e) HeapAlgorithm:
This class implements the heap algorithm.
The method summary for the HeapAlgorithm:
a) Initialize ()
This function initializes arrays needed to store values to perform the animation.
b) UpdateLength ()
This function updates the heap length after an insertion or a delete operation.
c) Parent ()
This function returns the parent of a node.
d) Rightchild ()
This function returns the right child of a node.
e) Leftchild ()
This function returns the left child of a node.
f) IsLeaf ()
This function is used to check if the current node is a leaf.
g) InsertElement ()
This function is implements the ‘Insertion’ process for the heap algorithm.
h) ReturnClonePointers ()
This function returns total number of animations to perform for the current
Insertion/Deletion process.
i) DeleteAlgorithm ()
This function implements the ‘Delete’ process for the heap algorithm.
The control flow of this class is as follows:
HeapAlgorithm
Calls
SetUp
f) HeapSort Algorithm.
This class implements the heap sort algorithm for the system.
The method summary for this class:
a) Initialize ()
This function initializes arrays needed to store values to perform the animation.
b) SetHeapBuild ()
This function is used to determine if the Initial heap to sort the elements is created.
c) Siftdown ()
After the initial heap is created, in case of heap sort algorithm the first element or
the root is replaced with the last position. This function puts the new element in its
proper position and recreates the heap.
d) SortElements ()
This function implements the sorting operation for the heap sort algorithm, after the
creation of the intial heap.
e) Parent ()
This function returns the parent of a node.
f) Rightchild ()
This function returns the right child of a node.
g) Leftchild ()
This function returns the left child of a node.
h) IsLeaf ()
This function is used to check if the current node is a leaf.
i) InsertElement ()
This function is implements the ‘Insertion’ process for the heap algorithm.
j) ReturnClonePointers ()
This function returns total number of animations to perform for the current
Insertion/Deletion process.
The control flow of this class is as follows:
HeapSortAlgorithm
Calls
SetUp
g) InstructionHeap:
This class displays the help manual for the heap algorithm. It is called when the user
clicks the help button.
The constructor of the class creates the interface for the help manual and adds text to it.
The method summary for the Instructions class:
a) Message ()
This function is used to display a string in the text area.
b) getrid ()
This function disables more than one help manual open at a time. It will destroy
the old manual window before displaying the new one.
The control flow of this class is as follows:
Calls
InstructionHeap
SetUp
h) InstructionHeapSort:
This class displays the help manual for the heap sort algorithm. It is called when the user
clicks the help button.
The constructor of the class creates the interface for the help manual and adds text to it.
The method summary for the Instructions class:
a) Message ()
This function is used to display a string in the text area.
b) getrid ()
This function disables more than one help manual open at a time. It will destroy
the old manual window before displaying the new one.
The control flow of this class is as follows:
Calls
InstructionHeap
SetUp
The control flow of the system is as follows:
HeapMain
SetUp
drawArray
drawHeaps
HeapSortAlgorithm
InstructionHeap
HeapAlgorithm
InstructionHeapSort
Download