Question

I have an exam here in a couple of hours and I could use some help...

I have an exam here in a couple of hours and I could use some help with reviewing! This is the only study guide he posted and any information helps! Thank you!

Hi all,

Exam 2 is scheduled on August 5th Monday in classroom. Study guide is shown in the below.

1, Implementing push() and pop() methods for a LinkedStack, and their time complexity.

2, Implementing enqueue() and dequeue() methods for a LinkedQueue and their time complexity.

3, The logic idea of evaluating a postfix expression. Give an example of input postfix, you should be able to explain how to evaluate the postfix with a stack in a step-by-step manner.

4, Implementing a Hashtable that uses separate chaining strategy. get() and put() method.

5. Binary search tree

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

1. Implementing push() and pop() methods for a LinkedStack, and their time complexity.

class Stack{
  
static class Node {
  
int data;
Node next;
}
  
Node top;
  
Stack()
{
this.top = null;
}
  

public void push(int value)
{
  
Node temp = new Node();
  
if (temp == null) {
System.out.println("\n Overflow");
return;
}
  

temp.data = value;
temp.next = top;
top = temp;
}
  
  
public void pop()
{

if (top == null) {
System.out.println("\nStack Underflow");
return;
}

top = top.next;
}
}

TIME COMPLEXITY: O(1)

-----------------------------------------------------------------------------------------------------------------------------------------------------

2. Implementing enqueue() and dequeue() methods for a LinkedQueue and their time complexity.

class Queue {
  
static class Node {
  
int key;
Node next;
  
Node(int key)
{
this.key = key;
this.next = null;
}
}
  
Node front;
Node rear;
  
public Queue()
{
this.front=this.rear =null;
}
  
void enqueue(int value)
{
  
Node temp = new Node(value);

if (this.rear == null) {
this.front =this.rear=temp;
return;
}
  
this.rear.next = temp;
this.rear = temp;
}
  
Node dequeue()
{
if (this.front == null)
return null;
  
Node temp = this.front;
this.front = this.front.next;
  
if (this.front == null)
this.rear = null;
  
return temp;
}
}

TIME COMPLEXITY: O(1)

-------------------------------------------------------------------------------------------------------------------------------------------

3. The logic idea of evaluating a postfix expression.

  1. Create a Stack to store the values.
  2. We should scan the given expression from left to right and do the following for every element which is scanned.
  • If the element which we encounter is a number, push it to the stack
  • If the element which we encounter is an operator, immediately pop the operands for the operator from stack. After evaluating the operator and push the result back to the stack.

3.When the expression is ended, the number remaining in the stack is your final result.

Example:

Example:
Expression is 4 2 3 * + 7 -.

We scan all elements from left to right.
1) After scanning ‘4’, we come to know that it’s a number, so push it to the stack. Now the Stack contains ‘4’
2) After scanning ‘2’, we come to know that it’s a number, so push it to the stack, Now the Stack contains ‘4 2’
3) After scanning ‘3’, we come to know that it’s a number, so push it to the stack, Now the Stack contains ‘4 2 3’
4) After scanning ‘*’, we come to know that it’s an operator, so pop the two operands from the stack, and apply the * operation.

We get 2*3 which results as 6, so we push the result ‘6’ to stack. The Stack now becomes ‘4 6’.


5) After scanning ‘+’, we come to know that it’s an operator, so pop two operands from stack, apply the + operation.

We get 4+6 which results as 10. We push the result ‘10’ to stack. The Stack now becomes ‘10’.


6) After scanning ‘7’, we come to know that it’s a number, so push it to the stack. Now the Stack contains ‘10 7’.


7) After scanning ‘-‘, we come to know that it’s an operator, so pop two operands from stack, apply the – operation.

We get 10 - 7 which results as 3. We push the result ‘3’ to stack. The Stack now becomes ‘3’.


8) There are no more elements to scan, we return the top element from the stack.

The element in the stack is our answer: 3.

--------------------------------------------------------------------------------------------------------------------------------------------

4.  Implementing a Hashtable that uses separate chaining strategy. get() and put() method.

import java.util.*;
  
public class Main<K, V>
{
static class HashNode<K, V>
{
HashNode<K, V> next;
V value;
K key;
  
public HashNode(K key, V value)
{
this.key = key;
this.value = value;
}
}

private ArrayList<HashNode<K, V>> Array;
int Buckets,size;
  
Main()
{
Buckets = 10;
size = 0;
Array = new ArrayList<>();
  
for (int i = 0; i < Buckets; i++)
Array.add(null);
}
  
private int getBIndex(K key)
{
int hCode = key.hashCode();
int ind = hCode % Buckets;
return ind;
}
  
public V get(K key)
{
  
int bIndex = getBIndex(key);
HashNode<K, V> head = Array.get(bIndex);
  
// Searching key
while (head != null)
{
if (head.key.equals(key))
return head.value;
  
head = head.next;
}
  
return null;
}
  

public void put(K key, V value)
{

int bIndex = getBIndex(key);
HashNode<K, V> head = Array.get(bIndex);
  
while (head != null)
{
if (head.key.equals(key))
{
head.value = value;
return;
}
head = head.next;
}
  
  
head = Array.get(bIndex);
size++;
  
HashNode<K, V> newnode = new HashNode<K, V>(key, value);
newnode.next = head;
Array.set(bIndex, newnode);
  
//say 6 out of 10 gets filled
if ((1.0*size)/Buckets >= 0.6)
{
ArrayList<HashNode<K, V>> temp = Array;
Array = new ArrayList<>();
size = 0;
Buckets = 2*Buckets;
  
for (int i = 0; i < Buckets; i++)
Array.add(null);
  
for (HashNode<K, V> Node : temp)
{
while (Node != null)
{
put(Node.key, Node.value);
Node = Node.next;
}
}
}
}
}

--------------------------------------------------------------------------------------------------------------------------------------------

5. Binary Search Tree


public class BST
{
static class Node{

int data;
Node left,right;

Node(int key){
data=key;
left=right=null;
}
}

static Node root;

BST()
{
root=null;
}

//function to calculate the minimum
static int min(Node root){

int min=root.data;

while(root.left!=null){
min=root.left.data;
root=root.left;
}
  
return min;
}


static void insert(int key)
{
root=insertutil(root,key);
}
  
  
static void delete(int key)
{
root=deleteutil(root,key);
}


static Node insertutil(Node node, int key)
{
if(node==null){
node=new Node(key);
return node;
}
  
//conditions for a binary search tree
if(key<node.data)
node.left =insertutil(node.left, key);

else if(key>node.data)
node.right =insertutil(node.right, key);

return node;
}
  
  
static Node deleteutil(Node root, int key){
  
if(root==null)
return null;

if(key<root.data)
root.left=deleteutil(root.left, key);

if(key>root.data)
root.right=deleteutil(root.right, key);

else{
if(root.left==null && root.right==null) // condition for no children
return root;

if(root.left==null) // condition if right children is present while left is null
return root.right;

if(root.right==null) // condition if left is present while right is null
return root.left;

int minvalue=min(root.right); // condition if two children are present
root.right=deleteutil(root.right,key);

}
return root;
}
}

----------------------------------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
I have an exam here in a couple of hours and I could use some help...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Please help with my car traffic simulator! Code that I already have below, I do not know how to...

    Please help with my car traffic simulator! Code that I already have below, I do not know how to start it off! public class IntersectionSimulation { private final static int EAST_WEST_GREEN_TIME = 30 ; private final static int[] NORTH_SOUTH_GREEN_TIMES = { 20, 24, 30, 42 } ; private final static int[] CAR_INTERSECTION_RATES = { 3, 5, 10 } ; private final static int[] CAR_QUEUEING_RATES = { 5, 10, 30 } ; private final static int[] EXPERIMENT_DURATIONS = { 3*60, 5*60,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT