Hi I have to implement this code parts
first code
second code
third code
at my code for linkedbinary tree
#include <iostream>
using namespace std;
template <typename Object>
class LinkedBinaryTree {
protected:
struct Node {
Object element;
Node* parent;
Node* left;
Node* right;
Node() : element(Object())
{ parent = left = right = NULL; }
Node* sibling() const {
return (this == parent->left ? parent->right : parent->left);
}
};
typedef Node* NodePtr;
public:
class Position {
private:
NodePtr node;
public:
Position(NodePtr n = NULL)
{ node = n; }
Object& element() const
{ return node->element; }
bool isNull() const
{ return node == NULL; }
friend class LinkedBinaryTree;
};
private:
NodePtr theRoot;
int sz;
protected:
NodePtr nodePtr(const Position& v) const
{ return v.node; }
bool isExternal(NodePtr n) const
{ return (n->left == NULL && n->right == NULL); }
bool isInternal(NodePtr n) const
{ return ! isExternal(n); }
bool isRoot(NodePtr n) const
{ return (n == theRoot); }
void setRoot(NodePtr r)
{ theRoot = r; r->parent = NULL; }
void replaceElement(NodePtr n, const Object& o)
{ n->element = o; }
void swapElements(NodePtr n, NodePtr w) {
Object temp = w->element;
w->element = n->element;
n->element = temp;
}
void expandExternal(NodePtr n) {
n->left = new Node; n->left->parent = n;
n->right = new Node; n->right->parent = n;
sz += 2;
}
NodePtr removeAboveExternal(NodePtr n) {
NodePtr p = n->parent;
NodePtr s = n->sibling();
if (isRoot(p)) setRoot(s);
else {
NodePtr g = p->parent;
if (p == g->left) g->left = s;
else g->right = s;
s->parent = g;
}
delete n; delete p;
sz -= 2;
return s;
}
public:
LinkedBinaryTree()
{ theRoot = new Node; sz = 1; }
int size() const
{ return sz; }
bool isEmpty() const
{ return (sz == 0); }
Position root() const
{ return Position(theRoot); }
Position leftChild(const Position& v) const
{ return Position(nodePtr(v)->left); }
Position rightChild(const Position& v) const
{ return Position(nodePtr(v)->right); }
bool isRoot(const Position& v) const
{ return isRoot(nodePtr(v)); }
bool isInternal(const Position& v) const
{ return isInternal(nodePtr(v)); }
bool isExternal(const Position& v) const
{ return isExternal(nodePtr(v)); }
void replaceElement(const Position& v, const Object& o)
{ replaceElement(nodePtr(v), o); }
void swapElements(const Position& v, const Position& w)
{ swapElements(nodePtr(v), nodePtr(w)); }
void expandExternal(const Position& v)
{ expandExternal(nodePtr(v)); }
Position removeAboveExternal(const Position& v)
{ return Position(removeAboveExternal(nodePtr(v))); }
};
class Int {
public:
Int() { counter++; num = counter; }
private:
static int counter;
int num;
friend ostream& operator<<(ostream& out, Int i);
};
ostream& operator<<(ostream& out, Int i)
{ out << i.num << " ";
return out;
}
typedef LinkedBinaryTree<Int> Tree;
typedef Tree::Position Position;
void binaryInorderPrint(const Tree& T, const Position& v)
{ if (T.isInternal(v))
binaryInorderPrint(T, T.leftChild(v));
cout << v.element();
if (T.isInternal(v))
binaryInorderPrint(T, T.rightChild(v));
}
int Int::counter = 0;
int main()
{ LinkedBinaryTree<Int> btree;
btree.expandExternal(btree.root());
binaryInorderPrint(btree, btree.root());
cout << endl;
btree.expandExternal(btree.leftChild(btree.root()));
binaryInorderPrint(btree, btree.root());
return 0;
}
but I have no idea how can some one take me some idea ?