Skip to content

Commit

Permalink
Binary Tree Intro
Browse files Browse the repository at this point in the history
  • Loading branch information
Khawaja Fashi Ud Din Abdullah committed Oct 5, 2024
1 parent 51225b6 commit 0073774
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions docs/Binary Trees/binary-tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
id: binary-tree-intro
sidebar_position: 10
title: Binary Trees
sidebar_label: Binary Trees
description: "In this blog post, we'll explore binary trees, a fundamental data structure in computer science that enables efficient data organization and retrieval."
tags: [dsa, data structures, binary trees]
---

## Introduction
Binary trees are a fundamental data structure used to represent hierarchical relationships between elements. Each node in a binary tree has at most two children, referred to as the left child and the right child. This structure allows for efficient searching, insertion, and deletion operations, making binary trees an essential concept in computer science.

## Definition and Structure
A binary tree consists of nodes, where each node contains:
- **Data:** The value stored in the node.
- **Left Child:** A reference to the left subtree (or null if no left child exists).
- **Right Child:** A reference to the right subtree (or null if no right child exists).

The tree begins with a single node called the **root**. The hierarchical structure allows for organized storage and retrieval of data.

## Properties
Key characteristics of binary trees include:
- **Height:** The length of the longest path from the root to a leaf node. The height of an empty tree is -1, and the height of a tree with only one node is 0.
- **Depth:** The distance from the root to a specific node. The root node has a depth of 0.
- **Balance:** A tree is considered balanced if the heights of the left and right subtrees of any node differ by at most one.
<pre> A
/ \
B C
/ / \
D F G
</pre>
**Height of the tree**: 2

**Depth of D, E, F**: 2

**Balanced**: Yes, the tree is balanced.


## Types of Binary Trees
1. **Full Binary Trees:** Every node has either 0 or 2 children.
<pre> A
/ \
B C
/ \
D E
</pre>

2. **Complete Binary Trees:** All levels are completely filled except possibly for the last level, which is filled from left to right.
<pre> A
/ \
B C
/ \ /
D E F
</pre>
3. **Perfect Binary Trees:** All internal nodes have two children, and all leaf nodes are at the same level.
<pre> A
/ \
B C
/ \ / \
D E F G
</pre>
4. **Balanced Binary Trees:** The heights of the two child subtrees of any node differ by at most one (e.g., AVL Trees, Red-Black Trees).
<pre> A (d=1)
/ \
(d=0) B C (d=0)
/ \
(d=0) F G (d=0)

**Depth of a node(d)=[height of left child - height of right child]**</pre>


5. **Degenerate Trees:** Each parent node has only one child, essentially behaving like a linked list.
<pre>
A
\
B
\
C
\
D</pre>
## Advantages and Disadvantages
**Advantages:**
- Efficient searching, insertion, and deletion operations.
- Provides a hierarchical representation of data.
- Allows for ease of traversal using various methods (in-order, pre-order, post-order).

**Disadvantages:**
- Can become unbalanced, leading to degraded performance (e.g., O(n) in the worst case).
- Memory overhead for pointers, as each node must store references to its children.

## Implementation

Let us see how to implement binary search in C++:
```cpp
struct Node {
int data;
Node* left;
Node* right;

Node(int val) {
data = val;
left = nullptr;
right = nullptr;
}
};
int main(){

Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
// 1
// / \
// 2 3
// / / \
// 4 5 6

}
```



0 comments on commit 0073774

Please sign in to comment.