-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_6.cpp
132 lines (120 loc) · 2.81 KB
/
4_6.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
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
//
// 4_6.cpp
//
//
// Created by Pengyan Qin on 6/27/15.
//
//
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct node{ // declare the node
int data;
node* left;
node* right;
node* parent;
node() {}
node(int data_in):data(data_in), left(nullptr), right(nullptr){}
};
void insert(node* &root, node* parent, int dat){ // create BST
if(root == nullptr){
node *n = new node(dat);
n->parent = parent;
root = n;
return;
}
if(dat < root->data){
insert(root->left,root, dat);
}
else{
insert(root->right,root, dat);
}
}
// consider different situdation
node* next_node(node* spot){
if(spot == nullptr)
return nullptr;
if(spot->right){
node *p = spot->right;
while(p->left){
p = p->left;
}
return p;
}
node *q = spot->parent;
while(q && q->right == spot){
spot = q;
q = q->parent;
}
return q;
/*
if(spot->parent && spot->parent->left == spot)
return spot->parent;
if(spot->parent && spot->parent->right == spot){
while(!spot->parent || spot->parent->left != spot){
spot = spot->parent;
}
return spot->parent;
}
return nullptr;
*/
}
// inorder traversal, most easiest way, but must know root node, can without parent node
void inorder(node* root, vector<node*> &vec){
if(root == nullptr)
return;
if(root->left)
inorder(root->left, vec);
vec.push_back(root);
if(root->right)
inorder(root->right, vec);
}
node* next_node1(node* root, node* spot){
vector<node*> vec;
inorder(root, vec);
for(int i = 0; i < vec.size(); ++i){
if(spot == vec[i])
return vec[i+1];
}
return nullptr;
}
// inorder traversal using stack
node* next_node2(node *root, node *spot){
stack<node*> s;
bool search = false;
node* tmp = root;
while(true){
while(tmp){
s.push(tmp);
tmp = tmp->left;
}
if(s.empty())
break;
tmp = s.top();
s.pop();
if(search)
return tmp;
if(spot == tmp)
search = true;
tmp = tmp->right;
}
return nullptr;
}
int main(){
node *root = nullptr;
int a[]= {8, 3, 13, 1, 5, 9, 14, 16};
for(int i = 0; i < 8; ++i)
insert(root, nullptr, a[i]);
node *spot1 = root->left->right;
//node *find1 = next_node1(root, spot1);
//node *find1 = next_node(spot1);
node *find1 = next_node2(root,spot1);
cout << find1->data << endl;
node *spot2 = root;
//node *find2 = next_node1(root, spot2);
//node *find2 = next_node(spot2);
node *find2 = next_node2(root, spot2);
cout << find2->data << endl;
return 0;
}