-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcode_1.cpp
More file actions
81 lines (72 loc) · 1.51 KB
/
code_1.cpp
File metadata and controls
81 lines (72 loc) · 1.51 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
/
// code_1.cpp
// Data Structure
//
// Created by Mohd Shoaib Rayeen on 19/03/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
using namespace std;
struct bnode {
int data;
bnode *left;
bnode *right;
};
class btree {
public:
bnode *root;
btree();
void insert();
void display();
bnode* newNode(int);
void NthNodeInorder(bnode* , int);
};
btree::btree() {
root = NULL;
}
bnode* btree:: newNode(int value) {
bnode* temp=new bnode;
temp->data=value;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void btree:: insert() {
root = newNode(8);
root->left = newNode(3);
root->right = newNode(10);
root->left->left = newNode(1);
root->left->right = newNode(6);
root->right->right = newNode(14);
root->right->right->left = newNode(13);
root->left->right->left = newNode(4);
root->left->right->right = newNode(7);
}
void btree::display() {
insert();
int n;
cout << "\nEnter Value of N\t:\t";
cin >> n;
cout << "\nNth Node in Inorder\t\t\t:\t";
NthNodeInorder(root , n);
}
void btree::NthNodeInorder(bnode* node, int n) {
static int count = 0;
if (node == NULL) {
return;
}
if (count <= n) {
NthNodeInorder(node->left, n);
count++;
if (count == n) {
cout << node->data << endl;
}
NthNodeInorder(node->right, n);
}
}
int main() {
btree obj;
obj.display();
cout << "\n";
return 0;
}