Binary Tree, sorting binary tree (continued)

Source: Internet
Author: User

PreviousArticleI talked about some knowledge about binary trees. This article introduces several common Binary Trees.Algorithm

Calculate the height of the tree: the height of the binary tree is the height of the Left subtree and the maximum value of the right subtree plus 1, so we can quickly obtain the pseudo algorithm implemented based on Recursive definitions.Code

 
Int blgtreedepth (blgtree & BT) {// If the fruit tree is empty, return 0 end recursion if (null = BT) {return 0 ;} else {int depth1 = blgtreedepth (BT-> lchild); // The depth of the Left subtree int depth1 = blgtreedepth (BT-> rchild ); // right subtree depth if (depth1> dep22.) {return depth1 + 1;} else {return dep22. + 1 ;}}}

Count the number of leaf nodes in the tree: A leaf node is a node with no child nodes. count the number of leaf nodes, you can determine whether the child node of the node is empty during traversal. The Code is as follows:

 
Int leafcount (blgtree & BT) {int nleafcount = 0; // Number of leaf nodes if (BT! = NULL) {leafcount (BT-> lchild); leafcount (BT-> rchild); If (BT-> lchild = NULL & BT-> rchild = NULL) {nleafcount ++ ;}} return nleafcount ;}

Hierarchy traversal of a tree: Hierarchy traversal of a tree refers to traversing from the root node, and then each layer is traversed from left to right. This completes the hierarchy traversal, queue is used during traversal. The specific operations are as follows:

1. Queue Q Initialization;

2.If the binary tree is not empty, add the root pointer to the queue;

3.Loop until queueQNull

3.1 q =QueueQThe first element of the team leaves the team;

3.2Access NodeQData domains;

3.3If the nodeQIf the left child exists, the left child is directed to the queue;

3.4If the nodeQIf the right child exists, the right child enters the queue;

The Code is as follows:

 
Void levelorder (blgtree & BT) {deque <blgnode *> nodequeue; If (BT! = NULL) {nodequeue. push_back (BT); While (! Nodequeue. empty () {blgnode * queuehead = nodequeue. at (0); printf ("% d \ t", queuehead-> data); nodequeue. pop_front (); If (queuehead-> lchild) {nodequeue. push_back (queuehead-> lchild);} If (queuehead-> rchild) {nodequeue. push_back (queuehead-> rchild );}}}}

The above are some simple algorithms. I hope you can add them!

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.