/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> using namespace std; struct element { int label; element *left, *right; }; typedef element *node; typedef element bt; node ParentB (node n, bt* T) { if (n==T) { cout << "Greska!" << endl; return NULL; } node rod = NULL; if (T->left) { if (T->left==n) return T; else rod = ParentB (n, T->left); } if (T->right) { if (T->right==n) return T; else rod = ParentB (n, T->right); } return rod; } node LeftChildB (node n, bt* T) { if (n->left==NULL) return NULL; else return n->left; } node RightChildB (node n, bt* T) { if (n->right==NULL) return NULL; else return n->right; } int LabelB (node n, bt* T) { return n->label; } void ChangeLabelB (int x, node n, bt* T) { n->label=x; } node RootB (bt* T) { return T; } void CreateLeftB (int x, node n, bt* T) { if (n->left!=NULL) { cout << "Greska!" << endl; return; } node child = new element; n->left = child; child->left = NULL; child->right = NULL; child->label = x; } void CreateRightB (int x, node n, bt* T) { if (n->right!=NULL) { cout << "Greska!" << endl; return; } node child = new element; n->right = child; child->left = NULL; child->right = NULL; child->label = x; } void DeleteB (node n, bt* T) { bool rek = false; if (!rek) { node roditelj = ParentB (n, T); if (roditelj->left==n) roditelj->left=NULL; if (roditelj->right==n) roditelj->right==NULL; rek=true; } if (n->left) DeleteB (n->left, T); if (n->right) DeleteB (n->right, T); delete n; } bt* InitB (labeltype x, bt* T) { T = new element; T->left = NULL; T->right = NULL; T->label = x; return T; }