Data Structures Test Prep Ch 10 - 13, 15
Divide-and-conquer sorting paradigm
1) Divide the elements to be sorted unto two groups of equal (or almost equal) size 2) Sort each of these smaller groups of elements (by recursive calls) 3) Combine the two sorted groups into one large sorted list
Heap storage rules
1) The entry contained by a node is never less than the entries of the node's children 2) The tree is a complete binary tree, so that every level except the deepest must contain as many nodes as possible; and at the deepest level, all the nodes are as far left as possible
Linked-List representation of graphs
A directed graph with n vertices can be represented by n different linked lists. List number i provides the connections for vertex i. To be specific: for each entry j in list number i, there is an edge from i to j.
Binary Tree
A finite set of nodes. If the set is not empty, it follows these rules: 1) There is one special node, called the root 2) Each node may be associated with up to two different nodes, called its left child and its right child. If a node c is the child of another node p, then we say that "p is c's parent." 3) Each node, except the node, has exactly one parent; the root has no parent 4) If you start a node and move to the node's parent (provided there is one), then move again to that node's parent, and keep moving upward to each node's parent, you will eventually reach the root
Directed graph
A finite set of vertices together with a finite set of edges. Both sets might be empty (no vertices and no edges), which is called an empty graph. Each edge is associated with two vertices, called its source and target vertices. We sometimes say that the edge connects its source to its target. The order of the two connected vertices is important, so it does matter whether we say "This edge connects vertex u to vertex v," or "This edge connects vertex v to vertex u."
Undirected graph
A finite set of vertices together with a finite set of edges. Both sets might be empty (no vertices and no edges), which is called an empty graph. Each edge is associated with two vertices. We sometimes say that the edge connects its two vertices. The order of the two connected vertices is unimportant, so it does not matter whether we say "This edge connects vertices u and v," or "This edge connects vertices v and u."
Weighted graph
A graph in which there is a number associated with each edge [its weight].
Labeled graph
A graph where each vertex is identified with a unique number, letter, or name. The information itself is called a vertex's label
Simple graphs
A graph with no loops and no multiple edges
Leaf
A node with no children
Descendant
A node's children are its first descendants. The children's children are its next descendants. The children of the children are...well, you get the idea
Ancestor
A node's first parent is its first ancestor. The parent of the parent is the next ancestor. The parent of the parent of the parent is the next ancestor...and so forth, until you reach the root. The root is an ancestor of each other node
Tree
A nonlinear data strcutures
Path
A path in a graph is a sequence of vertices, p0, p1, ..., pn, such that each adjacent pair of vertices pi and pi + 1 are connected by an edge. In a directed graph, the connection must fo from the source pI to the target pI + 1
Selection sort
A sort algorithm that repeatedly scans for the smallest item in the list and swaps it with the element at the current index. The index is then incremented, and the process repeats until the last two elements are sorted. The simplest interchange sort
B-Tree
A special kind of tree, similar to a binary search tree, where each node holds entries of some type. It is not a binary tree as B-tree nodes have many more nodes than two children. Each node contains more than just a single entry
Adjacency Matrix
A square grid of true/false values that represent the edges of a graph. If the graph contains n vertices, then the grid contains n rows and n columns. For two vertex numbers i and j, the component at row i and column j is true if there is an edge from vertex i to vertex j; otherwise the component is false.
Struct
A struct is a special kind of class. The special feature is that struct members are all public (unless you state otherwise). C++ programmers tend to use a struct only when all the members are public
Trees
A tree if a finite set of nodes. If the set is not empty, then it must follow these rules: 1) There is one special node, called the root 2) Each node may be associated with one or more different nodes, called its children. If a node c is the child of another node p, then we say that p is c's parent 3) Each node except the root except the root has exactly one parent; the root has no parent 4) If you start any node and move to the node's parent (provided there is one), then move again to that node's parent (provided there is one), and keep moving upward to each node's parent, you will eventually reach the root
Empty tree
A tree with no nodes
Consider the binary_tree_node from Section 10.3. Which expression indicates that t represents an empty tree? A) (t == NULL) B) (t -> data() == 0) C) (t -> data() == NULL) D) ((t -> left() == NULL) && (t -> right() == NULL))
A) (t == NULL)
Suppose that X is a B-tree leaf containing 41 entries and having at least one sibling. Which statement is true? A) Any sibling of X is also a leaf. B) Any sibling of X contains at least 41 entries C) The parent of X has exactly 42 entries D) X has at least 41 siblings.
A) Any sibling of X is also a leaf
Select the one FALSE statement about binary trees: A) Every binary tree has at least one node B) Every non-empty tree has exactly one root node C) Every node has at most two children D) Every non-root has exactly one parent
A) Every binary tree has at least one node
Tree algorithms typically run in time O(d). What is d? A) The depth of the tree B) The number of divisions at each level C) The number of entries in each node D) The number
A) The depth of the tree
If a heap is implemented using a partially filled array called data, and the array contains n elements (n > 0), where is the entry with the greatest value? A) data[0] B) data[n - 1] C) data[n] D) data[(2 * n) + 1] E) data[(2 * n) + 2]
A) data[0]
Which formula is the best approximation for the depth of a heap with n nodes? A) log (base 2) of n B) The number of digits in n (base 10) C) The square root of n D) n E) The square of n
A) log (base 2) of n
Worst-case times for binary search trees
Adding an entry, deleting an entry, or searching for an entry in a binary search tree with n entries is O(d), where d is the depth of the tree. Since d is no more than n-1, the operations are O(n-1), which is O(n) (since we can ignore constants in big-O notation)
Worst-case time for heap operations
Adding or deleting an entry in a heap with n entries is O(d), where d is the depth of the tree. Because d is no more than log(base 2)n, the operations are O(log(base 2)n), which is O(log n) (since we can ignore log bases in big-O notation)
Analysis for quadratic time
An O(n) process that is performed n times results in a quadratic number of operations: (O(n^2))
Loop
An edge that connects a vertex with itself
Subtree
Any node in a tree also can be views as the root of a new, smaller tree. This smaller tree contains the node that we've picked as the new rot and all of the new root's descendants. This is called a subtree of the original tree.
Interchange sort
Any sorting algorithm that is based on swapping is referred to as an interchange sort
Suppose T is a binary tree with 14 nodes. What is the minimum possible depth of T? A) 0 B) 3 C) 4 D) 5
B) 3
Which statement is true for a B-tree? A) All entries of a node are greater than or equal to the entries in the node's children B) All leaves are at the exact same depth C) All nodes contain the exact same number of entries D) All non-leaf nodes have the exact same number of children
B) All leaves are at the exact same depth
What feature of heaps allows them to be efficiently implemented using a partially filled array? A) Heaps are binary search trees B) Heaps are complete binary trees C) Heaps are full binary trees D) Heaps contain only integer data
B) Heaps are complete binary trees
Insertion sort running time
Both worst-case and average-case running times are quadratic. The best-case (when the starting array is already sorted) is linear, and the algorithm is also quick when the staring array is nearly sorted
Suppose that we have implemented a priority queue by sorting the items in a heap (using an array for the heap items) We are now executing reheapification upward and the out-of-place node is at data[i] with priority given by data[i]. Which of the following boolean expressions is TRUE to indicate that the reheapification IS NOT YET DONE? A) (i > 0) B) (data[(i-1)/2] < data[i]) C) (i > 0) && (data[(i-1)/2] < data[i]) D) (i > 0) || (data[(i-1)/2] < data[i])
C) (i > 0) && (data[(i-1)/2] < data[i])
How many recursive calls usually occur in the implementation of the tree_clear function for a binary tree? A) 0 B) 1 C) 2
C) 2
Suppose that a B-tree has MAXIMUM of 10 and that a node already contains the integers 1 through 10. If a new value, 11, is added to this node, the node will split into two pieces. What values will be in these two pieces? A) The first piece will have only 1 and the second piece will have the rest of the numbers B) The first pie will have 1 through 5 and the second piece will have 6 through 11 C) The first piece will have 1 through 5 and the second piece will have 7 through 11 D) The first piece will have 1 through 6 and the second piece will have 7 through 11 E) The first piece will have 1 through 10 and the second piece will have only 11
C) The first piece will have 1 through 5 and the second piece will have 7 through 11
Suppose that a non-leaf node in a B-tree has 41 entries. How many children will this node have? A) 2 B) 40 C) 41 D) 42 E) 82
D) 42
Consider the node of a complete binary tree whose value is stored in data[i] for an array implementation. If this node has a right child, where will the right child's value be stored? A) data[i + 1] B) data[i + 2] C) data[(2 * i) + 1] D) data[(2 * i) + 2]
D) data[(2 * i) + 2]
Suppose that we have implemented a priority queue by storing the items in a heap. We are now executing a rehapification downward and the out-of-place node has priority of 42. The node's parent has a priority of 72, the left child has priority 52 and the node's right child has priority 62. Which statement best describes the status of reheapification? A) The reheapification is done B) The next step will interchange the tail children of the out-of-place node. C) The next step will swap the out-of-place node with its parent. D) The next step will swap the out-of-place node with its left child. E) The next step will swap the out-of-place with its right child
E) The next step will swap the out-of-place with its right child
Node representation of binary trees
Each node of a tree is stored in an object of a new binary_tree_node class. Each node contains pointers that link it to other nodes in the tree. An entire tree is represented as a pointer to the root node
Time behavior of logarithmic algorithms
For a logarithmic algorithm, doubling the input size will make the time increase by a fixed number of new operations
Left and right subtrees of a node
For a node in a binary tree, the nodes beginning with its left child and below are its left subtree. The nodes beginning with its right child and below are its right subtree
Average-case time for serial search
For an array of n elements, the average-case time for serial search to find an element that is in the array requires (n + 1)/2 array accesses
Best-case time for serial search
For an array of n elements, the best-case time for serial search is just one array access
Worst-case time for binary search
For an array of n elements, the worst-case time for binary search is logarithmic
Worst-case time for serial search
For an array of n elements, the worst-case time for serial search requires n array accesses
Value of the halving function
H(n) = (the number of times that n can be divided by 2, stopping when the result is less than 1) has the value: H(n) = log (base 2) n + 1
Binary search tree storage rules
In a binary search tree, the entries of the nodes can be compared with a strict weak ordering. These two rules are followed for every node n: 1) The entry in node n is never less than an entry in its left subtree (though it may be equal to one of these entries) 2) The entry in node n is less than every entry in its right subtree
Full binary trees
In a full binary tree, every leaf has the same depth, and every non leaf has two children
Priority queue with heap
In the heap implementation of a priority queue, each node of the heap contains one entry, each node of the heap contains one entry, which can be compared to each of the other entries by the less-than operator. The tree is maintained so that it follows the heap storage rules using the entries' priorities to compare nodes
Heap
Like a binary search tree, it is a binary tree where a less than operator forms a strict weak ordering that can be used to compare the nodes' entries. The arrangement has rules to follow that are different from a binary search tree
Edge
Link of a graph
Binary search
Looking for an item in an already sorted list by eliminating large portions of the data on each comparison
Root
Node at the top of a tree diagram
Vertex
Node of a graph
B-Tree rules
Rule 1: The root may have as few as one entry (or even no entries if it also has no children); every other node has at least MINIMUM entries Rule 2: The maximum number of entries in a node is twice the value of MINIMUM Rule 3: The entries of each B-tree node are store in a partially filled array, sorted from the smallest entry (at index 0) to the largest entry (at the final used position of the array) Rule 4: The number of subtrees below a nonleaf node is always one more than the number of entries in the node Rule 5: For any nonleaded node: (a) An entry at index i is greater than all the entries in subtree number i of the node, and (b) an entry at index i is less than all the entries in subtree i + 1 of the node Rule 6: Every leaf in a B-tree has the same depth
Serial search
Steps through part of an array one item at a time looking for a desired item. The search stops when the item is found or when the search has examined each item without success
Depth of a node
Suppose you start a node n and move upward to its parents. We'll call this "one step." Then move u to the parent of the parents -- that's a second step. Eventually you will reach the root, and the number of steps taken is called the depth of the node. The depth of the root itself is zero; a child of the root has depth one.
Complete binary trees
Suppose you take a full binary tree and start adding new leaves at a new depth from left to right. All the new leaves have the same depth -- one more than where w started -- and we always add leftmost nodes first. The tree is no longer a full tree because some leaves are a bit deeper than others. Instead, we call this a complete a binary tree. In order to be a complete tree, every level except the deepest mist contain as many nodes as possible; and the deepest level, all the nodes are as far left as possible
Nonlinear structure
The components do not form a simple sequence of first entry, second entry, third entry, and so on. Instead, there is a more complex linking between the components
Depth of a tree
The depth of a tree is the maximum depth of any of its leaves
Depth of recursive calls for binary search of an n-element array
The depth of recursive calls is, at most, the number of times that n can be divided by 2, stopping when the result is less than 1
The halving function
The halving function H(n) is defined by H(n) = (the number of times that n can be divided by 2, stopping when the result is less than 1)
Depth of recursive calls
The length of the longest chain of recursive calls in the execution of an algorithm is called the depth of recursive calls for that algorithm
Shortest path
The lowest weight path between two nodes
Parent
The parent of a node is the node linked above it. If node c is the child of node p, we say that "p is c's parent." Except for the root, every node has just one parent
Tree Traversal
The process of systematically visiting every node in a tree once. The three most common traversals are: pre-order, in-order, and post-order.
Post-Order Traversal
The processing of the root is postponed until last in a non-empty tree 1) Process the nodes in the left subtree with a recursive call 2) Process the nodes in the right subtree with a recursive call 3) Process the root
In-order traversal
The root is processed in between the processing of its two subtrees 1) Process the nodes in the left subtree with a recursive call 2) Process the root 3) Process the nodes in the right subtree with a recursive call
Pre-Order Traversal
The root is processed previous to its two subtrees 1) Process the root 2) Process the nodes in the left subtree with a recursive call 3) Process the nodes in the right subtree with a recursive call
Left Child Right Child
The two nodes that can be linked to an above node on a tree, one on the left and one on the right
Shortest distance
The weight of the shortest path
Selection sort running time
The worst-case running time, average-case running time, and the best-case running time for selection sort are all quadratic
Worst-Case times for tree operations
The worst-case time performance for the following operations are all O(d), where d is the depth of the tree: 1) Adding an entry in a binary search tree, a heap, or a B-tree 2) Deleting an entry from a binary search tree, a heap, or a B-tree 3) Searching for a specified entry in a binary search tree or a B-tree
Merge sort running time
The worst-case, average-case, and best-case are all O(n log n)
Sibiling
Two nodes are siblings if they have the same parent.
Multiple edges
Two or more edges connecting the same two vertices
Binary taxonomy tree
Used to store certain kinds of knowledge
Heap sort running time
Worst-case and average-case running time are both O(n log n)
Quick sort running time
Worst-case is O(n^2). Average-case and best-case are both O(n log n).