Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time and space complexities added in binary trees #1437

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/binary-trees/basic-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ Inserting E at the right of B
/ / \ / \ / \
D F G D E F G
```

#### Time Complexity
- **Average Case:** O(log N) if balanced.
- **Worst Case:** O(N) for a skewed tree.

#### Space Complexity
- **Average and Worst Case:** O(N), due to recursion stack.

### 2. Deletion
In a binary tree, when deleting a node, the node to be deleted is replaced by the deepest node in the tree. This approach ensures that the tree remains complete. The deletion process involves the following steps:

Expand Down Expand Up @@ -129,6 +137,14 @@ Case # 01: Deleting A (Root Node Removal)
/ \ / \ / \ /
D E F G D E F
```

#### Time Complexity
- **Average Case:** O(log N) if balanced.
- **Worst Case:** O(N) for a skewed tree.

#### Space Complexity
- **Average and Worst Case:** O(N) due to recursion stack.

### 3. Searching
Searching for a value in a binary tree involves comparing the value with the current node’s data and then recursively searching in the left or right subtree based on the comparison.

Expand All @@ -147,6 +163,13 @@ bool search(Node* root, int val) {
}
}
```
#### Time Complexity
- **Average Case:** O(log N) if balanced.
- **Worst Case:** O(N) for a skewed tree.

#### Space Complexity
- **Average and Worst Case:** O(N), due to recursion stack.

## Complexity Analysis of Binary Tree Operations

### 1. Time Complexity
Expand Down Expand Up @@ -185,6 +208,11 @@ void preOrder(Node* root) {

Pre-Order Traversal: A B D E C F G
```
#### Time Complexity
- O(N), as every node is visited once.

#### Space Complexity
- O(N) in the worst case due to recursion stack for a skewed tree.

#### b) In-order Traversal (Left, Root, Right)
In in-order traversal, we visit the left subtree first, then the root node, and finally the right subtree. For binary search trees, in-order traversal visits nodes in ascending order.
Expand All @@ -207,6 +235,12 @@ void inOrder(Node* root) {

In-Order Traversal: D B E A F C G
```
#### Time Complexity
- O(N), as each node is visited once.

#### Space Complexity
- O(N) in the worst case due to recursion stack for a skewed tree.

#### c) Post-order Traversal (Left, Right, Root)
In post-order traversal, we visit the left subtree first, followed by the right subtree, and finally the root node.

Expand All @@ -228,3 +262,8 @@ void postOrder(Node* root) {

Post-Order Traversal: D E B F G C A
```
#### Time Complexity
- O(N), as each node is visited once.

#### Space Complexity
- O(N) in the worst case due to recursion stack for a skewed tree.
41 changes: 40 additions & 1 deletion docs/binary_trees/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: tree-algorithms-cpp
title: Tree Algorithms in C++
sidebar_label: Tree Algorithms
sidebar_position: 5
description: This document contains implementations of basic tree algorithms in C++ including traversals, BST operations, and tree properties.
description: This document contains implementations of basic tree algorithms in C++ including traversals, BST operations, and tree properties, along with time and space complexities.
tags: [C++, Trees, Algorithms]
---

Expand Down Expand Up @@ -35,6 +35,9 @@ void inorderTraversal(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h) (where h is the height of the tree, due to recursion stack)

### Pre-order Traversal

```cpp
Expand All @@ -46,6 +49,9 @@ void preorderTraversal(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

### Post-order Traversal

```cpp
Expand All @@ -57,6 +63,9 @@ void postorderTraversal(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

### Level-order Traversal (BFS)

```cpp
Expand All @@ -76,6 +85,9 @@ void levelOrderTraversal(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(n) (for the queue)

## 2. Binary Search Tree (BST) Operations

### Insertion
Expand All @@ -91,6 +103,9 @@ TreeNode* insert(TreeNode* root, int val) {
}
```

- **Time Complexity**: O(h)
- **Space Complexity**: O(h) (due to recursion)

### Search

```cpp
Expand All @@ -101,6 +116,9 @@ TreeNode* search(TreeNode* root, int val) {
}
```

- **Time Complexity**: O(h)
- **Space Complexity**: O(h)

### Finding Minimum Value

```cpp
Expand All @@ -110,6 +128,9 @@ TreeNode* findMin(TreeNode* root) {
}
```

- **Time Complexity**: O(h)
- **Space Complexity**: O(1)

## 3. Tree Properties

### Height of a Tree
Expand All @@ -121,6 +142,9 @@ int height(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

### Size of a Tree

```cpp
Expand All @@ -130,6 +154,9 @@ int size(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

## 4. Lowest Common Ancestor (LCA)

```cpp
Expand All @@ -142,6 +169,9 @@ TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

## 5. Tree Transformations

### Mirror a Binary Tree
Expand All @@ -155,6 +185,9 @@ void mirror(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

## 6. Miscellaneous

### Check if Two Trees are Identical
Expand All @@ -169,6 +202,9 @@ bool areIdentical(TreeNode* root1, TreeNode* root2) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

### Find the Diameter of a Binary Tree

```cpp
Expand All @@ -188,4 +224,7 @@ int diameterOfBinaryTree(TreeNode* root) {
}
```

- **Time Complexity**: O(n)
- **Space Complexity**: O(h)

These implementations cover the basic algorithms for tree data structures in C++. They can be used as a reference or starting point for more complex tree-related problems.
Loading