|
|
|
Binarno stablo -->pokazivac
#include <iostream>
using namespace std;
struct element {
int label;
element *left, *right;
};
element *bin_tree = new element;
element *RootB (element *bin_tree) {
return bin_tree;}
int LabelB (element *el) {
return el->label;}
element *ParentB (element *search, element *bin_tree) {
element *f=NULL, *s=NULL;
if (bin_tree->left == search || bin_tree->right == search)
return bin_tree;
if (bin_tree->left != NULL)
f = ParentB(search, bin_tree->left);
if (bin_tree->right != NULL)
s = ParentB(search, bin_tree->right);
if (f != NULL)
return f;
if (s != NULL)
return s;
return NULL;}
element *LeftChildB (element *el) {
return el->left;}
element *RightChildB (element *el) {
return el->right;}
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->left = novi;
novi->label = x;
novi->left = NULL;
novi->right = NULL;
return true;}
bool CreateRightB (int x, element *el) {
if (RightChildB(el) != NULL)
return false;
element *novi = new element;
el->right = novi;
novi->label = x;
novi->left = NULL;
novi->right = NULL;
return true;}
void DeleteB (element *el, element *bin_tree) {
if (el->left != NULL)
DeleteB(el->left, bin_tree);
if (el->right != NULL)
DeleteB(el->right, bin_tree);
if (el != RootB(bin_tree) && LeftChildB(ParentB(el, bin_tree))==el)
ParentB(el, bin_tree)->left = NULL;
else if (el != RootB(bin_tree))
ParentB(el, bin_tree)->right = NULL;
delete el;}
void InitB (int x, element *bin_tree) {
bin_tree->right = NULL;
bin_tree->left = NULL;
bin_tree->label = x;}
element *AntiLabelB (element *el, int x) {
if (el->label == x)
return el;
if (el->left != NULL)
if (AntiLabelB(el->left, x) != NULL)
return AntiLabelB(el->left, x);
if (el->right != NULL)
if (AntiLabelB(el->right, x) != NULL)
return AntiLabelB(el->right, x);
return NULL;}




There are currently no comments for this snippet.