-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_2.cpp
97 lines (91 loc) · 2.02 KB
/
code_2.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
//
// code_2.cpp
// Data Structure
//
// Created by Mohd Shoaib Rayeen on 19/03/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <queue>
#include <vector>
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 sortedLevelOrder(bnode*);
};
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(7);
root->left = newNode(6);
root->right = newNode(5);
root->left->left = newNode(4);
root->left->right = newNode(3);
root->right->left = newNode(8);
root->right->right = newNode(1);
}
void btree::display() {
insert();
cout << "\nSorted Order\n";
sortedLevelOrder(root);
}
void btree::sortedLevelOrder(bnode* root) {
if (root == NULL) {
return;
}
queue<bnode*> q;
priority_queue<int, vector<int>, greater<int> > current_level;
priority_queue<int, vector<int>, greater<int> > next_level;
q.push(root);
q.push(NULL);
current_level.push(root->data);
while (q.empty() == false) {
int data = current_level.top();
bnode* node = q.front();
if (node == NULL) {
q.pop();
if (q.empty()) {
break;
}
q.push(NULL);
cout << "\n";
current_level.swap(next_level);
continue;
}
cout << data << "\t";
q.pop();
current_level.pop();
if (node->left != NULL) {
q.push(node->left);
next_level.push(node->left->data);
}
if (node->right != NULL) {
q.push(node->right);
next_level.push(node->right->data);
}
}
}
int main() {
btree obj;
obj.display();
cout << "\n";
return 0;
}