Perfect Binary Tree Visualizer
All internal nodes have exactly 2 children, and all leaf nodes are at the same level. Insert, delete and search with step-by-step animation.
How to use the Perfect Binary Tree Visualizer on KickSolve
Three quick steps, no account needed.
Why choose KickSolve
Built for learning, speed and privacy.
What you can do with the Perfect Binary Tree Visualizer
Everything this tool offers.
About the Perfect Binary Tree Visualizer on KickSolve
What it does and how to get the best results.
What is a perfect binary tree?
A perfect binary tree is a binary tree in which every internal node has exactly two children and all leaves sit on the same level. Every level is completely full.
Key properties
- All leaves are at the same depth
- Every internal node has 2 children
- A tree of height h has 2^h - 1 nodes
- Level k contains 2^(k-1) nodes
Operations and time complexity
Height is exactly log2(n + 1). Traversal and search take O(n) here, while a root to leaf path is only O(log n) long.
Real-world uses
- Tournament brackets
- Ideal balanced structures
- Analysing the best case tree height
- Segment tree layouts
Comparing tree shapes? Try the Binary Tree, Full Binary Tree, Complete Binary Tree or Balanced Binary Tree visualizer.
Worked example: building a small perfect binary tree
The same steps the visualizer animates for any input.
Insert 1: it becomes the root. Insert 2 and 3: level 2 is full. Insert 4: level 3 starts. Dashed circles show 3 empty slots, so the tree is not perfect yet. Insert 5, 6 and 7: level 3 is full, no dashed circle is left. The tree is perfect: 7 nodes = 2^3 - 1, with 4 leaves. Preorder: 1 2 4 5 3 6 7 Inorder: 4 2 5 1 6 3 7 Postorder: 4 5 2 6 7 3 1 Level order: 1 2 3 4 5 6 7
Press Add level after inserting 4 to fill the missing slots in one click.
Perfect Binary Tree traversals compared
When each node is visited relative to its children.
| Traversal | Order | Typical use |
|---|---|---|
| Preorder | Root, left, right | Copying a tree, prefix expressions |
| Inorder | Left, root, right | Sorted output from a binary search tree |
| Postorder | Left, right, root | Deleting a tree, postfix expressions |
| Level order | Level by level, left to right | Shortest path, printing by depth |
Types of binary trees
The shapes the property badges check for.
| Type | Rule |
|---|---|
| Full | Every node has 0 or exactly 2 children |
| Complete | All levels filled except the last, which fills from the left |
| Perfect | All internal nodes have 2 children and all leaves share one level |
| Balanced | Left and right heights differ by at most 1 at every node |
| Binary search tree | Left values are smaller and right values are larger |
Nodes per perfect binary tree height
A perfect tree of each height has exactly this many nodes.
| Height | Nodes | Leaves | Internal nodes |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 3 | 2 | 1 |
| 3 | 7 | 4 | 3 |
| 4 | 15 | 8 | 7 |
| 5 | 31 | 16 | 15 |
| 6 | 63 | 32 | 31 |
| 7 | 127 | 64 | 63 |
| 8 | 255 | 128 | 127 |
| 9 | 511 | 256 | 255 |
| 10 | 1023 | 512 | 511 |
Perfect Binary Tree FAQ
Quick answers.
What is a perfect binary tree?
A binary tree where every internal node has exactly two children and all leaves are on the same level. Every level is completely full.
How many nodes are in a perfect binary tree of height h?
2^h - 1 nodes, counting height in levels. Height 3 has 7 nodes and height 4 has 15.
How many leaves does a perfect binary tree have?
A perfect tree of height h has 2^(h-1) leaves and 2^(h-1) - 1 internal nodes, so more than half of the nodes are leaves.
What is the height of a perfect binary tree with n nodes?
The height is log2(n + 1) levels. A perfect tree with 15 nodes has 4 levels.
Is a perfect binary tree full and complete?
Yes. A perfect tree is both full and complete. The reverse is not true: a full or complete tree is not always perfect.
What do the dashed circles mean?
They are empty slots on the last level. The tree is perfect only when every slot is filled and no dashed circle is left.
What does Add level do?
It fills the last level with new values until the tree is perfect. If the tree is already perfect, it adds a whole new level, which doubles the leaves.
How do I add and delete nodes in the visualizer?
Insert places the value in the next free slot in level order. To delete, type the value or click a node. The last node moves into the gap, so the tree may stop being perfect until you refill it.
What is the time complexity of perfect binary tree operations?
Search and traversal take O(n) in this unordered tree. A path from the root to any leaf is only log2(n + 1) steps, so an ordered perfect tree can be searched in O(log n).
Can I undo a change?
Yes. Press Undo to step back through your last 40 changes. Clear, Random and Add level can also be undone.
Is the visualizer free and private?
Yes. It is free with no signup, and everything runs in your browser. Nothing you type is sent to a server.
More free tools from KickSolve
Other tree visualizers.