Binarno stablo -->pokazivac





1
Date Submitted Sun. Jan. 8th, 2012 11:16 AM
Revision 1 of 1
Beginner Sostar_Martin
Tags strukture
Comments 0 comments
pokazivaci


#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;}

 

Martin Sostar

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Beginner Sostar_Martin

Votes Down