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.