Here's what I have so far

template <class KT, class DT>
void my_bst<KT,DT>::show_bst_structure(my_bst_node<KT,DT>*& p) const
{
	my_bst_node<KT,DT>* le=NULL;
	my_bst_node<KT,DT>* ri=NULL;
	//empty tree
	if(p==NULL)
	{
		cout<<"tree is empty\n";
	}
	else
	{
		//print node key
		cout<<p->data<<endl;
		if(p->left!=NULL&&p->right!=NULL)
		{
			show_bst_structure(p->left);
		}
		else if(p->left!=NULL)
		{
			cout<<"enter left\n";
			le=p->left;
			show_bst_structure(le);
		}
		else if(p->right!=NULL)
		{
			cout<<"enter right\n";
			show_bst_structure(ri->right);
		}
		
	}
}

Recommended Answers

All 3 Replies

When printing a binary tree you can either print it in pre, mid, or post
order. I am not going to explain all of these. You should google them.

The usual way one would display a binary tree is by either one of the printing methods from above, pre ,post or min.

The way below is mid order :

void _recursivePrint(_Node* &Parent){					
		if(Parent != _NULL){
			_recursivePrint(Parent->leftChild);			
			std::cout << Parent->data.var <<" ";
			_recursivePrint(Parent->rightChild);			
		}
		else return;
	}

A per order would put the cout statement before the parent->leftChild
recursive call and a post order would put it after the recursive call
parent->rightChild.

What it does is it first goes all the way to the left most node. And prints
it. If the left most node has a right node, then it prints that. It "goes"
up the tree like that. Google it, better explanation out there. Its late
so I can't use my brain much.

i got those functions to work. I need depth-first approach. Is their any way to do this without a stack or queue?

conclusion don't think its possible with just a recursive function since the number of links keep on increasing as you go down the tree :(. Wasted 24 hours on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.