Skip to content

Commit 38cdf99

Browse files
committed
added some additional description about static members of class
1 parent bafb04b commit 38cdf99

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

Data Structures/Trees/Binary Trees/DeepestLeftLeafNode.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,37 @@ BinaryTreeNode *DeepestLeftLeaf(struct BinaryTreeNode *root)
9292

9393

9494

95+
BinaryTreeNode *printleafTree(struct BinaryTreeNode *root,int level)
96+
{
97+
//base condition
98+
if(root==NULL) return;
99+
100+
int htl = heightTree(root->left);
101+
int htr = heightTree(root->right);
102+
if(level==0)
103+
{
104+
if(root->left==NULL && root->right==NULL)
105+
{
106+
return root;
107+
}
108+
109+
110+
}
111+
112+
else if(level > 0 )
113+
{
114+
//if order is to traverse from left to right
115+
116+
if(htl > htr)
117+
return printTree(root->left,level-1);
118+
else
119+
return printTree(root->right->left,level-1);
120+
121+
122+
//when order is to print from right to left
123+
}
124+
}
125+
95126

96127
void Insert( struct BinaryTreeNode **root,int data)
97128
{

Data Structures/Trees/Binary Trees/TraverseLevelWise(BFS).cpp

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,51 @@ void printTree(struct BinaryTreeNode *root,int level, bool ltr)
4545
}
4646

4747

48+
//function the get the height of tree
49+
int height(BinaryTreeNode *root)
50+
{
51+
if(root==NULL) return 0;
52+
53+
else return (1 + max(height(root->left),height(root->right)));
54+
}
55+
56+
BinaryTreeNode *printleafTree(struct BinaryTreeNode *root,int level)
57+
{
58+
//base condition
59+
if(root==NULL) return NULL;
60+
61+
BinaryTreeNode *l,*r;
62+
63+
int htl = height(root->left);
64+
int htr = height(root->right);
65+
if(level==0)
66+
{
67+
return root;
68+
}
69+
70+
else if(level > 0 )
71+
{
72+
//if order is to traverse from left to right
73+
74+
if(htl > htr)
75+
l = printleafTree(root->left,level-1);
76+
if(l->left==NULL && l->right==NULL)
77+
{
78+
return l;
79+
}
80+
else
81+
{
82+
r=printleafTree(root->right,level-1);
83+
if(r->left==NULL && r->right==NULL) return r;
84+
}
85+
86+
87+
88+
//when order is to print from right to left
89+
}
90+
}
91+
92+
4893

4994
void Insert( struct BinaryTreeNode **root,int data)
5095
{
@@ -98,13 +143,7 @@ void Insert( struct BinaryTreeNode **root,int data)
98143

99144

100145

101-
//function the get the height of tree
102-
int height(BinaryTreeNode *root)
103-
{
104-
if(root==NULL) return 0;
105-
106-
else return (1 + max(height(root->left),height(root->right)));
107-
}
146+
108147

109148

110149
int main()
@@ -120,7 +159,7 @@ int main()
120159

121160
for(int i = 0 ; i < height(root1); i++)
122161
{
123-
printTree(root1,i,false);
162+
cout<<printleafTree(root1,i)->data;
124163
}
125164

126165
}

StaticClass.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@ using namespace std;
77
class Human {
88
public:
99
static int scount;//cannot be defined inside a class
10-
int count;
10+
int count=0;
1111
//constructor
1212
Human() {
1313
scount++;
1414
count++;
15-
cout<<"Number of humans"<<" "<<scount<<endl;
15+
cout<<"Number of static humans"<<" "<<scount<<endl;
16+
17+
}
18+
19+
//destructor
20+
~Human()
21+
{
22+
cout<<endl;
23+
cout<<"There are now "<<scount<<" humans reamining"<<endl;
24+
scount--;
1625
}
1726

1827
static void totalHuman() {
1928
cout<<"There are "<<scount<< " "<< "people"<<endl;
29+
// cout<<"There are "<<count<< " "<< "people"<<endl;-Produces Error as static member function can only access, static class variable
2030

2131
}
2232

0 commit comments

Comments
 (0)