Binary Tree build Binary Tree first, middle, and back-order traversal to calculate the number of leaf knots and the depth of the tree

Source: Internet
Author: User

# Include <malloc. h>
# Include <iostream. h>
# Include <stdio. h>
Int COUNT = 0, high = 0, H1 = 0, H2 = 0;
Struct Node
{
Char data;
Node * leftchild;
Node * rigthchild;

};
Typedef node * btree;

Int Init (btree & BT)
{
Bt = (btree) malloc (sizeof (node ));
If (bt = 0) return 0;
Bt = NULL;
Return 1;
}
/////////////
// DLR create tree
//// // Create a binary tree
Void create (btree & BTR)
{
Char ch;
Cin> CH;
If (CH = '#')
{
BTR = NULL;
Return;
}
Else
{
BTR = (btree) malloc (sizeof (node ));
BTR-> DATA = CH;
Create (BTR-> leftchild );
Create (BTR-> rigthchild );
}
}

// Output tree DLR Binary Tree first Traversal
Void preorder_dlr (btree & root)
{
If (root! = NULL)
{
Cout <root-> data;
Preorder_dlr (root-> leftchild );
Preorder_dlr (root-> rigthchild );
}
}

// Output tree LDR Binary Tree sequential Traversal
Void preorder_ldr (btree & root)
{
If (root! = NULL)
{
Preorder_ldr (root-> leftchild );
Cout <root-> data;
Preorder_ldr (root-> rigthchild );
}
}
// Output tree LRD Binary Tree post-sequential Traversal
Void preorder_lrd (btree & root)
{
If (root! = NULL)
{
Preorder_lrd (root-> leftchild );
Preorder_lrd (root-> rigthchild );
Cout <root-> data;
}
}
// Calculate the number of nodes in a binary tree
Int trav_leaf (btree root)
{
If (root! = NULL)
{
If (root-> leftchild = NULL & root-> rigthchild = NULL) Count ++;
Trav_leaf (root-> leftchild );
Trav_leaf (root-> rigthchild );
}
Else
Return count;
}

Int posttreedepth (btree BT)
{
If (BT! = NULL)
{
H1 = posttreedepth (BT-> leftchild );
H2 = posttreedepth (BT-> rigthchild );
High = (h1> = h2 )? H1: H2;
Return (high + 1 );
}
Else return 0;

}

 

// Main Function

Int main ()
{

Btree Bt;
 
Cout <"OK! "<Endl;
Create (BT );

Cout <"DLR :";
Preorder_dlr (BT );
Cout <Endl;
/*
Cout <"LDR :";
Preorder_ldr (BT );
Cout <Endl;

Cout <"LRD :";
Preorder_lrd (BT );
Cout <Endl;
*/
Int getcout = trav_leaf (BT );
Cout <"Number of leaf nodes:" <getcout <Endl;
Cout <"tree depth:" <posttreedepth (BT) <Endl;
Cout <"high ="

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.