🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: fj.lang.visualbasic
3 messages
3 total messages Started by Ben Smith Tue, 07 Apr 1998 00:00
Binary Trees
#265
Author: Ben Smith
Date: Tue, 07 Apr 1998 00:00
18 lines
1025 bytes
Hi,

I'm trying to make a Binary Tree program in Visual Basic5

I'm a bit stuck on writting the code to find the predecessor and
successor of a node. And also on the Preorder and Postorder traversal.
I can do the inorder code, if they are similar.

Is there anyone who could help me?

Please respond by e-mail: 

bsmith01@globalnet.co.uk

Thanks,

Ben Smith
Re: Binary Trees
#271
Author: Jos A. Horsmeier
Date: Wed, 08 Apr 1998 00:00
106 lines
3484 bytes
Ben Smith wrote:

> I'm trying to make a Binary Tree program in Visual Basic5
> I'm a bit stuck on writting the code to find the predecessor and
> successor of a node. And also on the Preorder and Postorder traversal.
> I can do the inorder code, if they are similar.
> Is there anyone who could help me?

Have you considered adding a 'parent' pointer to each node, i.e.
instead of the regular 'left' and 'right' pointers, add a third
pointer 'parent' pointing the parent, to each node. All your 
problems will vanish instantly ... Note that the parent pointer
of the root of the tree points nowhere (NULL).

Here's some pseudo code; I assume the elementary functions
'parent(node)', 'left(node)' and 'right(node)' already present.

first(node ) {				/* find smallest element in the tree */

	if (node != NULL) 		/* is tree not empty?)
		while (left(node))	/* move down leftwards */
			root= left(node);
	
	return (node ;
} 

last(node ) {				/* find largest element in the tree */

	if (node  != NULL)		/* is tree not emty? */  
		while (right(node ))	/* move down rightwards */
			root= right(node);

	return root;
}

next(node) {				/* find next element in the tree */

	pointer r= right(node);

	if (r != NULL)			/* right child present? */
		r= first(r);		/* take smallest kid from this subtree */
	else {
		r= parent(node);	/* we're the 'right' kid, move up */
		while (r != NULL && node == right(r)) {
			node= r;
			r   = parent(r);
		}
	}	
	return r;
}

prev(node) {				/* find previous element in th tree */

	pointer l= left(node);

	if (l != NULL)			/* left child present? */
		l= last(l);		/* take largest kid from this subtree */
	else {
		l= parent(node);	/* we're the 'left' kid, move up */
		while (l != NULL && node == left(l)) {
			node= l;
			l   = parent(l);
		}
	}
	return l;
}

Inorder traversal is a walk over the tree with all elements in order,
so:

inorder(tree) {

	pointer node;

	node= first(tree);			/* leftmost element */

	while (node != NULL) {			/* travese the tree */	
		process(node);			/* do something with 'node' */
		node= next(node);
	}
}

Preorder and postorder walks can be easily implemented using recursion:

preorder(tree) {

	if (tree) {
		process(tree);			/* do something with 'node' */
		preorder(left(tree));		/* similar with left tree */	
		preorder(right(tree));		/* and the right tree */
	}
}

postorder(tree) {

	if (tree) {
		postorder(left(tree));		/* similar with left tree */	
		postorder(right(tree));		/* and the right tree */
		process(tree);			/* do something with 'node' */
	}
}

kind regards,

Jos aka jos@and.nl
Re: Binary Trees
#154
Author: ���c �N��
Date: Sat, 11 Apr 1998 00:00
111 lines
3728 bytes


Jos A. Horsmeier <jos@and.nl> wrote in article
<352B713D.A246A05E@and.nl>...
> Ben Smith wrote:
> 
> > I'm trying to make a Binary Tree program in Visual Basic5
> > I'm a bit stuck on writting the code to find the predecessor and
> > successor of a node. And also on the Preorder and Postorder traversal.
> > I can do the inorder code, if they are similar.
> > Is there anyone who could help me?
> 
> Have you considered adding a 'parent' pointer to each node, i.e.
> instead of the regular 'left' and 'right' pointers, add a third
> pointer 'parent' pointing the parent, to each node. All your 
> problems will vanish instantly ... Note that the parent pointer
> of the root of the tree points nowhere (NULL).
> 
> Here's some pseudo code; I assume the elementary functions
> 'parent(node)', 'left(node)' and 'right(node)' already present.
> 
> first(node ) {				/* find smallest element in the tree */
> 
> 	if (node != NULL) 		/* is tree not empty?)
> 		while (left(node))	/* move down leftwards */
> 			root= left(node);
> 	
> 	return (node ;
> } 
> 
> last(node ) {				/* find largest element in the tree */
> 
> 	if (node  != NULL)		/* is tree not emty? */  
> 		while (right(node ))	/* move down rightwards */
> 			root= right(node);
> 
> 	return root;
> }
> 
> next(node) {				/* find next element in the tree */
> 
> 	pointer r= right(node);
> 
> 	if (r != NULL)			/* right child present? */
> 		r= first(r);		/* take smallest kid from this subtree */
> 	else {
> 		r= parent(node);	/* we're the 'right' kid, move up */
> 		while (r != NULL && node == right(r)) {
> 			node= r;
> 			r   = parent(r);
> 		}
> 	}	
> 	return r;
> }
> 
> prev(node) {				/* find previous element in th tree */
> 
> 	pointer l= left(node);
> 
> 	if (l != NULL)			/* left child present? */
> 		l= last(l);		/* take largest kid from this subtree */
> 	else {
> 		l= parent(node);	/* we're the 'left' kid, move up */
> 		while (l != NULL && node == left(l)) {
> 			node= l;
> 			l   = parent(l);
> 		}
> 	}
> 	return l;
> }
> 
> Inorder traversal is a walk over the tree with all elements in order,
> so:
> 
> inorder(tree) {
> 
> 	pointer node;
> 
> 	node= first(tree);			/* leftmost element */
> 
> 	while (node != NULL) {			/* travese the tree */	
> 		process(node);			/* do something with 'node' */
> 		node= next(node);
> 	}
> }
> 
> Preorder and postorder walks can be easily implemented using recursion:
> 
> preorder(tree) {
> 
> 	if (tree) {
> 		process(tree);			/* do something with 'node' */
> 		preorder(left(tree));		/* similar with left tree */	
> 		preorder(right(tree));		/* and the right tree */
> 	}
> }
> 
> postorder(tree) {
> 
> 	if (tree) {
> 		postorder(left(tree));		/* similar with left tree */	
> 		postorder(right(tree));		/* and the right tree */
> 		process(tree);			/* do something with 'node' */
> 	}
> }
> 
> kind regards,
> 
> Jos aka jos@and.nl
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads