Repository navigation
Expand file tree
/
Copy pathnlargest.cpp
More file actions
150 lines (125 loc) · 2.58 KB
/
Copy pathnlargest.cpp
File metadata and controls
150 lines (125 loc) · 2.58 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <stdint.h>
#include <queue>
#include <deque>
//#include "print.h"
using namespace std;
typedef struct Node {
int value;
Node * next;
};
typedef struct tNode {
int value;
tNode * l;
tNode * r;
};
void push_back(Node * nptr_, Node * target_) {
if (target_->next == NULL) {
target_->next = nptr_;
}
else {
push_back(nptr_, target_->next);
}
}
void traverse(Node * target_) {
if (target_->next == NULL) {
cout << target_->value << endl;
}
else {
cout << target_->value << endl;
traverse(target_->next);
}
}
void dfs(tNode * head_){
if (head_->l == NULL) {
cout << head_->value << endl;
}
else {
dfs(head_->l);
dfs(head_->r);
cout << head_->value << endl;
}
}
//bst only guarenteed if ordered. ie. no rebalancing done.
void dumbInsert(tNode * leaf, tNode * cNode) {
if (leaf->value <= cNode->value && cNode->l == NULL) {
cNode->l = leaf;
}
else if (leaf->value > cNode->value && cNode->r == NULL) {
cNode->r = leaf;
}
else {
if (leaf->value > cNode-> value) {
dumbInsert(leaf, cNode->r);
}
else {
dumbInsert(leaf, cNode->l);
}
}
}
int main() {
tNode * head = new tNode();
head->value = 10;
tNode * n1 = new tNode();
tNode * n2 = new tNode();
tNode * n3 = new tNode();
tNode * n4 = new tNode();
tNode * n5 = new tNode();
tNode * n6 = new tNode();
tNode * n7 = new tNode();
n1->value = 5;
n2->value = 6;
n3->value = 1;
n4->value = 10;
n5->value = 4;
n6->value = 11;
n7->value = 0;
dumbInsert(n1, head);
dumbInsert(n2, head);
dumbInsert(n3, head);
dumbInsert(n4, head);
dumbInsert(n5, head);
dumbInsert(n6, head);
dumbInsert(n7, head);
dfs(head);
/*
int num[] = { 3, 5, 8 ,2,4,8,10,15,37,28};
int champ, champ2, temp;
temp = champ = champ2 = 0;
for (int i = 0; i < 10; i++) {
if (num[i] > champ2) {
champ2 = num[i];
}
if (champ2 > champ) {
//swap value with temp value then bubble up new champ
temp = champ;
champ = champ2;
champ2 = temp;
}
cout << "value " << i << ": " << num[i] << endl;
}
cout << "champ of list: " << champ << endl;
cout << "runnerup: " << champ2 << endl;
*/
/* BEGIN LINKED LIST USING ABOVE DEFINITIONS.
Node * head = new Node();
head->value = 5;
Node * ptr1 = new Node();
ptr1->value = 1;
Node * ptr2 = new Node();
ptr2->value = 2;
Node * ptr3 = new Node();
ptr3->value = 3;
// =========== END INITIALIZATIION PHASE OF PROGRAM.
//begin constructing a list.
push_back(ptr1, head);
push_back(ptr2, head);
push_back(ptr3, head);
traverse(head);
*/
return 0;
}