I do leetcoding every once a while, but keep forgetting some tree basics. This post here it to help with that.
Types of Tree
Trees can have multiple children. Or on the special case of binary trees have only two. A binary tree is still different from a binary search tree (BST) where things follow a certain order.
Binary search tree
For a Binary tree to be a BST, it has to follow the following rules:
- left node must be smaller than parent.
- right node must be greater than parent.
left < parent < right
Order of insertion matters for BST
Let’s say you have the following numbers
[1,2,3,4] in your tree. Depending on the order you add them into the binary search tree, the tree’s visualization would be different.
A: Inserting in the following order:
[4,3,2,1] will create:
4 / 3 / 2 / 1
B: Inserting in the following order:
[4,2,1,3] will create:
4 / 2 / \ 1 3
C: Inserting in the following order:
[2,3,1,4] will create:
2 / \ 1 3 \ 4
D: Inserting in the following order:
[3,1,4,2] will create:
3 / \ 1 4 \ 2
[1,2,3,4] will create
1 \ 2 \ 3 \ 4
and so on.
All the above are a BST made of 1,2,3,4. Because all conform to the two rules. Yet each is inserted with different order.
Binary Search Traversal
You can traverse the binary tree in the same fashion. However binary trees can be traversed differently which can come in handy with binary search trees:
In Order: left (recurse), self, right(recurse): To elaborate a bit further: If the current node has a left child, recursively visit this child. Then, visit the node itself. If the current node has a right child, recursively visit this child.
Pre Order: self, left (recurse), right(recurse)
Post Order:: left (recurse), right(recurse), self
All the different ways of traversing a BST are more or less a ‘hinted DFS’. Meaning in a DFS, if you have multiple children, you just pick whichever you like and just go deeper. However with in-order, pre-order, post-order, you pick a certain node first then continue down the tree again in a fashion similar to DFS.
The term ‘order’ might put you off a bit, it’s not top to bottom like a BFS, it’s more about smallest number to biggest. Hence an in-order traversal will start from the bottom left where the smallest item is at and finish at the bottom right where the biggest item is at.
That being said not every binary tree has an order or is a BST. So alternatively as a way to memorize you can think of nodes of just left and right as numbers grow left to right.
Mapping of a Tree to an Array
Mainly two of types of the above traversal types are used for array to BST conversions:
- Level-order traversal: More accurate, but not necessarily useful. Depends on your needs.
- In-order traversal: More logical and useful. The most commonly used binary search tree traversal method is in-order:
If we have an array of [1,2,3], we can turn it into a tree as such:
2 / \ 1 3
We can also see the same tree as:
2 / \ 1 3 / \ / \ nil nil nil nil
3 / \ 1 5 \ \ 2 6
3 / \ 1 5 / \ / \ nil 2 nil 6 / \ / \ nil nil nil nil
Go back to Example 1 above. Ask yourself how is the in-order & level-order traversal among all variations of
[1,2,3,4] different from each other?
Click to see answer 👇
- in-order traversal for all will be:
- level-order would be different for each.
Use Paper when debugging your mapping/traversal codes.
- Draw a sample case
- Then iterate through it. Just like a debugger. Think you’re pausing and write down values for your properties. Then traverse and repeat.
- Often times the order of traversal becomes more clear on paper and you realize your code isn’t respecting the intended order. My algorithm was correct, yet I was getting a bad result because I the input array I created in my head was incorrect. As soon as I wrote down on paper, I realized where the conversion of the table to array was wrong. I corrected the array and validated that my algorithm works as expected…
- The more you do on paper, the better you get at visualizing things in your head.
- BST Traversal
Root: The top node.
Leaf: Any node that doesn’t have a left or right node.
Depth of a node: The number of edges from the node to the root node.
Height of a tree: The maximum number of edges from the root node to a leaf node.
Types of BST:
- Balanced tree: A tree in which the heights of the left and right subtrees of any node differ by at most 1. In Example 1, the B,C trees are balanced.
- Full binary tree: A binary tree in which every node has either 0 or 2 children.
- A complete binary tree: A binary tree in which every level, except possibly the last level, is completely filled, and all nodes in the last level are as far left as possible.
- A Degenerate tree: Every non-leaf node has just one child in a binary tree known as a Degenerate Binary tree. Example 1, the A,E trees are degenerate and considered not efficient for sorting.
See here for more.
What’s the benefit of a balanced Tree?
- A balanced tree will allow you to cut the tree into half by having half on its left and the other half on its right. Unlike a degenerate tree where 100% of the remaining nodes is still on the same side. Degenerate trees are bad for doing a binary search tree. They’re more similar to a Linked List or an array.
- Tree Printer. See here
- Array to ‘Balanced Height Binary Tree Search’ Convertor
- Traversals. See here
- There are different kinds of trees that shouldn’t be confused with one another. When it comes to binary trees, you may question why the in-order of different trees ends up the same. The answer is:, Even though insertion order the insertion order of elements was different, the logic of in-order will always lead to an in-order output.
- A balanced binary search tree is best for searching because you can cut in half.
- Differences of level-order vs in-order traversal:
- level-order maintains a 1:1 relationship with its tree visualization. It’s agnostic to order. And is just all about position. In-order is considerate of order, which is based off of position and logic.
- level-order can map
nilelements in an array back to its original position. In-order isn’t capable of properly mapping nil elements, because then a nil node will not have left/right elements. Think of our
[1,nil,3]tree. If root is
nilthen its left and right nodes wouldn’t exist. So you have to remove any
nilelement before mapping back.
- Level-order is more generic and applicable to non-binary trees as well whereas in-order traversal is only applicable to binary search trees.
So choose your traversal/mapping algorithm wisely.
- You can easily map a
[1,nil,3]using level-order mapping to:
1 / \ nil 3
[1,nil,3]using an in-order mapping can get mapped to a root node of
nil, which will create a
I was confusing the two traversals and lost a tremendous amount of time, hence this blog post.