MESSAGE
DATE | 2015-02-24 |
FROM | Maneesh Kongara
|
SUBJECT | Re: [LIU Comp Sci] Binary tree Excersize
|
From owner-learn-outgoing-at-mrbrklyn.com Tue Feb 24 12:32:59 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 9B5D1161162; Tue, 24 Feb 2015 12:32:59 -0500 (EST) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id 896EE161168; Tue, 24 Feb 2015 12:32:59 -0500 (EST) Delivered-To: learn-at-mrbrklyn.com Received: from mail-qa0-f54.google.com (mail-qa0-f54.google.com [209.85.216.54]) by mrbrklyn.com (Postfix) with ESMTP id 04CB4161162 for ; Tue, 24 Feb 2015 12:32:58 -0500 (EST) Received: by mail-qa0-f54.google.com with SMTP id x12so28173631qac.13 for ; Tue, 24 Feb 2015 09:32:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=nodR+17OcYwbf5pts1CEHsmGhhVm70F6YeJJ8gULfx4=; b=UANY03sE02So6GmG4nKFhPhJtcY0n/ddPVHV7yUKPODSdmSpekXGe2EFYH13/EVa4u VFNkTyZBxiqjW6utlREvI4pTsEtNqFqqu/dQrcOnxNfaVtrLeNVvIwzQL2Ta92WnhSYE psolFf+X8pkYELXjtZW8SMFOXXHTBMqwJ8mThtPBtswm/qDKd09J2RVUOn/NxAHKMKeH 8vwoSlA3DMrcg+jZNsLYvBb1dDCgUUTbFV64cTpFBnFXOCySOME5m2eeUbU5IOPFG/B6 ouIJdJjaSszwSjPVJQpRCtsGa7hOMrXJ0RYoIAzfw2Qi2cbERoFi28qxXTioX/kW11C5 IjwA== MIME-Version: 1.0 X-Received: by 10.140.231.204 with SMTP id b195mr11606265qhc.98.1424799178152; Tue, 24 Feb 2015 09:32:58 -0800 (PST) Received: by 10.229.19.4 with HTTP; Tue, 24 Feb 2015 09:32:57 -0800 (PST) Received: by 10.229.19.4 with HTTP; Tue, 24 Feb 2015 09:32:57 -0800 (PST) In-Reply-To: <54EBFCC9.8070605-at-my.liu.edu> References: <54EB4BE9.6020505-at-panix.com> <54EBF7E1.2070605-at-panix.com> <54EBFCC9.8070605-at-my.liu.edu> Date: Tue, 24 Feb 2015 12:32:57 -0500 Message-ID: Subject: Re: [LIU Comp Sci] Binary tree Excersize From: Maneesh Kongara To: learn-at-mrbrklyn.com Content-Type: multipart/alternative; boundary=001a11c0b0f0e3e016050fd8e8ad Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
--001a11c0b0f0e3e016050fd8e8ad Content-Type: text/plain; charset=UTF-8
Is this the answer for the assignment? On Feb 23, 2015 11:23 PM, "Ruben" wrote:
> On 02/23/2015 11:02 PM, Ruben Safir wrote: > > On 02/23/2015 03:04 PM, Keisha Sylvester wrote: > >> sight* > >> > > OK - spelling correction noted. This is now the THIRD answer I sent to > you. > > > #include "binary.h" > #include > #include > /* Binary Tree Function File for Binary exercises > * from LIU Comp Sci Allorgithms Class > * J Rodriguez > * 2015 > * Ruben Safir > */ > > using namespace std; > char c; > BinTree NewBinTree() > { > srand(time(NULL)); > return 0; > }; //this just seeds a new radnom number generator fresh with each new > node, avoiding a repeating psuedorandom number > int node_counter = 0; > int level_counter; > void InsertOrdered(BinTree& top, int val){ > cout << endl << "CALLED: " << "Recieved =>" << val << endl ; > > static int index = -1; > int odd = index %2; > > if(top == 0){ > cout << "Opening New Node" << endl; > top = new bnode; > top->data = val; > top->left = top->right = 0; > index++; > return; > } > if(odd == 0){ > cout << "Going Left " << endl; > InsertOrdered(top->left, val); > cout << "Left Exited" << endl; > }else{ > cout << "Going Right" << endl; > InsertOrdered(top->right, val); > cout << "Right Exited" << endl; > } > return; > } > > void InsertRandom(BinTree& top, int val) > { > if(top == 0){ > cout << "\tInserting into the New Node: Node Identifier ==> "<< > node_counter++ << endl; > cout << "\tLevel entered is ==> " << level_counter; > top = new bnode; > cout << "\tcreated a new node" << endl; > top->data = val; > cout << "\tNode Inserted with value==> " << val << endl; > top->left = top->right = 0; > cout << "\tDaughter nodes defined as null" << endl; > return; > } > > int r = rand() % 2; > cout << "node occupied ==>find an Empty Node at random" << endl; > cout << "\tGoing Down ==>" << ++level_counter << endl; > cout << "Trying "; > if (r == 0){ > cout << "Inserting on Left " << endl; > InsertRandom(top->left, val); > cout << "Left Path Done -- Going Up" << endl; > } > else{ > cout << "Inserting Right " << endl; > InsertRandom(top->right, val); > cout << "Right Path Done -- Going Up" << endl; > } > level_counter--; > cout << "Next Level -->" << level_counter << endl; > return; > } > > void PrintNested(BinTree& top, char tag) > { > char save_tag; > > if (top == 0){ > cout << "[ ]"; > } > else{ > cout << "(" << tag << " " << top->data << ", "; > save_tag = tag; > tag++; > PrintNested(top->left, tag); > cout << ", "; > PrintNested(top->right, tag); > cout << ", " << save_tag << ")"; > } > return; > } > > int Height(const BinTree& top) > { > // cout << endl << "START NEW HEIGHT FUNCTION " << endl; > static int cycles = 0; > cycles++; > // int height = 0; > // cout << "CYLCLES " << cycles << ":Height " << height << endl; > int hl, hr; > if (top == 0){ > cout << "end of path" << endl; > cycles--; > return -1; > } > int step = cycles; > //cout << "Cycle" << cycles << endl; > cout << "Left " << step << endl; > hl = Height(top->left); > cout << "return Left: ==> " << step << ": "; > cout << "hl: " << hl << endl; > cout << "RIGHT " << step << endl; > hr = Height(top->right); > cout << "return RIGHT: ==> " << step << ":"; > cout << "hr:" << hr << endl; > //height = 1 + ((hl <= hr)?hr:hl); > return 1 + ((hl <= hr)?hr:hl); > //cout << "height " << height << " hl " << hl << " hr " << hr << endl; > > // return height; > } > > int node_count(const BinTree& node) > { > int static count = 0; > if(node == 0) > { > return count; > } > node_count(node->left); > node_count(node->right); > count++; > return count; > } > > void preorder(const BinTree& root) > { > if(root == 0) > return; > > cout << "Node ==>" << root->data << " "; > preorder(root->left); > preorder(root->right); > return; > } > >
--001a11c0b0f0e3e016050fd8e8ad Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Is this the answer for the assignment?
On Feb 23, 2015 11:23 PM, "Ruben" <= ruben.safir-at-my.liu.edu> wr= ote: gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 02/23/2015 1= 1:02 PM, Ruben Safir wrote:
> On 02/23/2015 03:04 PM, Keisha Sylvester wrote:
>> sight*
>>
> OK - spelling correction noted.=C2=A0 This is now the THIRD answer I s= ent to you.
>
#include "binary.h"
#include <iostream>
#include <cstdlib>
/* Binary Tree Function File for Binary exercises
=C2=A0* from LIU Comp Sci Allorgithms Class
=C2=A0* J Rodriguez
=C2=A0* 2015
=C2=A0* Ruben Safir
=C2=A0*/
using namespace std;
char c;
BinTree NewBinTree()
{
=C2=A0 =C2=A0 srand(time(NULL));
=C2=A0 =C2=A0 return 0;
};=C2=A0 //this just seeds a new radnom number generator fresh with each ne= w
node, avoiding a repeating psuedorandom number
int node_counter =3D 0;
int level_counter;
void InsertOrdered(BinTree& top, int val){
=C2=A0 =C2=A0 cout << endl << "CALLED:=C2=A0 " <&l= t;=C2=A0 "Recieved =3D>" << val << endl ;
=C2=A0 =C2=A0 static int index =3D -1;
=C2=A0 =C2=A0 int odd =3D index %2;
=C2=A0 =C2=A0 if(top =3D=3D 0){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Opening New Node" <= < endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 top =3D new bnode;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 top->data =3D val;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 top->left =3D top->right =3D 0;
=C2=A0 =C2=A0 =C2=A0 =C2=A0index++;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 if(odd =3D=3D 0){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Going Left " << = endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 InsertOrdered(top->left, val);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Left Exited" << = endl;
=C2=A0 =C2=A0 }else{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Going Right" << = endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 InsertOrdered(top->right, val);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Right Exited" <<= endl;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 return;
}
void InsertRandom(BinTree& top, int val)
{
=C2=A0 =C2=A0 if(top =3D=3D 0){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "\tInserting into the New No= de: Node Identifier =3D=3D> "<<
node_counter++ << endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "\tLevel entered is =3D=3D&g= t; " << level_counter;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 top =3D new bnode;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "\tcreated a new node" = << endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 top->data =3D val;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "\tNode Inserted with value= =3D=3D> " << val <<=C2=A0 endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 top->left =3D top->right =3D 0;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "\tDaughter nodes defined as= null" << endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 int r =3D rand() % 2;
=C2=A0 =C2=A0 cout << "node occupied =3D=3D>find an Empty Nod= e at random" << endl;
=C2=A0 =C2=A0 cout << "\tGoing Down =3D=3D>" << ++= level_counter << endl;
=C2=A0 =C2=A0 cout << "Trying ";
=C2=A0 =C2=A0 if (r =3D=3D 0){
=C2=A0 =C2=A0 =C2=A0 cout << "Inserting on Left " << = endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 InsertRandom(top->left, val);
=C2=A0 =C2=A0 =C2=A0 cout << "Left Path Done -- Going Up" &= lt;< endl;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 else{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Inserting Right " <= < endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 InsertRandom(top->right, val);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "Right Path Done -- Going Up= " << endl;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 level_counter--;
=C2=A0 =C2=A0 cout << "Next Level -->" << level_co= unter << endl;
=C2=A0 =C2=A0 return;
}
void PrintNested(BinTree& top, char tag)
{
=C2=A0 =C2=A0 char save_tag;
=C2=A0 =C2=A0 if (top =3D=3D 0){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "[ ]";
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 else{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "(" << tag <&l= t; " " << top->data << ", ";
=C2=A0 =C2=A0 =C2=A0 =C2=A0 save_tag =3D tag;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tag++;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 PrintNested(top->left, tag);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << ", ";
=C2=A0 =C2=A0 =C2=A0 =C2=A0 PrintNested(top->right, tag);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << ", " << save_tag = << ")";
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 return;
}
int Height(const BinTree& top)
{
//=C2=A0 =C2=A0 cout << endl << "START NEW HEIGHT FUNCTION= " << endl;
=C2=A0 =C2=A0 static int cycles =3D 0;
=C2=A0 =C2=A0 cycles++;
//=C2=A0 =C2=A0 int height =3D 0;
//=C2=A0 =C2=A0 cout << "CYLCLES " << cycles <<= ":Height " << height << endl;
=C2=A0 =C2=A0 int hl, hr;
=C2=A0 =C2=A0 if (top =3D=3D 0){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout << "end of path" << = endl;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cycles--;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return -1;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 int step =3D cycles;
=C2=A0 =C2=A0 //cout << "Cycle" << cycles << en= dl;
=C2=A0 =C2=A0 cout << "Left " << step << endl;<= br> =C2=A0 =C2=A0 hl =3D Height(top->left);
=C2=A0 =C2=A0 cout << "return Left: =3D=3D> " << s= tep << ": ";
=C2=A0 =C2=A0 cout << "hl: " << hl << endl;
=C2=A0 =C2=A0 cout << "RIGHT " << step << endl;=
=C2=A0 =C2=A0 hr =3D Height(top->right);
=C2=A0 =C2=A0 cout << "return RIGHT: =3D=3D> " << = step << ":";
=C2=A0 =C2=A0 cout << "hr:" << hr << endl;
=C2=A0 =C2=A0 //height =3D 1 + ((hl <=3D hr)?hr:hl);
=C2=A0 =C2=A0 return 1 + ((hl <=3D hr)?hr:hl);
=C2=A0 =C2=A0 //cout << "height " << height << = " hl " << hl << " hr " << hr <<= endl;
//=C2=A0 =C2=A0 return height;
}
int node_count(const BinTree& node)
{
=C2=A0 =C2=A0 int static count =3D 0;
=C2=A0 =C2=A0 if(node =3D=3D 0)
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return count;
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 node_count(node->left);
=C2=A0 =C2=A0 node_count(node->right);
=C2=A0 =C2=A0 count++;
=C2=A0 =C2=A0 return count;
}
void preorder(const BinTree& root)
{
=C2=A0 =C2=A0 if(root =3D=3D 0)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
=C2=A0 =C2=A0 cout << "Node =3D=3D>" << root->d= ata=C2=A0 << " ";
=C2=A0 =C2=A0 preorder(root->left);
=C2=A0 =C2=A0 preorder(root->right);
=C2=A0 =C2=A0 return;
}
--001a11c0b0f0e3e016050fd8e8ad--
|
|