A general tree data structure is composed of nodes with children nodes. The first/top node is called the root node . This chapter will explore many different types of trees such as binary trees, binary search trees, and self-balancing binary search trees. First, this chapter will cover what trees are and how they are structured. Then, it will cover methods of traversing the tree data structure in detail. Finally, you will learn about binary search trees and self-balancing binary search trees to understand how to store easily searchable data.
General Tree Structure

Generalized tree with any number of children
Binary Trees

Binary tree
Tree Traversal
Traversal through an array is simple: you access the tree using the index and increment the index until the index reaches the size limit. With trees, the left and right pointers have to be followed in order to go through every element in the tree. There are various ways to do this, of course; the most popular traversal techniques are pre-order traversal, post-order traversal, in-order traversal, and level-order traversal.
All the code for tree traversals is available on GitHub.1
Pre-order Traversal

Pre-order traversal
Here is the result: [42, 41, 10, 40, 50, 45, 75].
In-Order Traversal

In-order traversal
Here is the result of this traversal : [10, 41, 40, 42, 45, 50, 75].
Post-order Traversal

Post-order traversal
Here is the result : [10, 40, 41, 45, 75, 50, 42].
Level-Order Traversal

Level-order traversal
Here is the result: [42, 41, 50, 10, 40, 45, 75].
Tree Traversal Summary
If you know you need to explore the roots before inspecting any leaves, choose pre-order traversal because you will encounter all the roots before all of the leaves.
If you know you need to explore all the leaves before any nodes, choose post-order traversal because you don’t waste any time inspecting roots when searching for leaves.
If you know that the tree has an inherent sequence in the nodes and you want to flatten the tree into its original sequence, then you should use an in-order traversal. The tree would be flattened in the same way it was created. A pre-order or post-order traversal might not unwind the tree back into the sequence that was used to create it.
Time Complexity: O(n)
The time complexity of any of these traversals is the same because each traversal requires that all nodes are visited.
Binary Search Trees
Binary search trees (BSTs) also have two children, left and right. However, in a binary search tree, the left child is smaller than the parent, and the right child is bigger than the parent. BSTs have this structure because this property enables for searching, inserting, and removing specific values with O(log2(n)) time complexity.

Binary search tree

Unbalanced binary search tree
Insertion
Time Complexity (for balanced trees): O(log2(n))
Time Complexity (for unbalanced trees): O(n)
Time complexity is dependent on the height of the binary search tree.
Deletion
Case 1: The node has no children.
This is the simplest case. If the node has no child, return null. That node has been deleted now.
Case 2: The node has one child.
If the node has one child, simply return the existing child. That child has now bubbled up and replaced the parent.
Case 3: The node has two children.
If the node has two children, either find the maximum of the left subtree or find the minimum of the right subtree to replace that node.
Time Complexity (for balanced tree): O(log2(n))
Time Complexity (for unbalanced trees): O(n)
Time complexity for deletion is also O(log2(n)) because at most that’s the height that will need to be traversed to find and delete the desired node.
Search
Time Complexity (for balanced tree): O(log2(n))
Time Complexity (for unbalanced trees): O(n)
Note that all of the operations’ time complexities are equal to the height of the binary tree search. With unbalanced binary search trees, the time complexity is high. To address this, there are families of binary search trees that ensure the height is balanced. One example of such self-balancing trees is an AVL tree.
AVL Trees
Single Rotation
AVL trees rotate their children to maintain balance after insertion.
Rotate Left

Rotate left before

Rotate left after
Rotate Right

Rotate right before

Rotate right after
Double Rotation
If an AVL tree is still unbalanced after one rotation, it has to rotate twice for full balance.
Rotate Right Left (Right Then Left)

A situation where rotating right and then rotating left is appropriate

Rotate right first

Rotate left after
Rotate Left Right (Left Then Right)

A situation where rotating left and then rotating right is appropriate

Rotate left first

Rotate right after
Balancing the Tree
Insertion
Time Complexity: O(nlog2(n))
Space Complexity: O(nlog2(n))
Space complexity is from the recursive call stacks in memory.
Deletion
The time complexity and space complexity are both O(nlog2(n)) because AVL trees are balanced. The space complexity is from the recursive call stacks in memory.
Putting It All Together: AVL Tree Example

AVL result

BST result
Clearly, this is a skewed binary search tree that is completely unbalanced. At this point, it looks like a linked list. Once the tree becomes completely unbalanced like this, it has a linear time complexity for deletion, insertion, and search instead of logarithmic time.
Summary
Tree Summary
Operation | Best (If Balanced) | Worst (If Completely Unbalanced) |
---|---|---|
Deletion | O(log2(n)) | O(n) |
Insertion | O(log2(n)) | O(n) |
Search | O(log2(n)) | O(n) |
Exercises
You can find all the code for the exercises on GitHub.2
FIND THE LOWEST COMMON ANCESTOR OF TWO NODES IN A GIVEN BINARY TREE
The logic for this one is actually fairly simple but hard to notice at first.

Lowest common ancestor, example 1

Lowest common ancestor, example 2
Time Complexity: O(log2(n))
PRINT NODES NTH DISTANCE FROM THE ROOT
CHECK WHETHER A BINARY TREE IS A SUBTREE OF ANOTHER TREE
CHECK WHETHER A TREE IS A MIRROR OF ANOTHER TREE

Mirror trees
Their root node’s key must be the same.
The left subtree of root of a and the right subtree root of b are mirrors.
The right subtree of a and the left subtree of b are mirrors.