Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<fstream>
- using namespace std;
- ////////////////////////
- struct treeNode
- {
- treeNode* left;
- treeNode* right;
- int data;
- };
- ///////////////////////
- struct stackNode
- {
- stackNode* next;
- treeNode* node;
- };
- //////////////////////
- void push_tree(treeNode* &tree_top, int x)
- {
- treeNode *p, *m;
- bool b;
- if (tree_top == NULL)
- {
- m = new treeNode;
- m->data = x;
- m->left = NULL;
- m->right = NULL;
- tree_top = m;
- }
- else
- {
- m = tree_top;
- b = true;
- while (b)
- {
- if (x >= m->data)
- {
- if (m->right)
- m = m->right;
- else
- {
- p = new treeNode;
- p->data = x;
- p->left = NULL;
- p->right = NULL;
- m->right = p;
- b = false;
- }
- }
- else
- {
- if (m->left) m = m->left;
- else
- {
- p = new treeNode;
- p->data = x;
- p->left = NULL;
- p->right = NULL;
- m->left = p;
- b = false;
- }
- }
- }
- }
- }
- /////////////////////////////////////////////////
- void push_stack(stackNode* &stack_top, treeNode* t)
- {
- stackNode* p;
- p = new stackNode;
- p->node = t;
- p->next = stack_top;
- stack_top = p;
- }
- //////////////////////////////////////////////////////////////
- void create_tree(ifstream &f, treeNode* &tree_top)
- {
- tree_top = NULL;
- int x;
- while (f >> x)
- {
- push_tree(tree_top, x);
- }
- }
- /////////////////////////
- void pop_stack(stackNode* &stack_top, treeNode* &t)
- //get node of tree from stack
- {
- stackNode *p;
- if (stack_top)
- {
- t = stack_top->node;
- p = stack_top->next;
- delete stack_top;
- stack_top = p;
- }
- }
- /////////////////////////////////////////////////////////////
- void print_node(int x)
- {
- cout << " " << x;
- }
- ////////////////////////////
- void inorder_tree(treeNode* tree_top)
- //simmetric obhod tree
- {
- cout << "***************************" << endl;
- stackNode* stack_top;
- treeNode* tree_p;
- tree_p = tree_top;
- stack_top = NULL;
- do
- {
- while (tree_p)
- {
- while (tree_p->left)
- {
- push_stack(stack_top, tree_p);
- tree_p = tree_p->left;
- }
- print_node(tree_p->data);
- tree_p = tree_p->right;
- }
- if (stack_top)
- {
- pop_stack(stack_top, tree_p);
- print_node(tree_p->data);
- tree_p = tree_p->right;
- }
- } while (stack_top || tree_p);
- cout << endl;
- }
- void showTree(treeNode* tree_top)
- {
- if (tree_top != NULL)
- {
- showTree(tree_top->left);
- cout << tree_top->data << ' ';
- showTree(tree_top->right);
- }
- }
- int task8a(treeNode * tree_top, int x)
- {
- int count=0;
- if (tree_top == NULL)
- return 0;
- return (tree_top->data == x) + task8a(tree_top->left, x) + task8a(tree_top->right, x);
- }
- double task8v(treeNode *tree_top,double &x)
- {
- if (tree_top != NULL)
- {
- x += tree_top->data;
- task8v(tree_top->left, x);
- task8v(tree_top->right, x);
- }
- return x;
- }
- int task8g(treeNode * tree_top)
- {
- if (tree_top->right != NULL)
- {
- return task8g(tree_top->right);
- }
- else
- return tree_top->data;
- }
- int task8e(treeNode * tree_top)
- {
- int max;
- if (tree_top == NULL)
- return 0;
- int x, y;
- if (tree_top->left != NULL)
- {
- x = task8e(tree_top->left);
- }
- else
- x = -1;
- if (tree_top->right != NULL)
- {
- y = task8e(tree_top->right);
- }
- else
- y = -1;
- if (x > y)
- max=x;
- else max=y;
- return max + 1;
- }
- ///////////////////////////////////////
- void main()
- {
- setlocale(LC_ALL, "Russian");
- ifstream f("Text.txt");
- int x;
- double count = 0;
- bool found = false;
- treeNode* tree_top, *p = NULL;
- create_tree(f, tree_top);
- showTree(tree_top);
- f.close();
- cout << endl;
- cin >> x;
- /*
- if (tree_top != NULL)
- {
- if (task8a(tree_top, x) != 0)
- cout << "yes" << endl;
- else
- cout << "no" << endl;
- }
- else
- cout << "list is empty" << endl;
- */
- /*
- if (tree_top != NULL)
- {
- cout << task8v(tree_top,count);
- }
- else
- cout << "list is empty" << endl;
- */
- if (tree_top != NULL)
- {
- cout << task8a(tree_top,x);
- }
- else
- cout << "list is empty" << endl;
- /*
- if (tree_top != NULL)
- {
- cout<<task8g(tree_top);
- }
- else
- cout << "list is empty" << endl;
- */
- /*
- if (tree_top != NULL)
- {
- showTree(tree_top);
- }
- else
- cout << "list is empty" << endl;
- */
- /*
- if (tree_top != NULL)
- {
- cout << task8e(tree_top);
- }
- else
- cout << "list is empty" << endl;
- */
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment