Heaps, Trees and Hashing

User Generated

Xbbygbal843

Programming

Data Structures for Problem Solving (IT265 -1704A -01)

ctu online

Description

The next structure you will investigate is the Hash table and its methods implemented in pseudo code when linked lists are used to handle collisions.

The task this week is to complete the pseudo code for the following hash table operations:

  1. Insert
  2. Remove

Assume Hashtable is a simple array of size 8, with indices 0..7. Numeric keys are mapped by a Hashfunction that gives the mod(8,n) value for any key “n” yielding the Hashtable index for that key (0..7). A Hashtable entry is null unless a key exists with that index as its hashed index; if so, the Hashtable entry points to the first node of a linked list of keys with that hash index. The last node on this linked list has a null reference for the next referenced node. Assume the occurrence of a linked list node is represented by the object “Node” and its “Data” and “NextRef” attributes.

Week 2 Deliverables:

  • 1 pseudo code implementation of each Hash table operation: Insert and Remove
  • Fully documented pseudo code.
  • Add the completed pseudo code and the output to the Key Assignment template Section 2: Hashing, Heaps and Trees.
  • Name the document "IT265__IP2.doc."

Unformatted Attachment Preview

Running head: THE DATA STRUCTURE USING JAVA Data Structures for Problem Solving(IT265-1802B-01) Antonio Larkin The Data Structure Using Java Instructor: Donald Wilcoxen 5/19/18 1 THE DATA STRUCTURE USING JAVA 2 Abstract The identification of the different data structures is crucial within the java programming script. The understanding of the use of queues and stacks, heaps and trees, the sorting of algorithms increases the speed and efficiency of the usage of data. The proper and efficient planning of the queues, stacks and heaps allow businesses to be more alert in their data and in the hyper competitive market place. THE DATA STRUCTURE USING JAVA 3 Table of Contents Abstract ……………………………………………………………………………………2pg Section 1: lists, stacks and Queues ……………………………………………………….4pg The Stacks ………………………………………………………………………………….4-10pg The Queues………………………………………………………………………………10-16pg Section 2: Heaps and Trees ………………………………………………………………17pg Implementation of the pseudo code for the hash table Determining and solving the collisions with the linked list Section 3: sorting the Algorithms ………………………………………………………..18pg Compare sort algorithms Section 4: searching ……………………………………………………………………….19pg Implementation of the pseudo code to search for the values of the linked list Section 5: recursion: …………………………………………………………………….....20pg Implementation of the pseudo code to create a factorial of a number using recursion THE DATA STRUCTURE USING JAVA 4 Reference …………………………………………………………………………………...21pg Section 1: lists, stacks and Queues The Stacks: import java.io.*; class Node { public String element; public Node next; public Node(String value) { element = value; } public void displayNode() { THE DATA STRUCTURE USING JAVA System.out.println("Element [" + element + "] "); } } class LinkedList { private Node first; public LinkedList() { first = null; } public boolean isEmpty() { return (first==null); } public void push(String value) //inserts at beginning of list { 5 THE DATA STRUCTURE USING JAVA Node newNode = new Node(value); newNode.next = first; first = newNode; } public Node pop() //deletes at beginning of list { Node temp = first; first = first.next; return temp; } public void display() { System.out.println("Elements:"); Node current = first; while(current != null) { 6 THE DATA STRUCTURE USING JAVA current.displayNode(); current = current.next; } System.out.println(""); } } class Stack { private LinkedList stackLinkedList; public Stack() { stackLinkedList = new LinkedList(); } public void push(String value) { stackLinkedList.push(value); } 7 THE DATA STRUCTURE USING JAVA public Node pop() { return stackLinkedList.pop(); } public boolean isEmpty() { return stackLinkedList.isEmpty(); } public void displayStack() { System.out.println("Stack:"); stackLinkedList.display(); } } class StackCall { public static void main(String[] args) throws IOException 8 THE DATA STRUCTURE USING JAVA { Stack sample = new Stack(); System.out.println("Adding two elements into the stack"); sample.push("one"); sample.push("two"); sample.displayStack(); System.out.println("Adding two more elements into the stack"); sample.push("three"); sample.push("four"); sample.displayStack(); System.out.println("Removing two elements from the stack"); sample.pop(); } } 9 THE DATA STRUCTURE USING JAVA sample.pop(); sample.displayStack(); The Queues: import java.io.*; class Node { public String element; public Node next; public Node(String val) { element = val; 10 THE DATA STRUCTURE USING JAVA } public void displayNode() { System.out.println("Element [" + element + "]"); } } class LinkedList { private Node start; private Node end; public LinkedList() { start = null; end = null; } public boolean isEmpty() { 11 THE DATA STRUCTURE USING JAVA return start==null; } public void enqueue(String val) //Insert node at the end of list { Node newNode = new Node(val); if( isEmpty() ) start = newNode; else end.next = newNode; end = newNode; } public String dequeue() //delete the node from the beginning of the list { String temp = start.element; if(start.next == null) 12 THE DATA STRUCTURE USING JAVA end = null; start = start.next; return temp; } public void displayList() { System.out.println("Elements:"); Node current = start; while(current != null) { current.displayNode(); current = current.next; } System.out.println(""); } } class Queue 13 THE DATA STRUCTURE USING JAVA { private LinkedList queueLinkedList; public Queue() { queueLinkedList = new LinkedList(); } public boolean isEmpty() { return queueLinkedList.isEmpty(); } public void enqueue(String element) { queueLinkedList.enqueue(element); } public String dequeue() { return queueLinkedList.dequeue(); 14 THE DATA STRUCTURE USING JAVA } public void displayQueue() { System.out.println("Queue:"); queueLinkedList.displayList(); } } class QueueCall { public static void main(String[] args) { Queue sample = new Queue(); System.out.println("Adding two elements into the queue"); sample.enqueue("one"); sample.enqueue("two"); sample.displayQueue(); 15 THE DATA STRUCTURE USING JAVA } } System.out.println("Adding two more elements into the queue"); sample.enqueue("three"); sample.enqueue("four"); sample.displayQueue(); System.out.println("Removing two elements from the queue"); sample.dequeue(); sample.dequeue(); sample.displayQueue(); 16 THE DATA STRUCTURE USING JAVA Section 2: Heaps and Trees Implementation of the pseudo code for the hash table Determining and solving the collisions with the linked list 17 THE DATA STRUCTURE USING JAVA Section 3: sorting the Algorithms Compare sort algorithms 18 THE DATA STRUCTURE USING JAVA Section 4: searching Implementation of the pseudo code to search for the values of the linked list 19 THE DATA STRUCTURE USING JAVA Section 5: recursion: Implementation of the pseudo code to create a factorial of a number using recursion 20 THE DATA STRUCTURE USING JAVA Reference: Weiss, Mark A. (2009). Data Structures and Problem Solving Using Java, 4th Edition. Retrieved from https://bookshelf.vitalsource.com/#/books/9781323417645/ 21
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

attached is my answer

Solution to Heaps, Trees, and Hashing
Course’s Name
Student’s Name
Professor’s Name
Institution
Due Date

Solution to Heaps, Trees, and Hashing
Section 2:
The implementation of the pseudo code for the hash table (for operation Insert) is shown
below:
public class Insert
{
public Insert( )
{
this( size );
}
public Insert( int size )
{
theLists = new L[ nextPrime( size ) ];
for( int i = 0; i < theLists.length; i++ )
theLists[ i ] = new L( );
}
public void insert( type x )
{
List wL = theLists[ myhash( x ) ];
if( !wL.c( x ) )
{
wL.add( x );
if( ++currentSize > theLists.length )

rehash( );
}
}
public void remove( type x )
{
List wL = theLists[ myhash( x ) ];
if( wL.c ( x ) )
{
wL.remove( x );
currentSize--;
}
}
public boolean c ( type x )
{
List wL = theLists[ myhash( x ) ];
return wL.c ( x );
}
Similarly, the implementation of the pseudo code for ...


Anonymous
Very useful material for studying!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags