-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytrees.cpp
83 lines (48 loc) · 1.38 KB
/
binarytrees.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
#include "binarytrees.h"
#include<iostream>
#include<conio.h>
using namespace std;
BinaryTrees::BinaryTrees(TreeNode* initialroot)
{
root =initialroot;
}
void BinaryTrees:: print(){
printhelper(root);
}
bool BinaryTrees ::contains(int checkdata){
return containshelper(checkdata, root);
}
bool BinaryTrees :: containshelper(int checkdata, TreeNode * node){
if (node==NULL){
return false;
}
else if(checkdata==node->data){
return true;
}
else{
return containshelper(checkdata,node->left)||
containshelper(checkdata,node->right);
}
}
void BinaryTrees :: printhelper(TreeNode * node){
// There is a implicit base case in which if there is a node which is nu
if(node!=NULL){
//If you are printing at the top then it is pre-order
printhelper(node->left);
cout<<node->data<<endl;
//If it is in the middle then it is in-order
printhelper(node->right);
//If it is in the end then it is post order
}
}
void BinaryTrees :: printsideways(){
string len="";
printhelpersideways(root,""+len);
}
void BinaryTrees :: printhelpersideways(TreeNode * Node, string len){
if(Node!=NULL){
printhelpersideways(Node->left,len+" ");
cout<<len<<Node->data<<endl;
printhelpersideways(Node->right,len+" ");
}
}