CSE 205 EXAM 3

Ace your homework & exams now with Quizwiz!

A recursive BST method that takes a node and int and inserts a new key appropriately down the BST

Node insertRec(Node root, int key) { if (root == null) { root = new Node(key); return root; } if (key < root.key) root.left = insertRec(root.left, key); else if (key > root.key) root.right = insertRec(root.right, key); return root; }

Create a Constructor to create a new node

Node(int d) {data = d;}

Time complexity of push() is

O(1)

Time complexity of push() does a constant amount of work.

O(1) as it does

Time complexity of append is _____ where n is the number of nodes in linked list. Since there is a loop from head to end, the function does ____ work.

O(n)

Queues Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an

Overflow condition.

Queues Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an

Underflow condition.

Java Queue interface orders the element in

FIFO

FIFO

First In First Out

Front:

Get the front item from queue.

Rear:

Get the last item from queue.

void addLast(Object o)

It is used to append the given element to the end of a list.

boolean add(Object o)

It is used to append the specified element to the end of a list.

void addFirst(Object o)

It is used to insert the given element at the beginning of a list.

void add(int index, Object element)

It is used to insert the specified element at the specified position index in a list.

add(object)

It is used to insert the specified element into this queue and return true upon success.

offer(object)

It is used to insert the specified element into this queue.

boolean remove(Object o)

It is used to remove the first occurence of the specified element in a list.

poll()

It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.

remove()

It is used to retrieves and removes the head of this queue.

peek()

It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

element()

It is used to retrieves, but does not remove, the head of this queue.

Object getFirst()

It is used to return the first element in a list.

int indexOf(Object o)

It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.

int lastIndexOf(Object o)

It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.

Object getLast()

It is used to return the last element in a list.

int size()

It is used to return the number of elements in a list

boolean contains(Object o)

It is used to return true if the list contains a specified element.

LIFO

Last In First Out

Declare the head of list of class LinkedList /* Linked list Node*/ class Node { int data; Node next; } }

Node head;

In a linked list, elements are linked using

pointers.

Trace Through Stack X.push(1); X.peek() X.push(2); X.push(3); X.push(4); X.pop(); X.peek() X.push(5);

1 1 2 1 3 2 1 4 3 2 1 3 2 1 3 2 1 5 3 2 1

What are the steps to add a node at the front of a linked list?

1 & 2: Allocate the Node & Put in the data 3. Make next of new Node as head 4. Move the head to point to new Node

Advantages over singly linked list

1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.

Insertion A node can be added in four ways

1) At the front of the DLL 2) After a given node. 3) At the end of the DLL 4) Before a given node.

A node can be added in three ways

1) At the front of the linked list 2) After a given node. 3) At the end of the linked list.

Linked list Advantages over arrays

1) Dynamic size 2) Ease of insertion/deletion

DLL disadvantages over singly linked list

1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though (See this and this). 2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.

Drawbacks of a linked list

1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists. 2) Extra memory space for a pointer is required with each element of the list.

Arrays can be used to store linear data of similar types, but arrays have following limitations.

1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage. 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.

list the step for a method to Add a node at the end

1. Allocate the Node & 2. Put in the data 3. Set next as null 4. If the Linked List is empty, then make the new node as head & make new node that is going to be the last node, so make next of it as null 5. Else traverse till the last node 6. Change the next of last node

list the steps to Add a node after a given node: (5 steps process)

1. Check if the given Node is null 2. Allocate the Node & 3. Put in the data 4. Make next of new Node as next of prev_node 5. make next of prev_node as new_node

Hand trace a queue X through the following operations: X.enqueue(new Integer(14)); X.enqueue(new Integer(11)); Object Y = X.dequeue(); X.enqueue(new Integer(18)); X.enqueue(new Integer(12)); X.enqueue(new Integer(15)); X.enqueue(new Integer(13)); Object Y = X.dequeue(); X.enqueue(new Integer(14)); X.enqueue(new Integer(19));

14 11 14 11 18 11 12 18 11 15 12 18 11 13 15 12 18 11 13 15 12 18 14 13 15 12 18 19 14 13 15 12 18

3) Circular lists are useful in applications to repeatedly go

around the list

create a simple linked list with 3 nodes

class LinkedList { Node head; static class Node { int data; Node next; Node(int d) { data = d; next=null; } public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.head = new Node(1); Node second = new Node(2); Node third = new Node(3); llist.head.next = second; second node second.next = third; } }

/* Class containing left and right child of current node and key value*/ for BST

class Node { int key; Node left, right; public Node(int item) { key = item; left = right = null; } }

Circular linked list is a linked list where all nodes are

connected to form a circle.

Unlike arrays, linked list elements are not stored at

contiguous location

In a Binary Search Tree There must be no

duplicate nodes.

Queues: Assume elements are added to

end of the list

Stacks: Newest element is the is the new

head of the list

Advantages of Circular Linked Lists: We can maintain a pointer to the ______ inserted node and ______ can always be obtained as next of last.

last, front

Binary Search Tree Elements smaller go to the

left

Linked lists are what type of data structure?

linear

Circular linked list has what at the end?

no null

Any newly declared node is by default initialized as

null

write a re-balance that changes the root of the tree to the median.

private static int index; private static BST rebalance(BST im) { BST tree = new BST(); int[] nums = new int[im.getCount()]; index = 0; getValuesInOrder(im.getRoot(), nums); int rootDex = nums.length / 2; tree.add(new Node(nums[rootDex])); for(int i = 0; i < nums.length; i++) { if(i != rootDex) { tree.add(new Node(nums[i])); } } return tree; }

write a get values in order for a tree.

private static void getValuesInOrder(Node parent, int[] nums) { if(parent.getLeft() != null) { getValuesInOrder(parent.getLeft(), nums); } nums[index] = parent.getValue(); index++; if(parent.getRight() != null) { getValuesInOrder(parent.getRight(), nums); } }

insertRec for binary tree

private void insertRec(Node latestRoot, Node node){ if ( latestRoot.value > node.value){ if ( latestRoot.left == null ){ latestRoot.left = node; return; } else{ insertRec(latestRoot.left, node); } } else{ if (latestRoot.right == null){ latestRoot.right = node; return; } else{ insertRec(latestRoot.right, node); }

write a method that removes duplicates from a sorted linked list

public ListNode removeDuplicateElements(ListNode head) { if (head == null || head.next == null) { return null; } if (head.data.equals(head.next.data)) { ListNode next_next = head.next.next; head.next = null; head.next = next_next; removeDuplicateElements(head); } else { removeDuplicateElements(head.next); } return head; }

basic search through a binary search tree

public Node search(Node root, int key) { if (root==null || root.key==key) return root; // val is greater than root's key if (root.key > key) return search(root.left, key); // val is less than root's key return search(root.right, key); }

write an Append method that takes an int and adds it as a new last node.

public void append(int new_data) { Node new_node = new Node(new_data); if (head == null) { head = new Node(new_data); return; } new_node.next = null; Node last = head; while (last.next != null) last = last.next; last.next = new_node; return; }

write out a method that Adds a node after a given node

public void insertAfter(Node prev_node, int new_data) { if (prev_node == null) { System.out.println("The given previous node cannot be null"); return; } Node new_node = new Node(new_data); new_node.next = prev_node.next; prev_node.next = new_node; }

For traversal, let us write a general purpose function printList() that prints any given list.

public void printList() { Node n = head; while (n != null) { System.out.print(n.data+" "); n = n.next; } }

write a method that takes an int and inserts a node at the front (push)

public void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; }

Advantages of Circular Linked Lists: Useful for implementation of _____. Unlike this implementation, we don't need to maintain two pointers for front and rear if we use circular linked list.

queue

Pop: Removes an item from the stack. The items are popped in the ___________ order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

reversed

BST Elements larger go to the

right

A circular linked list can be a (2 kinds of lists)

singly circular linked list or doubly circular linked list.

Push: Adds an item in the ______. If the stack is full, then it is said to be an Overflow condition.

stack

Advantages of Circular Linked Lists: Any node can be a _________ ______. We can traverse the whole list by starting from any point. We just need to stop when the _____ _______ _____ __ _______ _______

starting point, first visited node is visited again.

public boolean isEmpty() or isEmpty() method checks if

this string is empty. It returns true, if length of string is 0 otherwise false.

what does this return? if (root.key > key) return search(root.left, key);

val is greater than root's key

utility to make a bst in order

void inorder() { inorderRec(root); }

// A utility function to do inorder traversal of BST

void inorderRec(Node root) { if (root != null) { inorderRec(root.left); System.out.println(root.key); inorderRec(root.right); } }


Related study sets

Short Answers to Death of a Salesman act 1

View Set

Geology Ch. 4-5, Geology: Overview of the Physical Earth, Bowens Reaction Series, Rocks, Quiz Questions

View Set

Macro-Economic Theory Final Exam (Villanova)

View Set

NUR 343 Adaptive Quiz #1 Women's Health/Disorders

View Set

chapt. 6 How Do We Know When to Reject the Null Hypothesis? Alpha, P Values, Normal Distribution, Confidence Intervals, Power and Effect Sizes

View Set

TTC NUR205: MedSurgII Chapter 24 PrepU (Intestinal & Rectal Disorders)

View Set

BIO 220 LAB Practical Exam 1 CSUSB Lujan

View Set

Who was the first Englishman to sail around the world

View Set

Nature and Environment 9 клас

View Set

INTEGRATIONS - Texas Nurse Practice Act (PART 1&2) COMPLETE

View Set