-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.cpp
99 lines (79 loc) · 2.53 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <cstddef>
class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};
class Solution {
public:
void preOrder(Node *root) {
if( root == NULL )
return;
std::cout << root->data << " ";
preOrder(root->left);
preOrder(root->right);
}
/* you only have to complete the function given below.
Node is defined as
class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};
*/
// C++ Function to Insert and Element in a Binary Search Tree in Class.
/* Binary Search Tree can't Have duplicates which is taken care of in the
Test cases itself. */
// --> Main Claim Used - If The Key is smaller than the current node's data then it has to be in its left subtree
// If The Key is Greater than the current node's data then it has to be in its Right subtree
// Arguments - Root of the tree,Key to be inserted
Node* insert(Node *root, int data)
{
// Creating a New node which has to be inserted using C++ constructor.
Node *p = new Node(data);
// Special case if root is NULL then We have to make root and directly return.
if (root == NULL)
{
root = p;
return root;
}
Node *front = root; // For traversal by comparing current node's data with key.
Node *tail = NULL; // It will be a tail pointing to the previous node of front as frot has to become NULL at last.
// Traversing Tree by comparing key with front pointer's data.
while(front)
{
tail = front;
if(front->data>p->data)
{
front = front->left;
}
else
{
front = front->right;
}
}
// Now tail pointer will be the parent of the Node to be inserted.
if(tail->data>p->data)
{
tail->left = p; // If tail->data is greater means new node will be its left child
}
else
{
tail->right = p; // If tail->data is smaller means new node will be its right child
}
return root; // Returning Tree.
}
}; //End of Solution