SHOW:
|
|
- or go back to the newest paste.
| 1 | #include<istream> | |
| 2 | #include<fstream> | |
| 3 | #include <string> | |
| 4 | #include<iostream> | |
| 5 | #include <sstream> | |
| 6 | using namespace std; | |
| 7 | ||
| 8 | - | /*Горбунов Никита мас-301 |
| 8 | + | |
| 9 | - | 45 Написать набор функций, позволяющих работать с бинарным деревом |
| 9 | + | |
| 10 | - | вещественных чисел: |
| 10 | + | |
| 11 | {
| |
| 12 | - | а. Ввести дерево из файла. Формат ввода: в каждой строке файла - одно |
| 12 | + | |
| 13 | - | число, перед которым могут стоять символы табуляции. Пусть в какой-то |
| 13 | + | |
| 14 | - | строке стоит число n в позиции k. Тогда потомки числа n стоят в |
| 14 | + | |
| 15 | - | последующих строках в позиции (k+1). Пример. Следующий файл: |
| 15 | + | |
| 16 | void PrintTree(TreeElem *root) //Функция обхода | |
| 17 | - | 5 |
| 17 | + | |
| 18 | - | 6 |
| 18 | + | |
| 19 | - | 7 |
| 19 | + | |
| 20 | - | 8 |
| 20 | + | |
| 21 | - | -10 |
| 21 | + | |
| 22 | - | -5 |
| 22 | + | |
| 23 | - | 9 |
| 23 | + | |
| 24 | - | 11 |
| 24 | + | |
| 25 | - | 13 |
| 25 | + | |
| 26 | void AddElem(TreeElem *&root, int i) //Функция добавления звена в дерево | |
| 27 | - | кодирует дерево |
| 27 | + | |
| 28 | - | 5 |
| 28 | + | |
| 29 | - | / \ |
| 29 | + | |
| 30 | - | / \ |
| 30 | + | |
| 31 | - | 6 9 |
| 31 | + | |
| 32 | - | / \ / \ |
| 32 | + | |
| 33 | - | 7 8 11 13 |
| 33 | + | |
| 34 | - | / \ |
| 34 | + | |
| 35 | - | -10 -5 |
| 35 | + | |
| 36 | if (i>root->info) AddElem(root->right, i); | |
| 37 | - | б. Посчитать высоту и количество элементов в дереве |
| 37 | + | |
| 38 | - | в. Напечатать путь от корня дерева до заданного элемента |
| 38 | + | |
| 39 | - | г. Напечатать элементы дерева заданного уровня (уровень 0 -- корень |
| 39 | + | |
| 40 | - | дерева, уровень 1 -- потомки корня и т.п.) |
| 40 | + | |
| 41 | if (!root) | |
| 42 | return; | |
| 43 | DelTree(root->left); | |
| 44 | - | */ |
| 44 | + | |
| 45 | delete root; | |
| 46 | root=0; | |
| 47 | } | |
| 48 | ||
| 49 | ||
| 50 | TreeElem* Make(istream& in, int curspaces, string& str){
| |
| 51 | ||
| 52 | ||
| 53 | ||
| 54 | TreeElem* root = 0; | |
| 55 | root = new TreeElem; | |
| 56 | ||
| 57 | std::stringstream ss; | |
| 58 | ss << str; | |
| 59 | ss >> root->info; | |
| 60 | ||
| 61 | root->left = 0; | |
| 62 | root->right = 0; | |
| 63 | ||
| 64 | getline(in, str); | |
| 65 | int count = 0; | |
| 66 | char a = str[0]; | |
| 67 | for (int i = 0; i<str.length() && str[i] == ' '; ++i){
| |
| 68 | count++; | |
| 69 | } | |
| 70 | ||
| 71 | if (count > curspaces){
| |
| 72 | root->left = Make(in, count, str); | |
| 73 | int count2 = 0; | |
| 74 | for (int i = 0; i<str.length() && str[i] == ' '; ++i){
| |
| 75 | count2++; | |
| 76 | } | |
| 77 | if (count2 > curspaces){
| |
| 78 | root->right = Make(in, count, str); | |
| 79 | ||
| 80 | } | |
| 81 | return root; | |
| 82 | } | |
| 83 | else if (count == curspaces){
| |
| 84 | return root; | |
| 85 | } | |
| 86 | else {//count < spaces
| |
| 87 | return root; | |
| 88 | } | |
| 89 | ||
| 90 | ||
| 91 | } | |
| 92 | ||
| 93 | ||
| 94 | ||
| 95 | TreeElem* MakeTreeFromFile(string FileName){
| |
| 96 | ifstream name(FileName.c_str()); | |
| 97 | TreeElem* root = 0; | |
| 98 | ||
| 99 | ||
| 100 | string str = ""; | |
| 101 | getline(name, str); | |
| 102 | int count = 0; | |
| 103 | for (int i = 0; i<str.length() && str[i] == ' '; ++i){
| |
| 104 | count++; | |
| 105 | } | |
| 106 | root = Make(name, count, str); | |
| 107 | return root; | |
| 108 | } | |
| 109 | ||
| 110 | int NodeCount(TreeElem * root) | |
| 111 | {
| |
| 112 | if (root->left == 0 && root->right == 0) | |
| 113 | return 1; | |
| 114 | int l, r; | |
| 115 | if (root->left != 0) | |
| 116 | l = NodeCount(root->left); | |
| 117 | else | |
| 118 | l = 0; | |
| 119 | if (root->right != 0) | |
| 120 | r = NodeCount(root->right); | |
| 121 | else | |
| 122 | r = 0; | |
| 123 | ||
| 124 | return l+r+1; | |
| 125 | } | |
| 126 | ||
| 127 | int HeightOfTree(TreeElem * root) | |
| 128 | {
| |
| 129 | if(root == 0) | |
| 130 | return 0; | |
| 131 | int l, r; | |
| 132 | if (root->left != 0) {
| |
| 133 | l = HeightOfTree(root->left); | |
| 134 | }else | |
| 135 | l = -1; | |
| 136 | if (root->right != 0) {
| |
| 137 | r = HeightOfTree(root->right); | |
| 138 | }else | |
| 139 | r = -1; | |
| 140 | int max = l > r ? l : r; | |
| 141 | return max+1; | |
| 142 | ||
| 143 | } | |
| 144 | ||
| 145 | ||
| 146 | ||
| 147 | void PrintTreeLvl(TreeElem * root,int level) | |
| 148 | {
| |
| 149 | if(!level) | |
| 150 | {
| |
| 151 | cout << root->info; | |
| 152 | } | |
| 153 | PrintTreeLvl(root->left,level++ ); | |
| 154 | PrintTreeLvl(root->right,level++ ); | |
| 155 | ||
| 156 | ||
| 157 | } | |
| 158 | ||
| 159 | void PrintChain(TreeElem * root,int x){
| |
| 160 | if(!root) return 0; | |
| 161 | if (root->info==x) return 1; | |
| 162 | z; | |
| 163 | } | |
| 164 | ||
| 165 | ||
| 166 | int main(int argc, char** argv) {
| |
| 167 | ||
| 168 | /* | |
| 169 | char* FileName= "1.txt"; | |
| 170 | ||
| 171 | TreeElem *Tree=0; | |
| 172 | ||
| 173 | ||
| 174 | for (int i=0;i<5;i++) {AddElem(Tree,i);}
| |
| 175 | ||
| 176 | PrintTree(Tree); //Вызов функции обхода дерева; | |
| 177 | cin.get(); //Обычная задержка перед выходом; | |
| 178 | */ | |
| 179 | string FileName = "1.txt"; | |
| 180 | TreeElem *Tree = 0; | |
| 181 | Tree = MakeTreeFromFile(FileName); | |
| 182 | PrintTree(Tree); //Вызов функции обхода дерева; | |
| 183 | int count = NodeCount(Tree); | |
| 184 | - | void PrintTreeLvl(TreeElem *root,int level){
|
| 184 | + | |
| 185 | - | if(root==0) return; |
| 185 | + | |
| 186 | - | if (level == 0) |
| 186 | + | |
| 187 | DelTree(Tree); | |
| 188 | - | cout << root->info << " "; |
| 188 | + | |
| 189 | int level; | |
| 190 | - | PrintTreeLvl(root->left,level-1); |
| 190 | + | |
| 191 | - | PrintTreeLvl(root->right,level-1 ); |
| 191 | + | |
| 192 | //return 0; | |
| 193 | } |