MESSAGE
DATE | 2015-02-24 |
FROM | Ruben Safir
|
SUBJECT | Re: Re: [LIU Comp Sci] Binary tree Excersize
|
From owner-learn-outgoing-at-mrbrklyn.com Tue Feb 24 14:13:55 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 0C50C16115D; Tue, 24 Feb 2015 14:13:55 -0500 (EST) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id E1219161167; Tue, 24 Feb 2015 14:13:54 -0500 (EST) Delivered-To: learn-at-mrbrklyn.com Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mrbrklyn.com (Postfix) with ESMTP id D52A016115D for ; Tue, 24 Feb 2015 14:13:53 -0500 (EST) Received: from panix2.panix.com (panix2.panix.com [166.84.1.2]) by mailbackend.panix.com (Postfix) with ESMTP id C8D7F10863 for ; Tue, 24 Feb 2015 14:13:53 -0500 (EST) Received: by panix2.panix.com (Postfix, from userid 20529) id BBA6633C79; Tue, 24 Feb 2015 14:13:53 -0500 (EST) Date: Tue, 24 Feb 2015 14:13:53 -0500 From: Ruben Safir To: learn-at-mrbrklyn.com Subject: Re: Re: [LIU Comp Sci] Binary tree Excersize Message-ID: <20150224191353.GA20987-at-panix.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
On Tue, Feb 24, 2015 at 12:31:22PM -0500, keisha.sylvester wrote: > Rubin that's long. >
It is more than one problem and it includes a lot of example programs from the nots in one library file.
Ruben
> > > > Sent from Samsung tabletRuben 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; > } >
|
|