1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int count=1;
int runs=0;
struct node
{
float data;
int line_number;
struct node* left;
struct node* right;
};
struct node* NewNode(float data, int line)
{
struct node* node = new(struct node);// "new" is like "malloc"
node->line_number=line;
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
struct node* insert(struct node* point, float data, int line)
{
// If the tree is empty, return a new, single node
if (point == NULL) {
return(NewNode(data, line));
}
else {
// Otherwise, work your way down the tree
if (data <= point->data) point->left = insert(point->left, data, line);
else point->right = insert(point->right, data, line);
return(point); // return the node pointer
}
}
struct node* inserting()
{
float x;
int line_number = 1;
ifstream inFile;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
std::cin.get();
exit(1); // terminate with error
}
inFile >> x;
inFile.close();
node* point = insert(NULL,x,line_number);
line_number++;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
std::cin.get();
exit(1); // terminate with error
}
inFile >> x;
insert(NULL,x,line_number);
while (!inFile.eof()) {
inFile >> x;
insert(point,x, line_number);
count++;
line_number++;
}
//cout << count;
//std::cin.get();
return(point);
}
float minValue(struct node* node)
{
struct node* current = node;
// loop down to find the leftmost leaf
while (current->left != NULL) {
current = current->left;
}
cout<<current->data<<" is the minimum at line "<<current->line_number;
std::cin.get();
return(current->data);
}
float maxValue(struct node* node)
{
struct node* current = node;
// loop down to find the leftmost leaf
while (current->right != NULL) {
current = current->right;
}
cout<<current->data<<" is the maximum at line "<<current->line_number;
std::cin.get();
return(current->data);
}
void printTree(struct node* point)
{
if (point == NULL) return;
printTree(point->left);
cout<<point->data<<"\n";
//std::cin.get();
printTree(point->right);
}
void find_median(struct node* point)
{
if (point == NULL) return;
find_median(point->left);
runs++;
//cout<<point->data<<"\n";
//cout<<runs;
//std::cin.get();
if (runs == count/2)
{
cout<<point->data<<"is the median at line "<<point->line_number<<"\n";
}
find_median(point->right);
}
int main()
{
int numberValues;
minValue(inserting());
numberValues = count;
maxValue(inserting());
printTree(inserting());
find_median(inserting());
cout<<numberValues<<" items in table";
std::cin.get();
return 0;
}
|