Establish and traverse a binary tree in C Language (recursive or non-recursive)

Source: Internet
Author: User

 

# Include <stdio. h>

# Include <stdlib. h>

# Define MAXSIZE 20

Typedef struct BiTnode {

Char data;

Struct BiTnode * lchild, * rchild;

} BiTnode, * BiTree;

 

// You can see the following:

Int I = 0;

BiTree Create (BiTree t, char s [])

{

Char ch;

Ch = s [I ++];

 

If (ch = '')

{

T = NULL;

}

Else

{

If (! (T = (BiTree) malloc (sizeof (BiTnode ))))

{

Printf ("fail to malloc! \ N ");

Exit (0 );

}

Else

{

T-> data = ch;

T-> lchild = Create (t-> lchild, s );

T-> rchild = Create (t-> rchild, s );

}

}

Return t;

}

 

// A few clicks

/*

Void display (BiTree t)

{

BiTree stack [MAXSIZE], p;

Int top =-1;

If (t)

{

P = t;

While (top>-1 | p)

{

While (p)

{

Stack [++ top] = p;

P = p-> lchild;

}

If (top>-1)

{

P = stack [top --];

Printf ("% c", p-> data );

P = p-> rchild;

}

}

Printf ("\ n ");

}

}

/*/

 

// Please wait until then

/*

Void display (BiTree t)

{

BiTree stack [MAXSIZE], p;

Int top =-1;

If (t)

{

P = t;

While (top>-1 | p)

{

While (p)

{

Printf ("% c", p-> data );

Stack [++ top] = p;

P = p-> lchild;

}

If (top>-1)

{

P = stack [top --];

P = p-> rchild;

}

}

Printf ("\ n ");

}

}

*//*

 

 

// Please wait until then

Void display (BiTree t)

{

BiTree stack [MAXSIZE], p;

Int top =-1;

If (t)

{

Stack [++ top] = t;

While (top>-1)

{

P = stack [top --];

Printf ("% c", p-> data );

If (p-> rchild)

{

Stack [++ top] = p-> rchild;

}

If (p-> lchild)

{

Stack [++ top] = p-> lchild;

}

}

Printf ("\ n ");

}

}*/

/*

// Assume that the parameter is € ;{{}}}}}}}

Void display (BiTree t)

{

BiTree m, stack [MAXSIZE];

Int top =-1;

Int flag;

If (t)

{

Loop:

Flag = 1;

M = NULL;

While (t)

{

Stack [++ top] = t;

T = t-> lchild;

}

While (flag)

{

T = stack [top];

If (t-> rchild = m)

{

Printf ("% c", t-> data );

Top --;

M = t;

}

Else

{

Flag = 0;

T = t-> rchild;

}

}

If (top>-1)

Goto loop;

}

 

Printf ("\ n ");

}

*/

 

// Assume that the parameter is € ;{{}}}}}}}

Void display (BiTree t)

{

BiTree p = t, stack [MAXSIZE];

Int tag [MAXSIZE];

Int top =-1;

Do

{

While (p! = NULL)

{

Stack [++ top] = p;

Tag [top] = 0;

P = p-> lchild;

}

If (top>-1)

{

If (! Tag [top])

{

P = stack [top];

P = p-> rchild;

Tag [top] = 1;

}

Else

{

Printf ("% c", stack [top --]-> data );

}

}

} While (p! = NULL) | (top>-1 ));

Printf ("\ n ");

}

 

// When there are too many threads, there are too many threads

/* Void display (BiTree t)

{

If (t)

{

Printf ("% c", t-> data );

Display (t-> lchild );

Display (t-> rchild) tables?

}

}*/

// Please try again

/* Void display (BiTree t)

{

If (t)

{

Display (t-> lchild );

Printf ("% c", t-> data );

Display (t-> rchild) tables?

}

}*/

// When there are too many threads, there are too many threads

/* Void display (BiTree t)

{

If (t)

{

 

Display (t-> lchild );

Display (t-> rchild );

Printf ("% c", t-> data );

}

}*/

Int main (int argc, char * argv [])

{

BiTree t;

Char s [] = "abc de f g ";

T = Create (t, s );

Display (t );

 

Return 0;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.