gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Tree.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef TREE_H_
18 #define TREE_H_
19 
20 #include <iostream>
21 using namespace std;
22 
23 #include <math.h>
24 #include <stdlib.h>
25 
26 class Tree {
27 Tree *childL, *childR;
28 int val; // value of this node
29 uint maximum, minimum; // represents the maximumimum and minimumimum numbers below this node
30 
31 public:
32 
33 Tree (void){
34  childL=(Tree*)NULL;
35  childR=(Tree*)NULL;
36  val=0;
37  maximum=minimum=0;
38 }
39 
40 ~Tree (void){
41  if (childL)
42  delete childL;
43  if (childR)
44  delete childR;
45 }
46 
47 void addLayers(uint N, int *vals, uint minimumIn=1){
48  if (N>=0){
49  minimum=minimumIn;
50  maximum=minimum+(uint)pow(2.0,(double)N)-1;
51  if (minimum==maximum)
52  val=vals[minimum];
53  N=N-1;
54  if (N!=-1){
55  childL= new Tree;
56  if (!childL){
57  cerr<<"Couldn't add childL"<<endl; exit(-1);
58  }
59  childL->addLayers(N,vals,minimum);
60  childR= new Tree;
61  if (!childR){
62  cerr<<"Couldn't add childR"<<endl; exit(-1);
63  }
64  childR->addLayers(N,vals,minimum+(maximum-minimum+1)/2);
65  }
66  if (minimum!=maximum)
67  val=childL->val*childR->val;
68  cout<<"layer="<<N<<", minimum="<<minimum<<", maximum="<<maximum<<", val="<<val<<endl;
69  }
70  return;
71 }
72 
73 int multiply(uint whichNode){
74  if (childL==NULL & childR==NULL) // the case where this is 'whichNode'
75  if ((whichNode==maximum) & (whichNode==minimum)){
76  //cout<<"1returning 1"<<endl;
77  return 1;
78  } else {
79  //cout<<"1returning minimum="<<minimum<<", maximum="<<maximum<<", val="<<val<<endl;
80  return val;
81  }
82 
83  int value=1;
84  if (childL) // If the left child exists ... compute
85  if ((whichNode<childL->minimum) | (whichNode>childL->maximum)){
86  //cout<<"2lreturning , value="<<childL->val<<endl;
87  value*=childL->val;
88  }else{
89  //cout<<"2lreturning minimum="<<childL->minimum<<", maximum="<<childL->maximum<<", value*=->mult"<<endl;
90  value*=childL->multiply(whichNode);
91  }
92  else // else the right child must exist - so set to 1
93  value*=1;
94  if (childR) // If the right child exists ... compute
95  if ((whichNode<childR->minimum) | (whichNode>childR->maximum)){
96  //cout<<"2rreturning , value="<<childR->val<<endl;
97  value*=childR->val;
98  } else {
99  //cout<<"2rreturning minimum="<<childR->minimum<<", maximum="<<childR->maximum<<", value*=->mult"<<endl;
100  value*=childR->multiply(whichNode);
101  }
102  else // else the left child must exist so multiply out its valueue
103  value*=1;
104  return value;
105 }
106 
107 };
108 
109 #endif //TREE_H_
~Tree(void)
Definition: Tree.H:40
int N
STL namespace.
unsigned int uint
Definition: Box.H:28
int val
Definition: Tree.H:28
Definition: Tree.H:26
Tree * childR
Definition: Tree.H:27
int multiply(uint whichNode)
Definition: Tree.H:73
Tree(void)
Definition: Tree.H:33
uint maximum
Definition: Tree.H:29
void addLayers(uint N, int *vals, uint minimumIn=1)
Definition: Tree.H:47
uint minimum
Definition: Tree.H:29
gtkIOStream: /tmp/gtkiostream/include/mffm/possible.future.additions/examples/intMult/Tree.H Source File
GTK+ IOStream  Beta