Skip to content

Commit 3f46353

Browse files
Add files via upload
1 parent ce55883 commit 3f46353

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
struct node {
7+
8+
int data;
9+
struct node *left;
10+
struct node *right;
11+
12+
};
13+
14+
struct node* insert( struct node* root, int data )
15+
{
16+
17+
if(root == NULL)
18+
{
19+
20+
struct node* node = (struct node*)malloc(sizeof(struct node));
21+
22+
node->data = data;
23+
24+
node->left = NULL;
25+
node->right = NULL;
26+
return node;
27+
28+
}
29+
else
30+
{
31+
32+
struct node* cur;
33+
34+
if(data <= root->data)
35+
{
36+
cur = insert(root->left, data);
37+
root->left = cur;
38+
}
39+
else
40+
{
41+
cur = insert(root->right, data);
42+
root->right = cur;
43+
}
44+
45+
return root;
46+
}
47+
}
48+
49+
50+
int height(struct node* node)
51+
{
52+
if (node==NULL)
53+
return 0;
54+
else
55+
{
56+
57+
int lheight = height(node->left);
58+
int rheight = height(node->right);
59+
60+
61+
if (lheight > rheight)
62+
return(lheight+1);
63+
else return(rheight+1);
64+
}
65+
}
66+
67+
68+
void printGivenLevel(struct node* root, int level)
69+
{
70+
if (root == NULL)
71+
return;
72+
if (level == 1)
73+
printf("%d ", root->data);
74+
else if (level > 1)
75+
{
76+
printGivenLevel(root->left, level-1);
77+
printGivenLevel(root->right, level-1);
78+
}
79+
}
80+
void levelOrder( struct node *root)
81+
{
82+
int h = height(root);
83+
int i;
84+
for (i=1; i<=h; i++)
85+
printGivenLevel(root, i);
86+
}
87+
88+
//Aditya Seth
89+
int main()
90+
{
91+
92+
struct node* root = NULL;
93+
94+
int t;
95+
int data;
96+
97+
scanf("%d", &t);
98+
99+
while(t-- > 0)
100+
{
101+
scanf("%d", &data);
102+
root = insert(root, data);
103+
}
104+
105+
levelOrder(root);
106+
return 0;
107+
}
108+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <cmath>
2+
#include <iostream>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
class Node {
8+
public:
9+
int data;
10+
Node *left, *right;
11+
12+
Node(int d) {
13+
data = d;
14+
left = right = NULL;
15+
}
16+
};
17+
18+
class Solution {
19+
public:
20+
Node *insert(Node *root, int data) {
21+
if (root == NULL) {
22+
return new Node(data);
23+
} else {
24+
Node *cur;
25+
if (data <= root->data) {
26+
cur = insert(root->left, data);
27+
root->left = cur;
28+
} else {
29+
cur = insert(root->right, data);
30+
root->right = cur;
31+
}
32+
return root;
33+
}
34+
}
35+
36+
void levelOrder(Node *root) {
37+
queue<Node *> q;
38+
q.push(root);
39+
40+
while (q.size() != 0) {
41+
Node *curr = q.front();
42+
q.pop();
43+
44+
cout << curr->data << " ";
45+
46+
if (curr->left) q.push(curr->left);
47+
if (curr->right) q.push(curr->right);
48+
}
49+
}
50+
};
51+
//Aditya Seth
52+
int main() {
53+
Solution myTree;
54+
Node *root = NULL;
55+
int T, data;
56+
cin >> T;
57+
while (T-- > 0) {
58+
cin >> data;
59+
root = myTree.insert(root, data);
60+
}
61+
myTree.levelOrder(root);
62+
return 0;
63+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.right = self.left = None
4+
self.data = data
5+
6+
7+
class Solution:
8+
def insert(self, root, data):
9+
if root is None:
10+
return Node(data)
11+
else:
12+
if data <= root.data:
13+
cur = self.insert(root.left, data)
14+
root.left = cur
15+
else:
16+
cur = self.insert(root.right, data)
17+
root.right = cur
18+
return root
19+
20+
def levelOrder(self, root):
21+
queue = [root]
22+
while len(queue) is not 0:
23+
curr = queue[0]
24+
queue = queue[1:]
25+
print(str(curr.data) + " ", end="")
26+
27+
if curr.left is not None:
28+
queue.append(curr.left)
29+
if curr.right is not None:
30+
queue.append(curr.right)
31+
32+
#Aditya Seth
33+
T = int(input())
34+
myTree = Solution()
35+
root = None
36+
for i in range(T):
37+
data = int(input())
38+
root = myTree.insert(root, data)
39+
myTree.levelOrder(root)

0 commit comments

Comments
 (0)