inorder traversal in binary tree class

V

Vijay Meena

Hi,

I cant think of an implementation of inorder tree traversal for my
binary tree class given below -

class BTree {
private:
struct Node {
int key;
Node* parent;
Node* left;
Node* right;
Node(int key, Node* parent, Node* left, Node* right);
}* root;
public:
BTree();
void treeInsert(int key);
void treePrint(void); //inorder traversal
//some more API function here
};

inline BTree::BTree() {
root = 0;
}

inline BTree::Node::Node(int key, Node* parent, Node* left, Node*
right) {
this->key = key;
this->parent = parent;
this->right = right;
this->left = left;
}

void BTree::treeInsert(int key) {
Node* node = new Node(key, 0, 0, 0);
Node* parentNode = 0;
Node* placementNode = root;
while(placementNode) {
parentNode = placementNode;
if(node->key < placementNode->key)
placementNode = placementNode->left;
else
placementNode = placementNode->right;
}
node->parent = parentNode;
if(!parentNode)
root = node;
else
if(placementNode == parentNode->left)
parentNode->left = node;
else
parentNode->right = node;
}

void BTree::printTree(void) {
if(!root)
return;
//--- how to implement inorder traversal here ?
}

//main function should be something like -

int main(int argc, char *argv[]) {
BTree btree;
btree.treeInsert(4);
btree.printTree();
}
 
R

red floyd

Vijay said:
Hi,

I cant think of an implementation of inorder tree traversal for my
binary tree class given below -
[code redacted]

Ask your instructor. Inorder, preorder, and postorder traversal of
is one of the fundamental operations performed on a binary tree.

You didn't even bother to *TRY* to do the assignment of inorder
traversal, why should we do your homework for you.

But here's a hint: What goes before and what goes after
the current node in an inorder traversal?

See FAQ 5.2

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top