Update Tree-Traversal.md

pull/1224/head
Ramya Korupolu 2024-06-21 16:48:53 +05:30 zatwierdzone przez GitHub
rodzic 15bc5c31a8
commit c6814da27f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 14 dodań i 1 usunięć

Wyświetl plik

@ -4,14 +4,21 @@ Tree Traversal refers to the process of visiting or accessing each node of the t
A Tree Data Structure can be traversed in following ways: A Tree Data Structure can be traversed in following ways:
- **Level Order Traversal or Breadth First Search or BFS** - **Level Order Traversal or Breadth First Search or BFS**
- **Depth First Search or DFS** - **Depth First Search or DFS**
- Inorder Traversal - Inorder Traversal
- Preorder Traversal - Preorder Traversal
- Postorder Traversal - Postorder Traversal
![Tree Traversal](images/traversal.png)
## Binary Tree Structure ## Binary Tree Structure
Before diving into traversal techniques, let's define a simple binary tree node structure: Before diving into traversal techniques, let's define a simple binary tree node structure:
![Binary Tree](images/binarytree.png)
```python ```python
class Node: class Node:
def __init__(self, key): def __init__(self, key):
@ -85,6 +92,8 @@ In this traversal method, the left subtree is visited first, then the root and l
`Note :` If a binary search tree is traversed in-order, the output will produce sorted key values in an ascending order. `Note :` If a binary search tree is traversed in-order, the output will produce sorted key values in an ascending order.
![Inorder](images/inorder-traversal.png)
**The order:** Left -> Root -> Right **The order:** Left -> Root -> Right
### Algorithm ### Algorithm
@ -116,6 +125,8 @@ def printInorder(root):
In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
![preorder](images/preorder-traversal.png)
**The order:** Root -> Left -> Right **The order:** Root -> Left -> Right
### Algorithm ### Algorithm
@ -146,6 +157,8 @@ def printPreorder(root):
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node. In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.
![postorder](images/postorder-traversal.png)
**The order:** Left -> Right -> Root **The order:** Left -> Right -> Root
### Algorithm ### Algorithm