reading-notes

View the Project on GitHub Abu-laban/reading-notes

Trees

check

Terminology

Traversals

  1. Depth First : is where we prioritize going through the depth (height) of the tree first
  1. Breadth First: iterates through the tree by going through each level of the tree node-by-node.
    • Traditionally, breadth first traversal uses a queue (instead of the call stack via recursion) to traverse the width/breadth of the tree.
    • Dequeue the front node, enqueue that node’s left and right nodes, and move to the next new front of the queue.

Binary Tree Vs K-ary Trees

Big O

The Big O time complexity for inserting a new node isO(n). Searching for a specific node will also be O(n).

The Big O space complexity for a node insertion using breadth first insertion will be O(w), where w is the largest width of the tree. For example, in the above tree, w is 4.

A “perfect” binary tree is one where every non-leaf node has exactly two children. The maximum width for a perfect binary tree, is 2^(h-1), where h is the height of the tree. Height can be calculated as log n, where n is the number of nodes.

A Binary Search Tree (BST)