DSFinal Flashcards
What are the four rules for red-black trees?
- each node is assigned a value of red or black
- each child of a red node must be black
- each node mas at most two children, making this a binary tree
- the path to each leaf must contain the same number of black nodes.
What is the difference between a stack and a queue?
A stack allows O(1) time to access, insert, or delete at the end of the container(the last thing you entered), while a queue allows O(1) time to access insert or delete at the front of the container(the first thing you entered).
Why do we separate the class definition and declarations into multiple files? How is this useful?
You put the declaration in an h file. You then use the scope resolution operator to define the class methods in a cpp file that #includes the h file. This is useful as you can use these classes in multiple projects by simply including the h file. This allows for more reusability and easier distribution.
What are the big three functions and why are the necessary?
I kind of thought there were six Constructor, Deconstructor, Copy Constructor, Move constructor, Copy Assignment operator, and Move Assignment operator. They are important as it tells the computer how you're class will be built without having to waste large amounts of space that comes from normally copying objects. Normally the computer would make a temporary copy of the object when copying or moving so by defining your own copy and move you can avoid this.
Define the run-time stack and run-time heap. Note the differences between the two.
The stack is a stack of data in ram that is provided to your program when it is run. it reads from the end and is a set length. It is where most of your varaibles are held including objects of a structs. The heap is where further information your program will need is held, it is located in ram where nothing has been allocated yet. it is where any variables created art run time is stored and where objects of a class are stored.
Define inheritance and explain how it works.
Inheritance is a feature that allows one class to contain all the features of anouther. First you create a base class, then you can create a derived class which inherits from that base class that will have access to all public and protected data members and methods. If you create an object from the derived class it will contain an object of the base class allong with all the aditional fuctionality you added to the derived.
What is a function template and how is it useful?
A function template is a blueprint for a function (not a function itself. No function is actually created when it is compiled, not until it is called with a specific data type). It allows you to create several similar functions that deals with different data types.
What is an abstract data type and how is it different from a regular data type?
abstract data types are used in base classes that are never suposed to be used to create objects only to be used to have classes derived form them.
What are the four types of rotations for AVL trees?
single left
single right
double left right
double right left
Order the following functions by growth rate: N, N−−√, N1.5, N2, N log N, N log log N, N log2 N, N log(N2), 2/N, 2N, 2N/2, 37, N2 log N, N3. Indicate which functions grow at the same rate.
2/N, 37, , N, N log log N, N log N, N log(N2), N log2 N, N1.5, N2, N2 log N, N3, 2N/2, 2N.
N log N and N log (N2) grow at the same rate.
An alternative to the deletion strategy we have given is to use lazy deletion. To delete an element, we merely mark it deleted (using an extra bit field). The number of deleted and nondeleted elements in the list is kept as part of the data structure. If there are as many deleted elements as nondeleted elements, we traverse the entire list, performing the standard deletion algorithm on all marked nodes.
a. List the advantages and disadvantages of lazy deletion.
The advantages are that it is simpler to code, and there is a possible saving if deleted keys are subsequently reinserted (in the same place). The disadvantage is that it uses more space, because each cell needs an extra bit (which is typically a byte), and unused cells are not freed.
Suppose that a singly linked list is implemented with both a header and a tail node. Describe constant-time algorithms to
a. insert item x before position p (given by an iterator)
b. remove the item stored at position p (given by an iterator)
(a) Add a copy of the node in position p after position p; then change the value stored in position
p to x.
(b) Set p->data = p->next->data and set p->next = p->next->next. Then delete p->next. Note that the
tail node guarantees that there is always a next node.
a. Give a precise expression for the minimum number of nodes in an AVL tree of height h.
b. What is the minimum number of nodes in an AVL tree of height 15?
(a) N(0) = 1, N(1) = 2, N(h) = N(h 1) + N(h 2) + 1.
(b) The heights are one less than the Fibonacci numbers.
Since a binary search tree with N nodes has N + 1 nullptr pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a nullptr left child, we make its left child link to its inorder predecessor, and if a node has a nullptr right child, we make its right child link to its inorder successor. This is known as a threaded tree and the extra links are calledthreads.
a. How can we distinguish threads from real children pointers?
b. What is the advantage of using threaded trees?
(a) You need an extra bit for each thread.
(c) You can do tree traversals somewhat easier and without recursion. The disadvantage is that it
reeks of old-style hacking.
Can both insert and findMin be implemented in constant time?
Yes. When an element is inserted, we compare it to the current minimum and change the minimum if the new element is smaller. deleteMin operations are expensive in this scheme.