ITSC 2214 ZyBooks Week 10

¡Supera tus tareas y exámenes ahora con Quizwiz!

Given a key, a search algorithm:

returns the first node found matching that key, or returns null if a matching node is not found

min-heap

similar to a max-heap, but a node's key is less than or equal to its children's keys

searching a BST takes ___ comparisons

log₂(N) + 1

Remove an internal node with two children

> First, the algorithm locates X's successor (the leftmost child of X's right subtree), and copies the successor to X. > Then, the algorithm recursively removes the successor from the right subtree.

minimum N-node binary tree height is:

N = # of nodes h = log₂(N)

Remove an internal node with single child

> If X has a parent (so X is not the root), the parent's left or right child (whichever points to X) is assigned with X's single child. > Else, if X was the root, the root pointer is assigned with X's single child.

Remove a leaf node

> If X has a parent (so X is not the root), the parent's left or right child (whichever points to X) is assigned with null. > Else, if X was the root, the root pointer is assigned with null, and the BST is now empty.

A remove from a max-heap

> a removal of the root, and is done by replacing the root with the last level's right node, and swapping that node with its greatest child until no max-heap property violation occurs > Because upon completion that node will occupy another node's location (which was swapped upwards), the tree height remains the minimum possible

simple BST search algorithm

> checks the current node (initially the tree's root) returning that node immediately as a match > else assigning the current node with the left (if key is less) or right (if key is greater) child and repeating > If such a child is null, the algorithm returns null after exiting the loop (matching node not found)

Given a new node, a BST insert operation:

> inserts the new node in a proper location obeying the BST ordering property > A simple BST insert algorithm compares the new node with the current node (initially the root)

heapify

> operation that is used to turn an array into a heap > starts on the internal node with the largest index and continues down to, and including, the root node at index 0. Given a binary tree with N nodes, the largest internal node index is floor(N / 2) - 1 > Since leaf nodes already satisfy the max heap property, heapifying to build a max-heap is achieved by percolating down on every non-leaf node in reverse order.

Given a key, a BST remove operation:

> removes the first-found matching node, restructuring the tree to preserve the BST ordering property > The algorithm first searches for a matching node just like the search algorithm > If found (call this node X), the algorithm performs one of the sub-algorithms

An insert into a max-heap:

> starts by inserting the node in the tree's last level, and then swapping the node with its parent until no max-heap property violation occurs > Inserts fill a level (left-to-right) before adding another level, so the tree's height is always the minimum possible

height

> the largest depth of any node. > A tree with just one node has height 0 > the maximum edges from the root to any leaf

Given a tree representation of a heap, the heap's array form is produced by:

> traversing the tree's levels from left to right and top to bottom > The root node is always the entry at index 0 in the array, the root's left child is the entry at index 1, the root's right child is the entry at index 2, and so on

Parent

A node with a child is said to be that child's parent

Internal node

A node with at least one child

Leaf

A tree node with no children

balanced

A tree whose: 1.) Left and right subtrees' heights differ by at most one 2.) Left subtree is balanced 3.) Right subtree is balanced

level

All nodes with the same depth

BST ordering

its just numeric or lexicographic order

Given N nodes, what is the height of a max-heap?

log(N)

edge

The link from a node to a child

An array sorted in ascending order is already a valid max-heap (T/F)

False > A max-heap can be used to sort an array, but a sorted array may not be valid max-heap. Ex: The array with values 10, 20, and 30 would have a root node with 2 larger children, and would not be a valid max-heap. > ascending means ascending as you go from top to bottom, so smallest number at the top, so not max-heap

Calling Heapsort on an array with 1 element will cause an out of bounds array access. (T/F)

False > In the first loop, i is initialized with 0 and MaxHeapPercolateDown is called on the root, which has no effect. In the second loop, i is initialized with 0 and the loop body never executes.

The formula for computing child node indices does not work on the root node. (T/F)

False > The formulas 2 * i + 1 and 2 * i + 2 work correctly to compute child indices of 1 and 2 for the root node.

Heapsort uses recursion (T/F)

False > The only function Heapsort calls is MaxHeapPercolateDown, which is not recursive. So, the Heapsort algorithm is not recursive.

Parent and child indices for a heap:

For Index ( i ): Parent Index = floor((i - 1) / 2) Child Indices = (2 * i + 1), (2 * i + 2)

Searching a BST in the worst case requires has an ___ complexity

H = tree height O(H)

perfect

if all internal nodes have 2 children and all leaf nodes are at the same level

Search for insert location:

If the left (or right) child is not null, the algorithm assigns the current node with that child and continues searching for a proper insert location

Insert as right child:

If the new node's key is greater than or equal the current node, and the current node's right child is null, the algorithm assigns the node's right child with the new node

Insert as left child:

If the new node's key is less than the current node, and the current node's left child is null, the algorithm assigns that node's left child with the new node

maximum N-node binary tree height is:

N = # of nodes N - 1

Heapsort's worst-case runtime is ___

O(N log N)

MaxHeapPercolateDown's worst-case runtime is:

O(log N)

Given a max-heap with N nodes, what is the complexity for removing the root?

O(log(N))

Given a max-heap with N nodes, what is the complexity of an insert, assuming an insert is dominated by the swaps?

O(log(N))

Removing a node from an N-node nearly-full BST has what computational complexity?

O(logN) > The computation is dominated by searching for the node, which is O(logN). > The actual removal is just a few pointer updates.

complete

if all levels, except the last level, are completely full and all nodes in the last level are on the left

Root

The one tree node with no parent (the "top" node)

percolating

The upward movement of a node in a max-heap

ancestors

include the node's parent, the parent's parent, etc., up to the tree's root

The formula for computing parent node index does not work on the root node. (T/F)

True > Since the root node has no parent, the formula must not be used on the root node. The formula holds for all other nodes in the heap.

Heap implementations must check to see if a node is a leaf node before using the child index formula. (T/F)

True > The formula always gives appropriate child indices, but the check to see if such children actually exist in the heap must be done before utilizing the formula.

post order traversal

Until all nodes are traversed: Step 1 − Recursively traverse left subtree. Step 2 − Recursively traverse right subtree. Step 3 − Visit root node. > when another tree is encountered, the algorithm is restarted for that tree until another tree is encountered or it ends, after it hits the end, it goes back up the chain and completes any traversals it hasn't done yet, this is all based on recursion

in-order traversal

Until all nodes are traversed: Step 1 − Recursively traverse left subtree. Step 2 − Visit root node. Step 3 − Recursively traverse right subtree. > when another tree is encountered, the algorithm is restarted for that tree until another tree is encountered or it ends, after it hits the end, it goes back up the chain and completes any traversals it hasn't done yet, this is all based on recursion > visits all nodes in a BST from smallest to largest

pre-order traversal

Until all nodes are traversed: Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse right subtree. > when another tree is encountered, the algorithm is restarted for that tree until another tree is encountered or it ends, after it hits the end, it goes back up the chain and completes any traversals it hasn't done yet, this is all based on recursion

max-heap

a binary tree that maintains the simple property that a node's key is greater than or equal to the node's childrens' keys > results in a node's key being greater than or equal to all the node's descendants' keys > Therefore, a max-heap's root always has the maximum key in the entire tree

Heapsort

a sorting algorithm that takes advantage of a max-heap's properties by repeatedly removing the max and building a sorted array in reverse order. An array of unsorted values must first be converted into a heap using heapify > max is swapped with the last element in the array and the new last element in the array which holds the max is removed, then the swapped last value at the root is percolated down the array, and the process repeats until the array is empty

tree traversal

algorithm which visits all nodes in the tree once and performs an operation on each node

binary tree

each node has up to two children, known as a left child and a right child. "Binary" means two, referring to the two children

full

every node contains 0 or 2 children

To search nodes means to:

find a node with a desired key, if such a node exists. A BST may yield faster searches than a list. Searching a BST starts by visiting the root node

Max-heap largest internal node index:

for N nodes in binary heap: Largest internal node index: floor(N / 2) - 1

binary search tree

has an ordering property that any node's left subtree keys ≤ the node's key, and the right subtree's keys > the node's key

# of percolate-down operations:

take the largest internal node index and add all the index values below it, excluding the index itself and including 0

successor

the node that comes after in the BST ordering

predecessor

the node that comes before in the BST ordering

depth

the number of edges on the path from the root to the node. The root node thus has depth 0

Heaps are typically stored:

using arrays


Conjuntos de estudio relacionados

Combo with "Chapter 21: Network Troubleshooting" and 4 others

View Set

Developmental Psychology Chapter 6, Developmental Psychology Chapter 7, Developmental Psychology Chapter 8, Developmental Psychology Chapter 9, Developmental Psychology Chapter 10, Developmental Psychology Chapter 5

View Set

chapter 9 the role of insurance video

View Set

Lesson 3: ¿Qué colores llevas?

View Set

Health & Illness 2 Final Exam--Practice Questions

View Set

Accounting Chapter 4 Learn Smart

View Set

Ch17 Preoperative Nursing Management

View Set

Jogi adatgyűjtés és feldolgozás

View Set

Chapter 9 Earth Science Test-Exam Review

View Set

Hello Huayu 1 Lesson 1 - 你叫什么名字

View Set