/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> using namespace std; typedef struct element { int label; struct element *left,*right,*parrent; } *btree, *node; btree init(int x) { btree myBTree; myBTree=(btree)malloc(sizeof(struct element)); myBTree->left=NULL; myBTree->right=NULL; myBTree->parrent=NULL; myBTree->label=x; return myBTree; } node root(btree T){ return T; } node parrent(node n, btree T) { if(n==T) { cout << "Zatrazili ste roditelja korjenskog cvora\n" ; return NULL; } else { return n->parrent; } } node leftChild(node n, btree T) { if(n->left == NULL) { return NULL; } else { return n->left; } } node rightChild(node n, btree T) { if(n->right == NULL) { return NULL; } else { return n->right; } } int label(node n, btree T) { return n->label; } void changeLabel(int x, node n, btree T) { n->label = x; } void createLeft(int x, node n, btree T) { node child; if(n->left != NULL) { cout << "Lijevo dijete ovog cvora vec postoji\n"; return; } child = (node)malloc(sizeof(struct element)); child->left = NULL; child->right = NULL; child->parrent = n; n->left = child; child->label = x; } void createRight(int x, node n, btree T) { node child; if(n->right != NULL) { cout << "Desno dijete ovog cvora vec postoji\n"; return; } child = (node)malloc(sizeof(struct element)); child->left = NULL; child->right = NULL; child->parrent = n; n->right = child; child->label = x; } void deleteN(node n, btree T){ if( n == NULL ) return; deleteN(leftChild(n, T), T); node parrent; parrent = n->parrent; if(parrent->left == n) parrent->left = NULL; if(parrent->right == n) parrent->right=NULL; free(n); n = parrent; deleteN(rightChild(n, T), T); } void deleteNode(node n, btree T ){ if(n->parrent==NULL) { cout << "Ne mogu obrisati korjen\n"; return; } node parrent; parrent = n->parrent; if(parrent->left == n) parrent->left = NULL; if(parrent->right == n) parrent->right = NULL; deleteN(n, T); }