I'm trying to implement a binary tree (NOT a binary search tree). It'll be mostly a class template consisting of insert/delete/search & clear procedures. The data held in the nodes could be anything. Something like below :
template<class T>
class BinaryTree
{
public:
BinaryTree(int size);
~BinaryTree();
virtual bool insert(T data);
virtual bool remove(T data);
virtual void clear();
virtual bool search(T data);
virtual void display(std::string str, ETravType type);
virtual unsigned int getSize();
//friend void swapWithLastNode(Node *root, Node **nodeToRemove);
protected:
virtual void inorder(Node<T>* root);
virtual void preorder(Node<T>* root);
virtual void postorder(Node<T>* root);
virtual void removeAll(Node<T>* root);
Node<T>* root;
int max;
unsigned int curSize;
};
I need some help on the algorithms, preferably iterative (want to avoid recursive due to stack size restrictions), to use for insert, search & delete:
Insert: How to ensure that the tree doesn't get left/right skewed ?
Delete: What is the best way to go about deletion when the node has both the children (For ex: In BST, you replace with inorder successor).
Search: Is there any efficient way so as to prevent a O(n) search ?
There's an abundance of resources on the web for BST procedures (mostly recursive), but none for Binary Tree (or atleast I was not able to find anything). Hence thought of posting it here.