MESSAGE
DATE | 2015-02-24 |
FROM | Ruben Safir
|
SUBJECT | Re: [LIU Comp Sci] Binary tree Excersize
|
From owner-learn-outgoing-at-mrbrklyn.com Tue Feb 24 14:12:39 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 7E2B416115D; Tue, 24 Feb 2015 14:12:39 -0500 (EST) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id 6BB5D161167; Tue, 24 Feb 2015 14:12:39 -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 3B7B116115D for ; Tue, 24 Feb 2015 14:12:37 -0500 (EST) Received: from panix2.panix.com (panix2.panix.com [166.84.1.2]) by mailbackend.panix.com (Postfix) with ESMTP id 9B8741071B for ; Tue, 24 Feb 2015 14:12:36 -0500 (EST) Received: by panix2.panix.com (Postfix, from userid 20529) id 8B00533C79; Tue, 24 Feb 2015 14:12:36 -0500 (EST) Date: Tue, 24 Feb 2015 14:12:36 -0500 From: Ruben Safir To: learn-at-mrbrklyn.com Subject: Re: [LIU Comp Sci] Binary tree Excersize Message-ID: <20150224191236.GA24246-at-panix.com> References: <54EB4BE9.6020505-at-panix.com> <54EBF7E1.2070605-at-panix.com> <54EBFCC9.8070605-at-my.liu.edu> 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:32:57PM -0500, Maneesh Kongara wrote: > Is this the answer for the assignment? > On Feb 23, 2015 11:23 PM, "Ruben" wrote:
One is a sample
the node counter is in here.
I'm trying to build an algorithm to put entries into the binary in order... 1,2,3,4,5,6 etc.
It is much harder than it would apear to be because there is no "bottom" to recoil recursions from. Or it only goes left left left, and never right.
Ideally I want to test the allorithms but in order to do so you need an ordered binary tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
or something like that > > > 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; > > } > > > >
|
|