So, I would wonder if it was easy to set all the values of a binary search tree in a simple inorder traversal, my thought being that since inorder traversal hits all the nodes anyway, and saves them in sorted order. Why not make it a generalized 'traverse' function that sets all the values, for instantaneous access later on?
My question: Is there a way to optimize this? Using iteration instead of recursion (edit: I know it is possible now, using a stack of pointers http://en.wikipedia.org/wiki/Tree_traversal#Implementations)
Can I modify it (or use a similar algorithm) to set successor, predecessor, or height of each node?
#include<iostream>
#include<list>
using namespace std;
class Node{
public:
    Node* left; 
    Node* right;
    Node* parent;
    int data;
    int level;
    bool isLeafNode;
    bool isLeftChild;
    bool hasLeftChild;
    bool hasRightChild;
    bool hasSibling;
public: Node(int x){
    right=NULL;
    left=NULL;
    parent=NULL;
    data = x;
    level=0;
    isLeafNode=false;
    isLeftChild=true;
    hasLeftChild=false;
    hasRightChild=false;
    hasSibling=false;
    }
};
class BinarySearchTree{
public: 
   Node* root; 
   list<int> inorder;
   int total; //total number of nodes
   int depth; //maximum level of any node in tree. Level of root is zero
   int total_leaf_nodes; //total number of leaf nodes
   int total_non_leaf_nodes;
public: BinarySearchTree(){
    root=NULL; 
    total=0;
    depth=0;
    total_leaf_nodes=0;
    total_non_leaf_nodes=0;
}
    void traverse(Node* p, int level){ //reset inorder, pass (root,0) to call.
           //go left i.e. 'L'
           if(p->left!=NULL) traverse(p->left, level+1);
           //visit Node i.e. 'V'
           total++;
           inorder.push_back(p->data); //pushes to last position
           p->level=level;
           if(level > depth) depth=level;
           if(p->left==NULL && p->right==NULL){
               p->isLeafNode=true;
               total_leaf_nodes++;
           }
           else{
              total_non_leaf_nodes++;
              if(p->left!=NULL)p->hasLeftChild=true;
              if(p->right!=NULL){    
                  p->hasRightChild=true;
                  (p->right)->isLeftChild=false;
              }
              if(p->left!=NULL && p->right!=NULL){
                  (p->left)->hasSibling=true;
                  (p->right)->hasSibling=true;
              }
           }
           //go right i.e. 'R'
           if(p->right!=NULL) traverse(p->right, level+1); 
    }
};
