This repository provides information and code related to binary trees.
- What is a Binary Tree?
- Difference between Binary Tree and Binary Search Tree
- Time Complexity Gain compared to Linked Lists
- Depth, Height, and Size of a Binary Tree
- Traversal Methods for Binary Trees
- Types of Binary Trees
What is a Binary Tree? 🌱
A binary tree is a data structure consisting of nodes, where each node has at most two children referred to as the left child and the right child. These children are usually known as the left subtree and right subtree, respectively.
Difference between Binary Tree and Binary Search Tree 🌲
While both are binary trees, a Binary Search Tree (BST) has an additional property. In a BST, the left subtree contains nodes with values less than the parent node, and the right subtree contains nodes with values greater than the parent node.
Time Complexity Gain compared to Linked Lists ⏰
Binary trees can offer significant improvements in terms of time complexity over linked lists for certain operations. For example, searching, inserting, and deleting elements in a balanced binary search tree can be performed in O(log n) time, whereas in a linked list, these operations typically take O(n) time.
Depth, Height, and Size of a Binary Tree 📏
- Depth: The depth of a node is the number of edges on the path from the root node to that particular node.
- Height: The height of a tree is the maximum depth of any node in the tree.
- Size: The size of a tree is the total number of nodes in the tree.
Traversal Methods for Binary Trees 🚶♂️
There are several ways to traverse a binary tree:
- Inorder Traversal
- Preorder Traversal
- Postorder Traversal
- Level Order Traversal
Types of Binary Trees 🌿
A binary tree is considered complete if all levels of the tree are completely filled except possibly for the last level, which is filled from left to right.
A binary tree is considered full if every node has either 0 or 2 children.
A binary tree is considered perfect if all of its levels are completely filled.
A binary tree is considered balanced if the height of the left and right subtrees of every node differ by at most one.
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \
8 9 10 11
/ \ \ / \
12 13 14 15 16