/ Published in: C++
Expand |
Embed | Plain Text
#include <iostream> #define header 2 #if header == 1 #include "BTree_pointer.h" #elif header == 2 #include "BTree_array.h" #else #include "ChildSiblingTree.h" #endif using namespace std; #if header == 1 || header == 2 void binaryTree() { int rt, nd1, nd2, nd3, nd4, nd5, nd6, nd7; cout << "Inicijalizacija stabla..." << endl; cout<<"-------------------------------------------------" << endl; cout << "Vrijednost korjena: "; cin >> rt; btree myTree = init(rt); cout << endl << "\nKreiranje cvorova..." << endl; cout << "Lijevo dijete korjena: "; cin >> nd1; createLeft(nd1, root(myTree), myTree); cout << "Desno dijete korjena: "; cin >> nd2; createRight(nd2, root(myTree), myTree); cout << "Lijevo dijete lijevog dijeteta korjena: "; cin >> nd3; createLeft(nd3, leftChild(root(myTree), myTree), myTree); cout << "Desno dijete lijevog dijeteta korjena: "; cin >> nd4; createRight(nd4, leftChild(root(myTree), myTree), myTree); cout << "Lijevo dijete desnog dijeteta korjena: "; cin >> nd5; createLeft(nd5, rightChild(root(myTree), myTree), myTree); cout << "Desno dijete desnog dijeteta korjena: "; cin >> nd6; createRight(nd6, rightChild(root(myTree), myTree), myTree); cout << endl << "-------------------------------------------------" << endl; cout << "Ispis" << endl; cout << "korijen: " << label(root(myTree), myTree) << endl; cout << "1. razina: " << label(leftChild(root(myTree), myTree), myTree)<< ", "; cout << label(rightChild(root(myTree), myTree), myTree)<< endl; cout << "2. razina: " << label(leftChild(leftChild(root(myTree), myTree), myTree), myTree); cout << ", " << label(rightChild(leftChild(root(myTree), myTree), myTree), myTree); cout << ", " << label(leftChild(rightChild(root(myTree), myTree), myTree), myTree); cout << ", " << label(rightChild(rightChild(root(myTree), myTree), myTree), myTree)<< endl; cout << "-----------------------------------------------" << endl; node tmp = leftChild(rightChild(root(myTree), myTree), myTree); cout << "Roditelj cvora 6 je: " << label(parrent(tmp, myTree), myTree) << endl; cout << "Brisanje cvora 3 i potomaka!" << endl; deleteNode(rightChild(root(myTree), myTree), myTree); cout << "Unesite novu vrijednost: "; cin >> nd7; changeLabel(nd7, leftChild(leftChild(root(myTree), myTree), myTree), myTree); cout << endl << "-------------------------------------------------" << endl; cout << "Novo stanje" << endl; cout << "Korijen: " << label(root(myTree), myTree) << endl; cout << "1. razina: " << label(leftChild(root(myTree), myTree), myTree)<< endl; cout << "2. razina: " << label(leftChild(leftChild(root(myTree), myTree), myTree), myTree); cout << ", " << label(rightChild(leftChild(root(myTree), myTree), myTree), myTree) << endl; cout << "-----------------------------------------------" << endl; } #else void trie() { tree *myTree = new tree; init(1, myTree); cout << "cvor 2:" << endl; create(2, 1, myTree); cout << "cvor 3:" << endl; create(3, 2, myTree); cout << "cvor 4:" << endl; create(4, 2, myTree); string newLabel; cout << "Nova oznaka za cvor 2:"; cin >> newLabel; changeLabel(newLabel, 2, myTree); label(3, myTree); label(2, myTree); label(3, myTree); label(4, myTree); label(5, myTree); cout << "prvo djete od 2: " << firstChild(2, myTree) << endl; cout << "roditelj od 3: " << parent(3, myTree) << endl; cout << "brat od 3: " << nextSibling(3, myTree) << endl; deleteNode(2, myTree); delete myTree; } #endif int main () { #if header == 1 || header == 2 binaryTree(); #else trie(); #endif system("pause"); return 0; }
You need to login to post a comment.
