MESSAGE
DATE | 2015-02-23 |
FROM | Ruben
|
SUBJECT | Re: [LIU Comp Sci] Binary tree Excersize
|
From owner-learn-outgoing-at-mrbrklyn.com Mon Feb 23 23:23:40 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 10C61161166; Mon, 23 Feb 2015 23:23:40 -0500 (EST) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id 0143C161168; Mon, 23 Feb 2015 23:23:39 -0500 (EST) Delivered-To: learn-at-mrbrklyn.com Received: from mail-qa0-f43.google.com (mail-qa0-f43.google.com [209.85.216.43]) by mrbrklyn.com (Postfix) with ESMTP id 558BE161166 for ; Mon, 23 Feb 2015 23:23:38 -0500 (EST) Received: by mail-qa0-f43.google.com with SMTP id bm13so25571385qab.2 for ; Mon, 23 Feb 2015 20:23:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=eQ+k4kCDl9p2wQePd4cj2sBylkb5wkgilYtIiYzqimw=; b=Iwjk6pwAFGYozBCfphKZCBr6LcfEjE1yluk3G7nvDnYo+6oSuRdGIWZ93/YbIGW173 WvMn4fzDs30/jPaCdkWxVdFWKO8EIgCv/LcHfvh9t4hd3ot1+AQzcJ0c3iytS8B78zKk fYVs6J/sSAqJ43nY9loODu9RwZy8d36fZ/SMBn1HMrXjJlzypVu0QgCpF52A0IuP46jO 9va82EY2qB5X7GVyK2t9s5nBbsABXR4yaZJZYh9tB9mA0zIHanmdJIpTQV1Fftpy3ljg ZrR4DrzVC7M2uXh7z9LtMrI58pZ6lXfIoZDBaVEAkeHu6O+aS6SHSYcJ62288mdtSTSp DBXw== X-Gm-Message-State: ALoCoQk+rDovsZuOhqkaDg+9IMpdYMefo0eTF/FbIZR9+9Kfwt8aelIqGZ+6Jhb6vyDyhrv5NWKb X-Received: by 10.229.25.200 with SMTP id a8mr32647449qcc.22.1424751818559; Mon, 23 Feb 2015 20:23:38 -0800 (PST) Received: from [10.0.0.19] ([96.57.23.82]) by mx.google.com with ESMTPSA id b195sm12720837qhc.45.2015.02.23.20.23.38 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Feb 2015 20:23:38 -0800 (PST) Message-ID: <54EBFCC9.8070605-at-my.liu.edu> Date: Mon, 23 Feb 2015 23:23:37 -0500 From: Ruben User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: learn-at-mrbrklyn.com Subject: Re: [LIU Comp Sci] Binary tree Excersize References: <54EB4BE9.6020505-at-panix.com> <54EBF7E1.2070605-at-panix.com> In-Reply-To: <54EBF7E1.2070605-at-panix.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
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; }
|
|