Basic Binary Tree operations (2)

Source: Internet
Author: User

[Cpp]
// File name: exp7-1.cpp
# Include <stdio. h>
Typedef char ElemType;
Typedef struct node
{
ElemType data; // data Element
Struct node * lchild; // point to left child
Struct node * rchild; // point to the right child
} BTNode;
Extern void CreateBTNode (BTNode * & B, char * str );
Extern BTNode * FindNode (BTNode * B, ElemType x );
Extern BTNode * LchildNode (BTNode * p );
Extern BTNode * RchildNode (BTNode * p );
Extern int BTNodeDepth (BTNode * B );
Extern void DispBTNode (BTNode * B );
Extern int BTWidth (BTNode * B );
Extern int Nodes (BTNode * B );
Extern int LeafNodes (BTNode * B );
Extern void DestroyBTNode (BTNode * & B );
Int main ()
{BTNode * B, * p, * lp, * rp ;;
CreateBTNode (B, "A (B (D, E (H (J, K (L, M (, N), C (F, G (, I )))");
Printf ("basic binary tree operation: \ n ");
Printf ("(1) output binary tree:"); DispBTNode (B); printf ("\ n ");
Printf ("(2) H node :");
P = FindNode (B, 'H ');
If (p! = NULL)
{Lp = LchildNode (p );
If (lp! = NULL)
Printf ("% c for left children", lp-> data );
Else
Printf ("No left child ");
Rp = RchildNode (p );
If (rp! = NULL)
Printf ("% c for the right child", rp-> data );
Else
Printf ("no right child ");
}
Printf ("\ n ");
Printf ("(3) Depth of Binary Tree B: % d \ n", BTNodeDepth (B ));
Printf ("(4) width of Binary Tree B: % d \ n", BTWidth (B ));
Printf ("(5) number of Nodes of Binary Tree B: % d \ n", Nodes (B ));
Printf ("(6) Number of leaf nodes of Binary Tree B: % d \ n", LeafNodes (B ));
Printf ("(7) Release Binary Tree B \ n ");
DestroyBTNode (B );
}

// File name: exp7-1.cpp
# Include <stdio. h>
Typedef char ElemType;
Typedef struct node
{
ElemType data; // data Element
Struct node * lchild; // point to left child
Struct node * rchild; // point to the right child
} BTNode;
Extern void CreateBTNode (BTNode * & B, char * str );
Extern BTNode * FindNode (BTNode * B, ElemType x );
Extern BTNode * LchildNode (BTNode * p );
Extern BTNode * RchildNode (BTNode * p );
Extern int BTNodeDepth (BTNode * B );
Extern void DispBTNode (BTNode * B );
Extern int BTWidth (BTNode * B );
Extern int Nodes (BTNode * B );
Extern int LeafNodes (BTNode * B );
Extern void DestroyBTNode (BTNode * & B );
Int main ()
{BTNode * B, * p, * lp, * rp ;;
CreateBTNode (B, "A (B (D, E (H (J, K (L, M (, N), C (F, G (, I )))");
Printf ("basic binary tree operation: \ n ");
Printf ("(1) output binary tree:"); DispBTNode (B); printf ("\ n ");
Printf ("(2) H node :");
P = FindNode (B, 'H ');
If (p! = NULL)
{Lp = LchildNode (p );
If (lp! = NULL)
Printf ("% c for left children", lp-> data );
Else
Printf ("No left child ");
Rp = RchildNode (p );
If (rp! = NULL)
Printf ("% c for the right child", rp-> data );
Else
Printf ("no right child ");
}
Printf ("\ n ");
Printf ("(3) Depth of Binary Tree B: % d \ n", BTNodeDepth (B ));
Printf ("(4) width of Binary Tree B: % d \ n", BTWidth (B ));
Printf ("(5) number of Nodes of Binary Tree B: % d \ n", Nodes (B ));
Printf ("(6) Number of leaf nodes of Binary Tree B: % d \ n", LeafNodes (B ));
Printf ("(7) Release Binary Tree B \ n ");
DestroyBTNode (B );
}


 

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.