Graphs and Trees Flashcards
Connected Graph
a graph in which any node can be reached from any other node (no islands)
Complete Graph
Every vertex is connected to every other vertex
Cyclic Graph
possible to get from a point of origin and then back to that same point (without taking the same path back)
-note: vertices can be repeated, but edges cannot
Acyclic Graph
No cycles
impossible to get from point of origin and then back without retracing your steps
Define a balanced tree
No subtrees differ in height by more than one
Define a complete tree
All levels of the tree are completely filled in, with the possible exception of the last level
Note: a heap is always a compete tree
Most common data structure for implementing a heap
an array
Equation for finding a child in a heap array
to find child of a parent node: (index * 2) + 1
Describe Heapify
- start with parent of last node in the tree
- swap that parent node with the max value of its two children
- swap all other parent nodes at the same level
- then move up to the level above
- continue this process until you have touched every level of the heap
-efficiency of heapify: O(n log n)
Describe Heapsort
-must start with a max heap
- take element from top, move to sorted portion at end of the array
- take smallest element and move to top
- sift down the small element from the top to its proper place
- repeat until finished
-efficiency of Heapsort: O(n log n)