A tree stores data in layers. For example, a file system is a Tree:
Whenever someone says the word "tree", they're usually referring to the top node of the tree, called the "root". This is one of the largest confusions to beginners.
In an interview, you'll usually be given the simplest tree, a "Binary Tree".
Each node in a Binary Tree has 2 children left
and right
and a value val
.
We often draw the value val inside of each node (like above). But you should understand that val is a pointer just like left and right.
Reading from a tree is slow, because you might need to step through many of the nodes. You always start at the root node and walk down to other nodes.
This means reading/writing is O(n), where n is the number of nodes in the tree.
Read/Write Time O(n)
You typically read/write faster, if the tree is balanced:
This is because the tree is spread "wider", so you have to take fewer steps down. A balanced tree has a height of O(logn), so it takes O(logn) steps to reach an arbitrary element in it. See the spoiler for an explanation on why the height is O(logn).
In a balanced tree, every layer doubles in size.
This means if our tree is h layers tall, the number nodes is roughly equal to all of the nodes in the bottom layer, which is 2h.
We can use this equation to solve for the height of our tree, given the number of nodes we have. To do this, you take the log of both sides of the equation.
This tells us that the number of layers in a balaned tree is h=log(n).
Balanced Tree Height O(logn)
O(logn) is really small.
This is because if n
doubles in size, log(n)
only increases by 1.
For example: