# The Great Tree-List Recursion Problem

## Introduction

The "Great Tree List Recursion" concept involves combining binary tree structures and linked lists through recursive algorithms, creating a versatile data structure that exhibits properties of both trees and lists.

This paradigm uses a binary tree with pointers to each node's left and right children.. Additionally, the tree's leaf nodes are connected to form a doubly linked list. This duality allows for efficient traversal and manipulation of the structure.

Recursion includes a depth-first traversal of the binary tree to produce a Great Tree List.. Starting from the root, the algorithm recursively traverses the left subtree, processes the current node by updating its pointers to form a linked list segment, and proceeds to the right subtree. This procedure continues until every node has been travelled through and connected to each other.

The real power of the Great Tree List Recursion lies in its ability to perform operations that benefit from both tree-like hierarchical organization and list-like sequential access. For instance, searching for elements, insertion, and deletion can be achieved efficiently using the binary tree structure, while tasks requiring sequential processing can utilize the linked list structure.

### Great Tree List Recursion code:

Java Code:

Output:

```Converted Doubly Linked List:
1 2 3 4 5 6 7
```

Explanation:

Working of the Code:

The code creates a binary search tree, converts it to a doubly linked list in place using the Great Tree List Recursion concept, and then prints the linked list. The convertToLinkedList method recursively traverses the tree, updating pointers to create the linked list structure.

Time Complexity:

The time complexity of the algorithm is O(n), where n is the quantity of nodes in the binary tree.. This is because each node is visited exactly once during the conversion process.

Space Complexity:

The space complexity is O(h), where h is the binary tree's height. This is due to the recursive calls on the stack. When the tree is skewed and has a n (unbalanced) height, the space complexity may be O(n)., the space complexity will be closer to O(log n).

Dry Run

Main Method:

• The binary search tree is created with the structure shown above.
• An instance of TreeToListConversion is created.
• The convertToLinkedList method is called with the root of the tree.

• Initial state: prev = null
• Starting from the root (value 4):
• Recursively call convertToLinkedList on the left subtree (node with value 2).
• Recursively call convertToLinkedList on the left subtree (node with value 1).
• Since there's no left subtree, nothing happens.
• Update prev to point to the node with value 1.
• Connect a node with value 1 to the right of a node with value 2.
• Update prev to point to the node with value 2.
• Connect a node with value 2 to the right of a node with value 4.
• Recursively call convertToLinkedList on the right subtree (node with value 3).
• Since there's no left subtree, nothing happens.
• Update prev to point to the node with value 3.
• Connect a node with value 3 to the right of a node with value 2.
• Recursively call convertToLinkedList on the right subtree (node with value 6).
• Recursively call convertToLinkedList on the left subtree (node with value 5).
• Since there's no left subtree, nothing happens.
• Update prev to point to the node with value 5.
• Connect a node with value 5 to the right of a node with value 6.
• Update prev to point to the node with value 6.
• Connect a node with value 6 to the right of a node with value 4.
• Update prev to point to the node with value 4.

• The printList method is called with the leftmost node (value 1).
• The loop iterates through the linked list using the right pointers and prints the values: 1 2 3 4 5 6 7.
• Output: Converted Doubly Linked List: 1 2 3 4 5 6 7

### Great Tree List Using Stack Data Structure code

Java Code:

Output:

```Converted Doubly Linked List:
1 2 3 4 5 6 7
```

Explanation:

Time Complexity:

The time complexity of this algorithm is O(n), where n is the number of nodes in the binary tree. Every node is visited exactly once, and each node's push/pop operation on the stack takes constant time.

Space Complexity:

The stack size used for simulating the iterative process determines the space complexity. In the worst-case scenario, where the tree is skewed (unbalanced), the space complexity could reach O(n). In a balanced tree, the space complexity will be closer to O(log n) as the height of the stack is determined by the tree's height.

Dry Run

Step 1: Initializing

• The binary search tree is created with the structure shown above.
• The convertToLinkedList method is called with the root of the tree.
• The stack is empty, and the current node is initialized to the root (value 4).
• Prev, head, and current are also set to null.

Step 2: Iterative In-Order Traversal with Stack

Current node: 4

• Push node 4 onto the stack.
• Move to the left, child.

Current node: 2

• Push node 2 onto the stack.
• Move to the left, child.

Current node: 1

• Push node 1 onto the stack.
• Move to the left child (null).
• Pop node 1 from the stack.
• Update prev to point to node 1.
• Connect node 1's right to node 2.

Current node: 2

• Update prev to point to node 2.
• Connect node 2's right to node 4.
• Move to the right child.

Current node: 3

• Push node 3 onto the stack.
• Move to the left child (null).
• Pop node 3 from the stack.
• Update prev to point to node 3.
• Connect node 3's right to node 2.

Current node: 4

• Move to the right child.
• Current node: 6
• Push node 6 onto the stack.
• Move to the left, child.

Current node: 5

• Push node 5 onto the stack.
• Move to the left child (null).
• Pop node 5 from the stack.
• Update prev to point to node 5.
• Connect node 5's right to node 6.

Current node: 6

• Update prev to point to node 6.
• Connect node 6's right to node 4.
• Move to the right child.

Current node: 7

• Push node 7 onto the stack.
• Move to the left child (null).
• Pop node 7 from the stack.
• Update prev to point to node 7.
• Connect node 7's right to node 6.

Current node: null

The stack is empty, and traversal is complete.