#ifndef TREE_H␍␊ |
#define TREE_H␍␊ |
␍␊ |
#include "Node.h"␍␊ |
#include <vector>␍␊ |
␍␊ |
template <class T>␍␊ |
class Tree␍␊ |
{␍␊ |
private:␍␊ |
␉void _DeleteTree(Node<T> * node)␍␊ |
␉{␍␊ |
␉␉cout << "Killing node: " << node->object << endl;␍␊ |
␉␉if (node->left != null)␍␊ |
␉␉␉_DeleteTree(node->left);␍␊ |
␉␉if (node->right != null)␍␊ |
␉␉␉_DeleteTree(node->right);␍␊ |
␉␉delete node;␍␊ |
␉␉node = null;␍␊ |
␉}␍␊ |
public:␍␊ |
␉Node<T> * root;␍␊ |
␍␊ |
␉void DeleteTree()␍␊ |
␉{␍␊ |
␉␉this->_DeleteTree(root);␍␊ |
␉}␍␊ |
␉Tree(Node<T> * root)␍␊ |
␉{␍␊ |
␉␉this->root = root;␍␊ |
␉}␍␊ |
␉~Tree()␍␊ |
␉{␍␊ |
␉␉//DeleteTree(root);␍␊ |
␉}␍␊ |
␍␊ |
␉std::vector<Node<T> *> getBoundary()␍␊ |
␉{␍␊ |
␉␉std::vector<Node<T> *> edges;␍␊ |
␉␉edges.push_back(root);␍␊ |
␉␉std::vector<Node<T> *> tmp;␍␊ |
␉␉tmp = getLeftEdges(root->left);␍␊ |
␉␉edges.insert(edges.end(), tmp.begin(), tmp.end());␍␊ |
␉␉tmp = getRightEdges(root->right);␍␊ |
␉␉edges.insert(edges.end(), tmp.begin(), tmp.end());␍␊ |
␉␉return edges;␍␊ |
␉}␍␊ |
␍␊ |
␉std::vector<Node<T> *> getLeftEdges(Node<T> * nodes)␍␊ |
␉{␍␊ |
␉␉std::vector<Node<T> *> ret;␍␊ |
␉␉if (nodes == null)␍␊ |
␉␉␉return ret;␍␊ |
␉␉if (nodes->parent->right == nodes) //if the node is a right child␍␊ |
␉␉{␍␊ |
␉␉␉//add current node␍␊ |
␉␉␉if (nodes->right == null && nodes->left == null)␍␊ |
␉␉␉␉ret.push_back(nodes);␍␊ |
␉␉} ␍␊ |
␉␉else if (nodes->parent->left == nodes) // if the node is a left child␍␊ |
␉␉{␍␊ |
␉␉␉ret.push_back(nodes);␍␊ |
␉␉}␍␊ |
␉␉std::vector<Node<T> *> tmp;␍␊ |
␉␉tmp = getLeftEdges(nodes->left);␍␊ |
␉␉ret.insert(ret.end(), tmp.begin(), tmp.end());␍␊ |
␉␉tmp = getLeftEdges(nodes->right);␍␊ |
␉␉ret.insert(ret.end(), tmp.begin(), tmp.end());␍␊ |
␉␉return ret;␍␊ |
␉}␍␊ |
␍␊ |
␉std::vector<Node<T> *> getRightEdges(Node<T> * nodes)␍␊ |
␉{␍␊ |
␉␉std::vector<Node<T> *> ret;␍␊ |
␉␉if (nodes == null)␍␊ |
␉␉␉return ret;␍␊ |
␉␉␍␊ |
␉␉std::vector<Node<T> *> tmp;␍␊ |
␉␉tmp = getRightEdges(nodes->left);␍␊ |
␉␉ret.insert(ret.end(), tmp.begin(), tmp.end());␍␊ |
␉␉tmp = getRightEdges(nodes->right);␍␊ |
␉␉ret.insert(ret.end(), tmp.begin(), tmp.end());␍␊ |
␉␉if (nodes->parent->right == nodes) //if the node is a right child␍␊ |
␉␉{␍␊ |
␉␉␉//add current node␍␊ |
␉␉␉ret.push_back(nodes);␍␊ |
␉␉} ␍␊ |
␉␉else if (nodes->parent->left == nodes) // if the node is a left child␍␊ |
␉␉{␍␊ |
␉␉␉if (nodes->right == null && nodes->left == null)␍␊ |
␉␉␉␉ret.push_back(nodes);␍␊ |
␉␉}␍␊ |
␉␉return ret;␍␊ |
␉}␍␊ |
};␍␊ |
␍␊ |
#endif |