forked from rpotter12/data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmm.cpp
More file actions
95 lines (78 loc) · 1.84 KB
/
Copy pathhmm.cpp
File metadata and controls
95 lines (78 loc) · 1.84 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
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node* temp = new Node;
Node* newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
Node* leftMostNode(Node* node)
{
while (node != NULL && node->left != NULL)
node = node->left;
return node;
}
Node* rightMostNode(Node* node)
{
while (node != NULL && node->right != NULL)
node = node->right;
return node;
}
Node* findInorderRecursive(Node* root, Node* x )
{
if (!root)
return NULL;
if (root==x || (temp = findInorderRecursive(root->left,x)) ||
(temp = findInorderRecursive(root->right,x)))
{
if (temp)
{
if (root->left == temp)
{
cout << "Inorder Successor of " << x->data;
cout << " is "<< root->data << "\n";
return NULL;
}
}
return root;
}
return NULL;
}
void inorderSuccesor(Node* root, Node* x)
{
if (x->right != NULL)
{
Node* inorderSucc = leftMostNode(x->right);
cout<<"Inorder Successor of "<<x->data<<" is ";
cout<<inorderSucc->data<<"\n";
}
if (x->right == NULL)
{
int f = 0;
Node* rightMost = rightMostNode(root);
if (rightMost == x)
cout << "No inorder successor! Right most node.\n";
else
findInorderRecursive(root, x);
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
inorderSuccesor(root, root->right);
inorderSuccesor(root, root->left->left);
inorderSuccesor(root, root->right->right);
return 0;
}