Data Structures & Algorithms Flashcards
Find Height of Binary Tree
Recursive: Check to see if the root node is null, if so return 0. Otherwise, set an int left equal to a recursive call with the root’s left node, and the same for the right, which will recurse until it’s found the last node for each left and right. The function returns the max of left and right plus one for the root node, which happens for every branch off of left and right until the tree is fully traversed.
Tree
Data structure comprised of recursive nodes
Binary Tree
Tree where each node has up to 2 children
Binary Search Tree
Ordered tree where all left descendants are less than or equal to their right descendants.
Balanced Tree
Balanced enough, not perfectly balanced, so that we can ensure O(log n) insert and lookup.
Complete Binary Tree
Every node has either 0 or 2 children.
Perfect Binary Tree
Full and complete.
In Order Tree Traversal
“Visit” left, current node, right. In a Binary Search Tree, ascends in order.
Pre-Order Tree Traversal
“Visit” current node, left, right. Root will be first visited.
Post-Order Tree Traversal
“Visit” left, right, current node. Root will be last visited.
Min Heap
Complete Binary Tree where each node is smaller than its children.
Min Heap insertion
Insert at bottom rightmost location, fix by moving up into right spot and swapping nodes as you sort.
Min Heap extract
Remove top (min node) and replace with bottom rightmost node. Swap now-top node until min is inserted at top and tree is in correct order.
Tries
A prefix tree. Variant of n-ary tree with characters at each node. Each path down could be a word, with the end of the word represented by a null node.
Uses for Tries
Autocomplete. A trie could be used to store the English language and do O(n) prefix lookup, where n is the string prefix being searched.
Graph definition
A collection of nodes with edges between (some of) them.
Directed Graph
Edges point the way, like a one way street.
Undirected Graph
Edges go both ways, like a two way street.
Connected Graph
Graph with an edge between every pair of vertices.
Cyclic
Graph that cycles, end node touching root node
Acyclic
Graph that does not have a cycle
Adjacency List
Way of representing a graph where every vertex (node) stores a list of adjacent vertices (nodes).
Adjacency Matrix
A NxN boolean matrix where a true value at matrix[i][j] indicates an edge between i and j. In an undirected graph, the matrix will be symmetric, and in a directed graph, it will not necessarily be symmetric.
Depth First Search
Way of searching graphs that starts at the root (or another arbitrary node) and explores each branch completely before going to the next branch.