CMPS 390 Flashcards

1
Q

What is recursion?

A

A self calling method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Show an example of recursion for the fibonacci sequence.

A

int fib(int n) {

if(n == 1) {

return 1;

}

if(n == 2) {

return 1;

}

return(fib(n - 1) + fib(n - 2));

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create even or odd method with no modulus.

A

boolean EvenOrOdd(int n) {

if(n == 1) {

return false;

}

if(n == 2) {

return true;

}

return(EvenOrOdd(n - 2));

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the diagram for a singly linked list (node first) look like?

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does a doubly linked list: node first look like?

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between an array and a linked list?

A

[Edit: An array is indexed and a linked list is not.] An array has a fixed size, and the size must be known ahead of time. A Linked List can grow organically. Also, changing the positions in a linked list is easier due to being able to change node destinations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a stack?

A

LIFO - Last in First Out. (Think of a PEZ dispenser)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What operations are used on a stack?

A

Pop - removes an element on the top. Push - pushes an element on the top. Peek - Gives you the element at the top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a queue?

A

FIFO - First in First out. (Think of waiting in line for a roller coaster)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What operations are used on a queue?

A

Enqueue - Add to back of queue. Dequeue - Remove item from the front of queue. Peek - give you the element on the front. Empty - is the queue empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Linked List Write code to add an element to the start.

A

procedure addToHead(element)

head = new Node(element, head)

if(not the Tail)

tail = head

end if

end procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Linked List Write code to add an element to the end

A

procedure addToTail(element)

if(this is the tail)

tail->next = new Node(element)

tail = tail->next

endif

else

head -> tail = new Nodel(element)

end procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Linked List Write code to remove an element from the head

A

procedure RemoveFromHead()

if(this is the head)

element = head->info

temp = head

if(this is the last item in the list)

Break pointer connection between head and tail to terminate list

else

set head.next to equal next item in list

endif

release temp from memory pool reutrn element else return 0

end if

end procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Linked List Write code to remove an element from the tail

A

procedure RemoveFromTail()

if(this is the tail)

element = tail->info

if(this is the last item in the list)

Break pointer connection between head and tail to terminate list release head from memory pool

else

Traverse list until you reach the tail release tail from memory pool set tail pointer to previous element if one exists set tail’s point to next element to null

endif

return element

else

return 0

end if

end procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given a stack (S), use a queue (Q) to reverse the elements.

A

while(!S.isEmpty()) {

Q.Enqueue(S.Pop());

}

while(!Q.isEmpty()) {

S.Push(Q.Dequeue());

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Binary Tree In what order do you traverse a tree using Preorder Traversal?

A

Root, Left, Right

17
Q

Binary Tree In what order do you traverse a tree using In Order Traversal?

A

Left, Root, Right

18
Q

Binary Tree In what order do you traverse a tree using Post Order Traversal?

A

Left, Right, Root