kopia lustrzana https://github.com/animator/learn-python
Update Tree-Traversal.md
rodzic
15bc5c31a8
commit
c6814da27f
|
@ -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:
|
||||
- **Level Order Traversal or Breadth First Search or BFS**
|
||||
- **Level Order Traversal or Breadth First Search or BFS**
|
||||
- **Depth First Search or DFS**
|
||||
- Inorder Traversal
|
||||
- Preorder Traversal
|
||||
- Postorder Traversal
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
## Binary Tree Structure
|
||||
Before diving into traversal techniques, let's define a simple binary tree node structure:
|
||||
|
||||

|
||||
|
||||
```python
|
||||
class Node:
|
||||
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.
|
||||
|
||||

|
||||
|
||||
**The order:** Left -> Root -> Right
|
||||
|
||||
### 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.
|
||||
|
||||

|
||||
|
||||
**The order:** Root -> Left -> Right
|
||||
|
||||
### 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.
|
||||
|
||||

|
||||
|
||||
**The order:** Left -> Right -> Root
|
||||
|
||||
### Algorithm
|
||||
|
|
Ładowanie…
Reference in New Issue