Binary Tree Algorithm set

Source: Internet
Author: User


# Include <stdlib. h>
# Include <stdio. h>
# Define MAX 50
# Define MAS 20
# Define CHAR 1
# If CHAR
Typedef char TElemType;
TElemType Nil = '';
# Define form "% c"
# Else
Typedef int TElemType;
TElemType Nil = 0;
# Define form "% d"
# Endif
Typedef struct node
{TElemType data;
Struct node * left;
Struct node * right;
Struct node * parent;
} BiTNode, * BiTree;
BiTNode * InitBiTree (BiTNode * bt)
{
Bt = NULL;
Return bt;
}
BiTNode * CreateBiTree (BiTNode * bt)
{TElemType ch;
Scanf (form, & ch );
If (ch = Nil) bt = NULL;
Else
{Bt = (BiTNode *) malloc (sizeof (BiTNode ));
If (! Bt) exit (0 );
Bt-> data = ch; bt-> parent = NULL;
Bt-> left = CreateBiTree (bt-> left );
If (bt-> left) bt-> left-> parent = bt;
Bt-> right = CreateBiTree (bt-> right );
If (bt-> right) bt-> right-> parent = bt;
}
Return bt;
}
Void PrintTree (BiTNode * bt, int I)
{If (bt! = NULL)
{PrintTree (bt-> right, I + 5 );
# If CHAR
If (bt-> data! = Nil)
Printf ("% * cn", I, bt-> data );
# Else
If (bt-> data! = Nil)
Printf ("% * dn", I, bt-> data );
# Endif
PrintTree (bt-> left, I + 5 );
I = I-5;
}
}
Void Prorder1 (BiTNode * bt, void (* visit) (TElemType)/* sequential traversal */
{If (bt! = NULL)
{Visit (bt-> data );
Prorder1 (bt-> left, visit );
Prorder1 (bt-> right, visit );
}
}
Void Prorder2 (BiTNode * bt, void (* visit) (TElemType)/* sequential traversal */
{BiTNode * p, * stack [MAS];
Int top;
Top = 0; p = bt;
While (top! = 0 | p! = NULL)
{While (p! = NULL)
{Stack [top] = p; top ++;
P = p-> left;
}
If (top! = 0)
{P = stack [top-1];
Top --;
Visit (p-> data );
P = p-> right;
}
}
}
Void Prorder3 (BiTNode * bt, void (* visit) (TElemType)/* post-sequential traversal */
{BiTNode * p,

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.