Sie sind auf Seite 1von 9

#include<iostream>

#include "BinaryTreeNode.h"
#include "queue_node_template.h"
using namespace std;

BinaryTreeNode<int>* takeInputLevelWise()
{
int rootData;
cout<<"Enter Root Data : ";
cin>>rootData;
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);

queue_node_temp<BinaryTreeNode<int>*> pendingNodes;
pendingNodes.enqueue(root);

while(!pendingNodes.isEmpty())
{
BinaryTreeNode<int>* currentNode = pendingNodes.dequeue();
cout<<"Enter Left Child for "<<currentNode->data<<" : ";
int LeftChildData;
cin>>LeftChildData;

if(LeftChildData!=-1)
{
BinaryTreeNode<int>* child = new BinaryTreeNode<int>(LeftChildData);
currentNode->left = child;
pendingNodes.enqueue(child);
}

cout<<"Enter Right Child for "<<currentNode->data<<" : ";

int RightChildData;
cin>>RightChildData;

if(RightChildData!=-1)
{
BinaryTreeNode<int>* child = new BinaryTreeNode<int>(RightChildData);
currentNode->right = child;
pendingNodes.enqueue(child);
}

}
return root;
}

void print(BinaryTreeNode<int>* root)


{
if(root == NULL)
{
return;
}
cout<<root->data<<" ";
print(root->left);
print(root->right);
}

// Time Complexity = O(n)


int height(BinaryTreeNode<int>* root)
{

// PreOrder

if(root == NULL)
{
return 0;
}

return 1+max(height(root->left), height(root->right));


}

// Time Complexity = O(n)


int count(BinaryTreeNode<int>* root)
{
if(root == NULL)
{
return 0;
}
return 1+ count(root->left) + count(root->right);
}

// Time Complexity = O(n)


BinaryTreeNode<int>* search(BinaryTreeNode<int>* root , int info)
{
BinaryTreeNode<int>* leftSearch;
BinaryTreeNode<int>* rightSearch;
if(root == NULL)
{
return NULL;
}

if(root->data == info)
{

return root;
}
else
{
leftSearch = search(root->left , info);
rightSearch = search(root->right , info);
}

if(leftSearch == NULL && rightSearch == NULL)


{
return NULL;
}
if(leftSearch !=NULL)
{
return leftSearch;
}
if(rightSearch != NULL)
{
return rightSearch;
}

// Diameter is the maximum distance b/w any 2 nodes(leaves) (max number of


edges)

// Time Complexity = O(n*h) where h=height of tree


int diameter (BinaryTreeNode<int>* root)
{

if(root == NULL)
{
return 0;
}

int option1 = height(root->left) + height(root->right);


int option2 = diameter(root->left);
int option3 = diameter(root->right);

return max(option1, max(option2, option3));


elements

// max function can take only 2

// BETTER METHOD
//
// Time Complexity = O(n)
class DoubleInt
{
public:
int diameter;
int height;
};

DoubleInt diameterFaster(BinaryTreeNode<int>* root)


{
if(root == NULL)
{
DoubleInt output;
output.diameter = 0;

output.height = 0;
return output;
}
DoubleInt leftOutput = diameterFaster(root->left);
DoubleInt rightOutput = diameterFaster(root->right);
int treeHeight = 1+ max(leftOutput.height , rightOutput.height);

int option1 = leftOutput.height + rightOutput.height;


int option2 = leftOutput.diameter;
int option3 = rightOutput.diameter;

int treeDiameter = max(option1, max(option2, option3));


DoubleInt output;
output.diameter = treeDiameter;
output.height = treeHeight;

return output;
}

//

void preorder(BinaryTreeNode<int>* root)


{
if(root == NULL)
{
return;
}
cout<<root->data <<" ";
preorder(root->left);
preorder(root->right);

void postorder(BinaryTreeNode<int>* root)


{
if(root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout<<root->data <<" ";
}

void inorder(BinaryTreeNode<int>* root)


{
if(root == NULL)
{
return;
}
inorder(root->left);
cout<<root->data <<" ";
inorder(root->right);
}

// TODO Later on your Own


/*
BinaryTreeNode<int>* ConstructTree (int pre[], int in[] , int start , int n)
{
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(pre[start]);

int i=0;
int mid;
while(in[i] != pre[start])
{
i++;
}
mid = i;

int preNewLeft[10] , inNewLeft[10],preNewRight[10] , inNewRight[10];

int j=0;
while(j < i)
{
inNewLeft[j] = in[j];
j++;
}

while( j < n)
{
inNewRight[j] = in[j];
j++;
}

root->left = ConstructTree(preNewLeft , inNewLeft , start+1 , mid-1);


root->right = ConstructTree(preNewRight , inNewRight , mid+1 , n);

return root;

}
*/

int main()
{
/*
BinaryTreeNode<int>* root = takeInputLevelWise();
print(root);
cout<<endl;

BinaryTreeNode<int>* Element = search(root, 4);


if(Element == NULL)
{
cout<<"Not Found"<<endl;
}
else
{
cout<<"Found Element "<<Element->data<<endl;
}
*/

int pre[] = {1 ,2, 4, 5, 3, 6, 7};


int in[] = {4, 2, 5, 1, 6, 3, 7};
/*
BinaryTreeNode<int>* RT = ConstructTree(pre , in , 0 , 7);
cout<<RT->data<<endl;
*/

return 0;
}

Das könnte Ihnen auch gefallen