[Turn] clue binary tree creation, traversal

Reprinted from: http://blog.csdn.net/cheneagle/article/details/4397750

 

     The clue binary tree uses the null pointer of the end node to connect other nodes, so that the entire branch can be traversed in order and reverse order. Because any binary tree with n nodes will always have n+1 empty pointers. For example, if there is a binary tree with 1 node, then 2 or so children are null pointers, and so on. In this way, the space is fully utilized to achieve the effect of fast traversal. Please see the source code for details:

    In order to find it quickly in the future, I will briefly explain how to input data, as shown in the data structure below:

 

Binary tree input list

How do we enter a data structure like this?

Enter ab# for the first time, then press Enter.

Enter c## for the second time, then press Enter.

Enter d# for the third time, then press Enter.

Enter g## for the fourth time, then press Enter.

I don't know if I or my netizens will understand it in the future? I hope that in the future, my understanding ability and everyone's ability to comprehend is stronger than the XX power of these pale words now!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>

typedef char DataType;

typedef struct _ThreadBiTreeNode
{
	DataType data;
	int ltag, rtag;
	struct _ThreadBiTreeNode *right, *left;
}ThreadBiTreeNode, *ThreadBiTree;

/**
  * Pre-order method to create clue binary tree
  * Please input ab# in the following way, then press Enter; c##, then press Enter;
  * d#, then enter; g##, then enter.
  */
ThreadBiTree CreateTree( )
{
	ThreadBiTree t;
	DataType ch;
	cin >> ch;
	if( ch == '#' )
		t = NULL;
	else
	{
		t = ( ThreadBiTree )malloc( sizeof( ThreadBiTreeNode ) );
		if( t )
		{
			t->data = ch;
			t->ltag = t->rtag = 0;
			t->left = CreateTree();
			t->right = CreateTree();
		}
	}
	return t;
}

/**
  * Preorder clue binary tree, point a null pointer to a connection
  * Here to apply for a global variable g_pre
  *
  */
ThreadBiTree g_pre;

void PreThreadTree( ThreadBiTree t )
{
	if( t == NULL )
		return;
	
	if( t->left == NULL )
		t->ltag = 1;
	if( t->right == NULL )
		t->rtag = 1;

	if( g_pre != NULL && t->ltag == 1 )
		t->left = g_pre;
	if( g_pre != NULL && g_pre->rtag == 1  )
		g_pre->right = t;
	g_pre = t;

	if( t->ltag == 0 )
		PreThreadTree( t->left );
	if( t->rtag == 0 )
		PreThreadTree( t->right );
}

/**
  * Inorder clue binary tree
  *
  *
  */
void InThreadTree( ThreadBiTree t )
{
	if( t == NULL )
		return;

	InThreadTree( t->left );
	if( t->left == NULL )
		t->ltag = 1;
	if( t->right == NULL )
		t->rtag = 1;

	if( g_pre != NULL && t->ltag == 1 )
		t->left = g_pre;
	if( g_pre != NULL && g_pre->rtag == 1  )
		g_pre->right = t;
	g_pre = t;

	if( t->rtag == 0 )
		InThreadTree( t->right );
}

/**
  * Post-order clue binary tree
  *
  *
  */
void PostThreadTree( ThreadBiTree t )
{
	if( t == NULL )
		return;

	PostThreadTree( t->left );
	PostThreadTree( t->right );

	if( t->left == NULL )
		t->ltag = 1;
	if( t->right == NULL )
		t->rtag = 1;

	if( g_pre != NULL && t->ltag == 1 )
		t->left = g_pre;
	if( g_pre != NULL && g_pre->rtag == 1  )
		g_pre->right = t;
	g_pre = t;
}

/**
  * Find the next node of the current node
  *  
  *
  */
ThreadBiTree FindNode( ThreadBiTree t )
{
	if( t->rtag == 1 )
		return t->right;
	else
	{
		t = t->right;
		while( t->ltag == 0 )
		{
			t = t->left;
		}	
		return t;
	}
}

/**
  * Traverse the clue binary tree in inorder
  * Successfully tested under VC6.0
  *
  */
void InOrderThreadBiTree( ThreadBiTree t )
{
	if( t == NULL )
		return;
	printf( "%c ", t->data );
	ThreadBiTree p;
	
	//When there is only one element, the clue binary tree is not established
	if( t->ltag == 0 )
		p = t->left;
	else
		p = t->right;

	while( p && ( p != t ) )
	{
		while( p->ltag == 0  )
		{	
			printf( "%c ", p->data );
			p = p->left;
		}
		printf( "%c ", p->data );
		
		while( p->rtag == 1 && p->right != t && p->right )
		{
			p = p->right;
			printf( "%c ", p->data );
		}
		p = p->right;
	}
}

/**
  * Insert the right node: refer to the online code, some places need to be discussed.
  *
  *	
  */
void InsertRightNode( ThreadBiTree p, ThreadBiTree node )
{
    node->ltag = node->rtag = 1;//node->rtag=1 why set 1, because there is node->right=p->right; this sentence
    node->left = p; //this can be considered node->rtag = 0.
    node->right = p->right;//You can also make it node->left = p->right, (^_^) is it a horn?
    p->right = p; //Of course, because the clues are connected, in order not to affect the previously set clues, this is done, and we will not verify it for the time being!
    p->rtag = 0;
}

int main( void )
{
	ThreadBiTree t;
	t = CreateTree();

	PreThreadTree( t  );
	InOrderThreadBiTree( t );
	return 0;
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038782&siteId=291194637