Skip to content

Commit 90582a2

Browse files
authored
Add files via upload
1 parent ce6b039 commit 90582a2

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'''
2+
Aim: To perform level-order traversal on a BST.
3+
4+
'''
5+
6+
# initializing the tree
7+
class Node:
8+
def __init__(self,data):
9+
self.right=self.left=None
10+
self.data = data
11+
12+
class Solution:
13+
# inserting node
14+
def insert(self,root,data):
15+
if root==None:
16+
return Node(data)
17+
else:
18+
if data<=root.data:
19+
cur=self.insert(root.left,data)
20+
root.left=cur
21+
else:
22+
cur=self.insert(root.right,data)
23+
root.right=cur
24+
return root
25+
# performing level order traversal
26+
# the values will be printed according to the level they are in
27+
def levelOrder(self,root):
28+
if root is None:
29+
return
30+
q = []
31+
q.append(root)
32+
while len(q) !=0:
33+
p = q.pop(0)
34+
print(p.data, end=' ')
35+
if p.left is not None:
36+
q.append(p.left)
37+
if p.right is not None:
38+
q.append(p.right)
39+
return q
40+
41+
# getting the input
42+
T=int(input())
43+
myTree=Solution()
44+
root=None
45+
for i in range(T):
46+
data=int(input())
47+
root=myTree.insert(root,data)
48+
# printing the result
49+
myTree.levelOrder(root)
50+
51+
'''
52+
COMPLEXITY:
53+
54+
Time Complexity -> O(N)
55+
Space Complexity -> O(N)
56+
57+
Sample Input:
58+
6
59+
3
60+
5
61+
4
62+
7
63+
2
64+
1
65+
Sample Output:
66+
3 2 5 1 4 7
67+
68+
Explaination:
69+
The BST looks something like this:
70+
71+
3 Level 0
72+
2 5 Level 1
73+
1 4 7 Level 2
74+
75+
So, starting from level 0 and going to level 2, traversal will look like: 3, 2 5, 1 4 7.
76+
77+
'''

Diff for: Trees/BST Level Order Traversal/Images/ss.png

21.5 KB
Loading

Diff for: Trees/BST Level Order Traversal/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# BST - Level Order Traversal
2+
3+
## Aim
4+
5+
To perform Level Order Traversal on a Binary Search Tree.
6+
7+
8+
## Purpose
9+
10+
The purpose is to explore a type of traversal for BST.
11+
12+
13+
## Short description of package/script
14+
15+
- The user enters the numbers to be used for constructing a BST.
16+
- The BST is constructed according to the rules, with each node having an element entered by the user.
17+
- Level by level the numbers are displayed.
18+
19+
20+
## Workflow of the Project
21+
22+
- The tree is initialized.
23+
- Nodes are inserted.
24+
- Level order traversal is performed.
25+
- The values will be printed according to the level they are in.
26+
27+
28+
## Compilation Steps
29+
30+
After the script is run, enter:
31+
32+
1. Total number of nodes.
33+
2. All the nodes one by one.
34+
35+
That's it! The numbers will accordingly get printed.
36+
37+
38+
## Output
39+
40+
<img src="../BST Level Order Traversal/Images/ss.png">
41+
42+
43+
## Author
44+
45+
[Manasi Chhibber](https://github.com/Manasi2001)

0 commit comments

Comments
 (0)