-
Notifications
You must be signed in to change notification settings - Fork 0
/
Creating Tree
95 lines (87 loc) · 1.5 KB
/
Creating Tree
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;
class Node
{
public:
Node *lchild;
int dat;
Node *rchild;
};
class Queue
{
private:
int front;
int rear;
int size;
Node **Q; //in queue we were pointing to an array, here we are pointing to a node
public:
Queue(int size)
{
this->size=size;
front=rear=-1;
Q=new Node*[this->size];
}
void enqueue(Node* x);
Node* dequeue();
void Display();
}
class Tree
{
public:
Node *root;
Tree()
{
root=NULL;
}
void CreateTree();
void Preorder(Node *p);
void Postorder(Node *p);
void Inorder(Node *p);
void Levelorder(Node *p);
void Height(Node *root);
};
void create(struct Queue *q,int size)
{
q->size=size;
q->fron=q->rear=-1;
q->Q=(int *)malloc(q->size*sizeof(int));
}
void enqueue(struct Queue *q,int x)
{
if(q->rear==q->size-1)
{
cout<<"Queue is Full!";
}
else
{
q->rear++;
Q[q->rear]=x;
}
}
void dequeue(struct Queue *q)
{
int x=-1;
if(q->rear==q->front)
{
cout<<"Queue is Empty!";
}
else
{
q->front++;
x=Q[q->front]
}
return x;
}
void display(struct Queue *q)
{
for(int i=q->front+1;i<=q->rear;i++)
cout<<Q[i]<<" ";
}
int main()
{
struct Queue q;
int size;
cout<<"Enter size";
cin>>size;
create(&q,size);
}