Jun 26, 2017

Print Diameter (max path) of a tree


#include <iostream>
#include <string.h>
using namespace std;

int max(int p, int q)
{
 return ((p>q) ? p : q);
}

int max3(int p, int q, int r)
{
 return ((p>q) ? (p>r? p:r) : (q>r ? q:r));
}
class node;
class node
{
 public:
  char c;
  node* l;
  node* r;
};

void insert(node** root, char c)
{
 if(!(*root))
 {
  node* t = new node();
  t->c = c;
  t->l=NULL;
  t->r=NULL;
  *root = t;
  //cout << "root=" << (*root)->c << endl;
 }
 else
 {
  if((*root)->c > c)
  {
   if(!(*root)->l)
   {
    node* t = new node();
    t->c = c;
    t->l=NULL;
    t->r=NULL;
    (*root)->l = t;
    //cout << "left=" << (*root)->l->c << endl;
   }
   else
    insert(&((*root)->l), c);
  }
  else
  {
   if(!(*root)->r)
   {
    node* t = new node();
    t->c = c;
    t->l=NULL;
    t->r=NULL;
    (*root)->r = t;
    //cout << "right=" << (*root)->r->c << endl;
   }
   else
    insert(&((*root)->r), c);
  }
 }
}

void printinorder(node* root)
{
 if(!root)
 {
  return;
 }
 
 printinorder(root->l);
 cout << root->c << " ";
 printinorder(root->r);
}

int depth(node* root)
{
 if(!root)
  return 0;
 return 1 + max(depth(root->l), depth(root->r));
}

void printaltbylevel(node* root, int depth, int level)
{
 if(!root)
  return;
  
 if(depth == level)
 {
  cout << root->c;
 }
 else
 {
  if(depth%2 == 0)
  {
   printaltbylevel(root->l, depth, level+1);
   printaltbylevel(root->r, depth, level+1);
  }
  else
  {
   printaltbylevel(root->r, depth, level+1);
   printaltbylevel(root->l, depth, level+1);
  }
 }
}

void printbylevel(node* root, int depth, int level)
{
 if(!root)
  return;
  
 if(depth == level)
 {
  cout << root->c;
 }
 else
 {
  printbylevel(root->l, depth, level+1);
  cout<<"  ";
  printbylevel(root->r, depth, level+1);
 }
}

void printlevelorder(node *root)
{
 int d = depth(root);
 for(int i=0; i<d; i++)
 {
  for(int j=0;j<d-i;j++)
   cout << " ";
  printbylevel(root, i, 0);
  cout << endl;
 }
}

void printaltlevelorder(node *root)
{
 int d = depth(root);
 for(int i=0; i<d; i++)
 {
  printaltbylevel(root, i, 0);
 }
}

int diameter(node* root)
{
 if(!root)
  return 0;
  
 int lheight = depth(root->l);
 int rheight = depth(root->r);
 
 return (1 + max3(lheight + rheight, diameter(root->l), diameter(root->r)));
}

class dia
{
 public:
  char height[30];
  char diam[60];
};

dia* cdepth(node* root)
{
 if(!root)
 {
  dia *t = new dia();
  strcpy(t->height, "");
  strcpy(t->diam, "");
  return t;
 }
 
 dia* l = cdepth(root->l);
 dia* r = cdepth(root->r);
 if(strlen(l->height) >= strlen(r->height))
 {
  strcat(l->height, &(root->c));
  return l;
 }
 else
 {
  strcat(r->height, &(root->c));
  return r;
 }
}

dia* maxPath(node* root)
{
 if(!root)
 { 
  dia *t = new dia();
  strcpy(t->height,"");
  strcpy(t->diam,"");
  return t;
 }
 
 dia* lht = cdepth(root->l);
 dia* rht = cdepth(root->r);
 
 dia* lmaxpath = maxPath(root->l);
 dia* rmaxpath = maxPath(root->r);
 
 if((strlen(lht->height) + strlen(rht->height)) >= strlen(lmaxpath->diam))
 {
  if((strlen(lht->height) + strlen(rht->height)) >= strlen(rmaxpath->diam))
  {
   strcat(lht->diam, lht->height);
   strcat(lht->diam, &(root->c));
   strcat(lht->diam, rht->height);
   return lht;
  }
  else
  {
   strcat(rmaxpath->diam, &(root->c));
   return rmaxpath;
  }
 }
 else
 {
  if(strlen(lmaxpath->diam) >= strlen(rmaxpath->diam))
  {
   strcat(lmaxpath->diam, &(root->c));
   return lmaxpath;
  }
  else
  {
   strcat(rmaxpath->diam, &(root->c));
   return rmaxpath;
  }
 }
 
}


int main() {
 // your code goes here
 char s[100];
 cin >> s;
 node* root=NULL;
 for(int i=0; i<strlen(s);i++)
  insert(&root, s[i]);
 cout << "tree input is:" << s << endl;
 cout << "inorder: ";
 printinorder(root);
 cout << "\ntree depth=" << depth(root) << endl;
 dia* t = cdepth(root);
 cout << "tree height path= " << t->height << endl;
 cout << "level order: \n";
 printlevelorder(root);
 cout << "alt level order: \n";
 printaltlevelorder(root);
 cout << "\ndiameter of tree: " << diameter(root) <<endl;
 t = maxPath(root);
 cout << "max Path = " << t->diam << endl;
 return 0;
}

tree input is:fjghdklsamcbe
inorder: a b c d e f g h j k l m s 
tree depth=6
tree height path= mslkjf
level order: 
      f
     d  j
    a  e  g  k
     c        h    l
    b                  s
                       m  
alt level order: 
fjdaegklhcbsm
diameter of tree: 10
max Path = bcadfmslkj

No comments: