Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // bst.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct wezel
- {
- int klucz;
- wezel* lewe;
- wezel* prawe;
- };
- void insert_BST(int x, wezel* &w)
- {
- if (w == NULL)
- {
- w = new wezel; w->klucz = x;
- w->lewe = NULL; w->prawe = NULL;
- }
- else if (x < w->klucz) insert_BST(x, w->lewe);
- else insert_BST(x, w->prawe);
- }
- void built_BST(int n, wezel* &w)
- {
- w = NULL;
- for (int i = 1; i <= n; i++)
- {
- cout << "wypisz wartosci: " << endl;
- int x; cin >> x;
- insert_BST(x, w);
- }
- }
- int wysokosc_drzewa(wezel *w)
- {
- int h;
- if (w != NULL)
- {
- int x = wysokosc_drzewa(w->lewe);
- int y = wysokosc_drzewa(w->prawe);
- if (x > y) h = x + 1;
- else h = y + 1;
- }
- else h = 0;
- return h;
- }
- void wyswietl_drzewo_inorder(wezel *w, int h)
- {
- if (w != NULL)
- {
- wyswietl_drzewo_inorder(w->lewe, h - 1);
- for (int i = 1; i <= h; i++) cout << " ";
- cout << w->klucz << "\n";
- wyswietl_drzewo_inorder(w->prawe, h - 1);
- }
- }
- void wyswietl_drzewo_preorder(wezel *w, int h)
- {
- if (w != NULL)
- {
- wyswietl_drzewo_preorder(w->lewe, h - 1);
- //for (int i = 1; i <= h; i++) cout << " ";
- cout << w->klucz << " ";
- wyswietl_drzewo_preorder(w->prawe, h - 1);
- }
- }
- void wyswietl_drzewo_postorder(wezel *w, int h)
- {
- if (w != NULL)
- {
- wyswietl_drzewo_postorder(w->lewe, h - 1);
- wyswietl_drzewo_postorder(w->prawe, h - 1);
- //for (int i = 1; i <= h; i++) cout << " ";
- cout << w->klucz << " ";
- }
- }
- int main()
- {
- wezel* drzewo = NULL;
- int n;
- cout << "podaj n:"; cin >> n;
- built_BST(n, drzewo);
- int h = wysokosc_drzewa(drzewo);
- wyswietl_drzewo_inorder(drzewo, h);
- cout << endl << endl;
- wyswietl_drzewo_preorder(drzewo, h);
- cout << endl << endl;
- wyswietl_drzewo_postorder(drzewo, h);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement