-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTask 06.cpp
More file actions
98 lines (90 loc) · 2.73 KB
/
Task 06.cpp
File metadata and controls
98 lines (90 loc) · 2.73 KB
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>
using namespace std;
typedef struct node Node; //making life easier
struct node //structure for each node
{
int value;
Node* leftChild;
Node* rightChild;
Node* parent;
};
Node* createNode (int value) //allocates memory space to a node and sets default pointers
{
Node* newNode = new Node();
newNode->value = value;
newNode->leftChild = NULL;
newNode->rightChild = NULL;
newNode->parent = NULL;
return newNode;
}
Node* searchTree (Node* x, int value) //locating a particular node
{
if (x == NULL || x->value == value) return x; //return node with value or NULL if not found
else if (value < x->value) return searchTree(x->leftChild, value);
else if (value > x->value) return searchTree(x->rightChild, value);
}
Node* insertNode (Node* root, int value) //inserting new node
{
Node* x = root; //will need actual root later, so making a copy to traverse
Node* newNode = createNode(value); //creating the new node
if (x == NULL) return newNode; //if no root exists, the node just created must be the root
else
{
Node* parent = NULL;
while (x != NULL) //keep going down, keeping track of the current parent
{
parent = x;
x = (x->value < value) ? x->rightChild : x->leftChild;
}
newNode->parent = parent; //make parent's child as needed
if (value > parent->value) parent->rightChild = newNode;
else parent->leftChild = newNode;
return root;
}
}
void inOrderTraversal (Node* x)
{
if (x != NULL)
{
inOrderTraversal(x->leftChild);
cout<<x->value<<" ";
inOrderTraversal(x->rightChild);
}
}
int maxBetween(Node* root, int x, int y) //maximum value between two nodes
{
int maximum = x;
Node* a = searchTree(root, x); //finding first node
if (a == NULL) return -1; //if node does not exist
while (a->parent != NULL && a->parent->value <= y) //keep going up until pivot reached
{
a = a->parent;
if (maximum < a->value) maximum = a->value; //new maximum values
}
while (a->value != y) //keep going down until node two reached
{
if (y>a->value) a = a->rightChild;
else a = a->leftChild;
if (a->value > maximum) maximum = a->value;
}
return maximum;
}
int main()
{
Node* root;
root = NULL;
int i = 0;
while (i != -1)
{
cin>>i;
if (i == -1) break;
root = insertNode(root, i);
}
int n, x, y;
cin>>n;
for (int j=0; j<n; j++)
{
cin>>x>>y;
cout<<maxBetween(root, x, y)<<endl;
}
}