Algorithm-Data Structure-Binary Tree

Source: Internet
Author: User
Algorithm-Data Structure-Binary Tree-general Linux technology-Linux programming and kernel information. The following is a detailed description. /*************************************** *************************
Title: BinTree. c
Author: Zhang Long
Time: 200508
**************************************** *************************/


# Include


/* ============================ Data struct ============ =============== */
Typedef char ElementType;

Typedef struct Node {
ElementType data;
Struct Node * LChild;/* LeftChild */
Struct Node * RChild;/* RightChild */
} TreeNode;





/* ==================================== Function declaration ==== ============= */
Int CreateBinTree (TreeNode ** rootp, ElementType ** lp );
Void FreeTree (TreeNode * rootp );
Int DelSubTree (TreeNode * rootp, ElementType e );
Int Insert (TreeNode * rootp, ElementType Curr, ElementType e, int CurrPos );
TreeNode * FindNode (TreeNode * rootp, ElementType e );
TreeNode * FindParent (TreeNode * rootp, ElementType e );
Void Leaf (TreeNode * rootp, int * np );
Int Depth (TreeNode * rootp );
Void PreOrder (TreeNode * rootp );
Void InOrder (TreeNode * rootp );
Void PostOrder (TreeNode * rootp );




/* ==================================== Function implementation ==== =============== */
/* -------------------------------------------------------- CreateTree -------*/
Int CreateBinTree (TreeNode ** rootp, ElementType ** lp)
{
ElementType CurrElement;
If (* lp = NULL) return 0;/* string isn't exist */
If (** lp = 0) return 1;/* empty string */

Printf ("hello1 \ n ");
CurrElement = ** lp;
Printf ("hello2 \ n ");
(* Lp) ++;
Printf ("hello3 \ n ");
If (CurrElement = '.'){
(* Rootp) = NULL;
Return 1;
}

If (! (* Rootp) = (TreeNode *) malloc (sizeof (TreeNode) return 0;
(* Rootp)-> data = CurrElement;
Printf ("the value of node is % c \ n", (* rootp)-> data );
Printf ("hello4 \ n ");
If (! CreateBinTree (& (* rootp)-> LChild, lp) return 0;
Return CreateBinTree (& (* rootp)-> RChild, lp );
}


/* --------------------------------------------------------- FreeTree --------*/
Void FreeTree (TreeNode * rootp)
{
If (! Rootp) return;
FreeTree (rootp-> LChild );
FreeTree (rootp-> RChild );
Free (rootp );
}

/* ---------------------------------------------------------- DelSubTree --------*/
Int DelSubTree (TreeNode * rootp, ElementType e)
{
TreeNode * temp;
Temp = FindParent (rootp, e );
If (! Temp) return 0;
If (temp-> LChild & (temp-> LChild-> data = e )){
FreeTree (temp-> LChild );
Temp-> LChild = NULL;
}
Else {
FreeTree (temp-> RChild );
Temp-> RChild = NULL;
}
Return 1;
}

/* ---------------------------------------------------------- Insert --------*/
Int Insert (TreeNode * rootp, ElementType Curr, ElementType e, int CurrPos)
{
TreeNode * parent, * new, * temp;
If (! (Parent = FindParent (rootp, Curr) return 0;
If (! (New = (TreeNode *) malloc (sizeof (TreeNode) return 0;
New-> data = e;
If (parent-> LChild-> data = Curr ){
Temp = parent-> LChild;
Parent-> LChild = new;
}
If (parent-> LChild-> data = Curr ){
Temp = parent-> RChild;
Parent-> RChild = new;
}
If (CurrPos = 0 ){
New-> LChild = temp;
New-> RChild = NULL;
}
If (CurrPos = 1 ){
New-> RChild = temp;
New-> LChild = NULL;
}
Return 1;
}

/* --------------------------------------------------------- FindNode --------*/
TreeNode * FindNode (TreeNode * rootp, ElementType e)
{
TreeNode * temp;
If (rootp = NULL) return NULL;/* if (! Rootp )*/
If (rootp-> data = e) return rootp;
If (temp = FindNode (rootp-> LChild, e) return temp;
Return FindNode (rootp-> RChild, e );
}

/* ------------------------------------------------------- FindParent --------*/
TreeNode * FindParent (TreeNode * rootp, ElementType e)
{
TreeNode * temp;
If (rootp = NULL) | (rootp-> LChild = NULL & rootp-> RChild = NULL) return NULL;
If (rootp-> LChild & rootp-> LChild-> data = e) | (rootp-> RChild & rootp-> RChild-> data = e ))
Return rootp;
Temp = FindParent (rootp-> LChild, e );
If (temp) return temp;
Return FindParent (rootp-> RChild, e );
}


/* ------------------------------------------------------------- Leaf --------*/
Void Leaf (TreeNode * rootp, int * np)
{
If (rootp = NULL) return;
If (rootp-> LChild = NULL & rootp-> RChild = NULL ){
(* Np) ++;
Return;
}
Leaf (rootp-> LChild, np );
Leaf (rootp-> RChild, np );
}

/* ------------------------------------------------------------ Depth --------*/
Int Depth (TreeNode * rootp ){
Int ld, rd;/* ld: LeftChild's depth * // * rd: RightChild's depth */
If (rootp = NULL) return 0;
Ld = Depth (rootp-> LChild );
Rd = Depth (rootp-> RChild );
If (ld> rd)
Return ld + 1;
Else
Return rd + 1;
}

/* --------------------------------------------------------- PreOrder --------*/
Void PreOrder (TreeNode * rootp)
{
If (rootp = NULL) return; printf ("% c", rootp-> data);/* show parent */
If (rootp-> LChild! = NULL) PreOrder (rootp-> LChild);/* show LeftChild */
If (rootp-> RChild! = NULL) PreOrder (rootp-> RChild);/* show RightChild */
Return;
}

/* ---------------------------------------------------------- InOrder --------*/
Void InOrder (TreeNode * rootp)
{
If (rootp = NULL) return;
If (rootp-> LChild! = NULL) InOrder (rootp-> LChild );
Printf ("% c", rootp-> data );
If (rootp-> RChild! = NULL) InOrder (rootp-> RChild );
Return;
}

/* -------------------------------------------------------- PostOrder --------*/
Void PostOrder (TreeNode * rootp)
{
If (rootp = NULL) return;
If (rootp-> LChild! = NULL) PostOrder (rootp-> LChild );
If (rootp-> RChild! = NULL) PostOrder (rootp-> RChild );
Printf ("% c", rootp-> data );
Return;
}




/* = ======= */
Int main (int argc, char * argv [])
{
TreeNode ** mytree;
Char * x = "abcdefghijklmn ";
ElementType ** lp;
Printf ("hello \ n ");
Lp = & x;
Printf ("% s \ n", * lp );

CreateBinTree (mytree, lp );
Printf ("\ nthe depth of tree is % d \ n", Depth (* mytree ));
Printf ("\ nthe PreOrder is: \ n ");
PreOrder (* mytree );
Printf ("\ nthe InOrder is: \ n ");
InOrder (* mytree );
Printf ("\ nthe PostOrder is: \ n ");
PostOrder (* mytree );

TreeNode * temp;
Temp = FindParent (* mytree, 'C ');
Printf ("\ nthe parent of c is % c \ n", temp-> data );

Insert (* mytree, 'h', 'O', 1 );
Printf ("\ nthe depth of tree is % d \ n", Depth (* mytree ));
Printf ("\ nthe PreOrder is: \ n ");
PreOrder (* mytree );
Printf ("\ nthe InOrder is: \ n ");
InOrder (* mytree );
Printf ("\ nthe PostOrder is: \ n ");
PostOrder (* mytree );

DelSubTree (* mytree, 'E ');
Printf ("\ nthe depth of tree is % d \ n", Depth (* mytree ));
Printf ("\ nthe PreOrder is: \ n ");
PreOrder (* mytree );
Printf ("\ nthe InOrder is: \ n ");
InOrder (* mytree );
Printf ("\ nthe PostOrder is: \ n ");
PostOrder (* mytree );

Return 1;
}

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.