Midterm Flashcards
A _________ binary tree is a binary tree where every interior node has exactly 2 children.
Full binary tree
A _________ binary tree is a binary tree where every interior node has exactly 2 children and all leaves are at the same depth except for the deepest level which has all leaves as far left as possible.
Complete binary tree
A _________ binary tree is a binary tree where every interior node has exactly 2 children and all leaves are at the same depth.
Perfect Binary Tree
If a perfect binary tree as height of 10, how many leaves are there?
2^10
If a perfect binary tree as height of 10, how many nodes are there?
2^11 - 1
If a complete binary tree has a height of 3 and 3 leaves at the deepest level, how many nodes are there?
2^3 - 1 + 3 … its perfect through height 2, so that is (2^3 -1). Then the bottom depth has 3 nodes, so add 3.
What are the height constraints on trees (minimum and maximum)?
A tree has the smallest height when it is complete or perfect which leads to height of approximately log(n). The worst case scenario is a non-complete tree where there is one node per height which leads to height of n.
How do you remove a node with 1 subtree?
Check if the removed node is its parent’s left or right. Check if removed node’s subtree is its left or right. Then make removed node’s subtree a child of removed node’s parent.
How do you remove a leaf node?
Set its parent’s left or right to None.
How do you remove a node with 2 subtrees?
Find inorder successor (S) and successor’s parent (PS). Set S’s left to removed node’s (N) left. If S is not N’s right, updated PS’s left to whatever S’s right was. Then update S’s right to N’s right. Lastly, update removed node’s parent (PN) to reflect its new child, S.
What’s the difference between data structures and ADTs?
Data structures define how data is stored in memory. ADT is a specification that describes how we can interact with a data structure.
What are the differences between a C array and Python list?
Arrays store one type of data, Arrays can only read/write a value at an index, array size must be declared at compile time, and size of array cannot be changed at run time.
If an array memory location starts at 200 and each element required 10 bytes, how do we access the 5th element?
200 + (10*4) = starting memory location of 5th element (4th index)
What is amortized analysis?
The method for analyzing a given operation’s complexity or how much of a resource (time/memory) it needs to execute by computing the average cost over the entire sequence.
What is aggregate analysis?
What is Encapsulation?
A mechanism thru which we hide the internal details of a data type from the user.
What do __iter__() and __next__() do?
__iter__ create a reference tp the self object and establishes an index counter. __next__ tries to get the next indexed item and returns the value / iterates index counter. If index out of range, __next__ raises a StopIteration exception.