-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cpp
84 lines (83 loc) · 1.33 KB
/
BinaryTree.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
#include<iostream>
#include"Headers/BinaryTree.h"
#include"Headers/Queue.h"
using namespace std;
TNode* create_node(int val)
{
TNode* a=new TNode;
a->data=val;
return a;
}
TNode* BinaryTree::create(int val)
{
root=create_node(val);
return root;
}
TNode* BinaryTree::get_root()
{
return root;
}
void Inorder(TNode*a)
{
if(a!=NULL)
{
Inorder(a->left);
cout<<a->data<<" ";
Inorder(a->right);
}
}
void Preorder(TNode*a)
{
if(a!=NULL)
{
cout<<a->data<<" ";
Preorder(a->left);
Preorder(a->right);
}
}
void Postorder(TNode*a)
{
if(a!=NULL)
{
Postorder(a->left);
Postorder(a->right);
cout<<a->data<<" ";
}
}
int height(TNode*a)
{
if(a==NULL)
{
return 0;
}
else
{
int l= height(a->left);
int r= height(a->right);
if(l<r)
{
return r+1;
}
else
{
return l+1;
}
}
}
void print_row(TNode*a,int i){
if(a==NULL){
return;
}
if(i==1){
cout<<a->data<<" ";
}else{
print_row(a->left,i-1);
print_row(a->right,i-1);
}
}
void BFS(TNode*a)
{
for(int i=1;i<=height(a);i++){
print_row(a,i);
}
}