View on GitHub

reading-notes

Trees

there are some common properties among all trees :

Traversals

traversing a tree is important it allows us to do many things and make it easy to deal with nodes , there are two way to traverse a tree:

Binary Tree Vs K-ary Trees

Binary tree: the number is resticted to two in each node which are the left and right

K-ary Trees: trees that it nodes have more than two childeren in it, in this type of the K is to refer to the maximum number of children that each Node is able to have.

Breadth First Traversal for the k-ary trees:
enqueue root -> dequeue root -> enqueue root children and so on until it reach the leaf nodes it will dequeue them (level by level).

Adding a node

there are no structural rules for where nodes are “supposed to go” in a binary tree, it really doesn’t matter where a new node gets placed.
One strategy for adding a new node to a binary tree is to fill all “child” spots from the top down, we use the breadth first traversal, the first node that doesn’t have its childs filled we will insert the new node to be its child.

Big O:
time complexity for inserting a new node is O(n). Searching for a specific node will also be O(n)
space complexity for a node insertion using breadth first insertion will be O(w), where w is the largest width of the tree.

Binary Search Trees (BST)

in BST the nodes are organized in a way that all values that are smaller than the root are on the left, and all values that are larger than the root are on the right.

Searching a BST
compare the node you are searching for against the root of the tree or sub-tree. If the value is smaller, you only traverse the left side. If the value is larger, you only traverse the right side.
The Big O time complexity of a Binary Search Tree’s insertion and search operations is O(h), or O(height).
The Big O space complexity of a BST search would be O(1)