/ Published in: C++
Implementacija binarnog stabla pomoću pokazivaÄa
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> using namespace std; struct element { int label; element *lijevi, *desni; }; element *bin_stablo = new element; element *Rootb (element *bin_stablo) { return bin_stablo; } int Labelb (element *el) { return el->label; } element *Parentb (element *trazi, element *bin_stablo) { element *f=NULL, *s=NULL; if (bin_stablo->lijevi == trazi || bin_stablo->desni == trazi) return bin_stablo; if (bin_stablo->lijevi != NULL) f = Parentb(trazi, bin_stablo->lijevi); if (bin_stablo->desni != NULL) s = Parentb(trazi, bin_stablo->desni); if (f != NULL) return f; if (s != NULL) return s; return NULL; } element *LeftChildb (element *el) { return el->lijevi; } element *RightChildb (element *el) { return el->desni; } void ChangeLabelb (int x, element *el) { el->label = x; } bool CreateLeftb (int x, element *el) { if (LeftChildb(el) != NULL) return false; element *novi = new element; el->lijevi = novi; novi->label = x; novi->lijevi = NULL; novi->desni = NULL; return true; } bool CreateRightb (int x, element *el) { if (RightChildb(el) != NULL) return false; element *novi = new element; el->desni = novi; novi->label = x; novi->lijevi = NULL; novi->desni = NULL; return true; } void Deleteb (element *el, element *bin_stablo) { if (el->lijevi != NULL) Deleteb(el->lijevi, bin_stablo); if (el->desni != NULL) Deleteb(el->desni, bin_stablo); if (el != Rootb(bin_stablo) && LeftChildb(Parentb(el, bin_stablo))==el) Parentb(el, bin_stablo)->lijevi = NULL; else if (el != Rootb(bin_stablo)) Parentb(el, bin_stablo)->desni = NULL; delete el; } void Initb (int x, element *bin_stablo) { bin_stablo->desni = NULL; bin_stablo->lijevi = NULL; bin_stablo->label = x; } element *AntiLabelb (element *el, int x) { if (el->label == x) return el; if (el->lijevi != NULL) if (AntiLabelb(el->lijevi, x) != NULL) return AntiLabelb(el->lijevi, x); if (el->desni != NULL) if (AntiLabelb(el->desni, x) != NULL) return AntiLabelb(el->desni, x); return NULL; }