Data Structures Flashcards
What is the height of a node in a tree?
the number of edges on the longest path between that node and a leaf.
any leaf node will have height 0
What is a tree?
it is a hierarchical data structure made of nodes and edges that:
- all nodes are connected
- there are no cycles
- each node has only one parent
What are the 3 ways if traversing a binary tree by depth?
PreOrder: evaluate the node once your reach it
InOrder: evaluate the node once you evaluated all left subtree
PostOrder: evaluate the node once you evaluated all right subtree
What is the average and worst time complexity of insert, search, delete on a binary search tree?
Average: O(h) where h is the tree height
Worst : O(n)
For all operations
O(h) is also O(logN)
What is a Full binary tree?
is a binary tree in which all nodes except leaves have two children
What is a Complete binary tree?
Binary Tree is complete if all levels are completely filled except possibly the last level and the last level has all keys as left as possible
What is a Perfect binary tree?
It is a binary tree in which all internal nodes have 2 children and every leaf node is on the same level.
What is the depth of a node in a tree?
is the number of edges from the node to the tree’s root node.
A root node will have a depth of 0.
What is a BST?
Binary Search Tree
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
What are the three abstract data types explained by Skiena? What are their difference?
Containers, dictionary and priority queue
Containers allow storage and retrieval of elements independent of the content (lists, arrays, trees)
Dictionaries stores and retrieves based on a key.
What is the advantage of priority queue over a normal sorted list?
Priority queue is more efficient for inserting items after everything is sorted.
It can be done in O(logn) time.
What are the two most common ways of collision resolution in a hash map? Explain.
Chaining: where we keep an array of linked lists. Waste memory with pointers on the linked list
Open memory: we keep an array of elements. When there is a collision we search for the next free spot. Deletion can be tricky.
What is the degree of a vertex/node in a graph?
It is the number of incident edges on a node, with loops counted twice.
What are the 3 main ways of representing graphs in programming? Explain each.
- Adjacency List: Vertices are stored as objects, and every vertex stores a list of adjacent vertices. Allows the storage of additional data on the vertices because it is an object. Additional data can be stored if edges are also stored as objects, in which case each vertex stores its incident edges and each edge stores its incident vertices.
- Adjacency Matrix: A two-dimensional matrix, in which the rows represent source vertices and columns represent destination vertices. Data on edges and vertices must be stored externally. Only the cost for one edge can be stored between each pair of vertices.
- Incidence Matrix: A two-dimensional Boolean matrix, in which the rows represent the vertices and columns represent the edges. The entries indicate whether the vertex at a row is incident to the edge at a column.
Which structure is better for representing sparse graphs? Why?
Adjacency List, because it uses space more effectively than the Adjacency Matrix, since the latter will have many empty spots in the matrix.