Binary Tree judgment (full Binary Tree, full Binary Tree, similar, symmetric homogeneous)

Source: Internet
Author: User

Ii. Determine whether a binary tree is a Complete Binary Tree

Judgment Method: 1: Find the node that does not exist in the first two sons by means of hierarchical traversal.

2: (1) if the left son does not exist and the right son does, the binary tree is not a Complete Binary Tree.

(2) If this node has a left son and no right son, or both sons do not exist, you only need to determine whether the node after this node is

Son: If there is no son, it is a full binary tree; otherwise, it is not a full binary tree;

Function:

Typedef struct node {char data; node * left; node * right;} btnode; int judge (btnode * root) {node * P; queue <btnode *> q; if (root! = NULL) {q. Push (Root); // Through layered traversal, find the node where the first two sons do not both exist while (! Q. Empty () {P = Q. Front (); q. Pop (); If (p-> left! = NULL & P-> right! = NULL) {q. push (p-> left); q. push (p-> right);} else break;} // Judge 1: If the left son does not exist, the right son exists, if (p-> left = NULL & P-> right! = NULL) return 0; // other else {// If the left son is not empty, the incoming queue if (p-> left! = NULL) Q. Push (p-> left); // if the remaining node in the queue has a son, it is not a complete binary tree while (! Q. Empty () {P = Q. Front (); q. Pop (); If (p-> left! = NULL | p-> right! = NULL) return 0 ;}} return 1 ;}

Iii. Determine whether two binary trees are similar:

First, a recursive definition of two trees is given: If two trees B1 and B2 are known to be empty, or both are not empty, and the left and right Subtrees of B1 are similar to those of B2, b1 is similar to B2. Determine whether the two given binary trees are similar.

The similarity between the two binary trees is that the element values of the two binary trees may be different, but they are in the same shape and have the same number of nodes;

// Determine whether the two binary trees are similar to int like (btnode * P1, btnode * P2) {If (p1 = NULL & p2 = NULL) return 1; else if (p1 = NULL | P2 = NULL) return 0; else return like (P1-> left, P2-> left) & like (P1-> right, p2-> right );}

Iv. Determine whether a binary tree is symmetric and homogeneous

Actually, it is recursive to determine whether the Left and Right Subtrees are similar. Therefore, it is OK to add a special decision on the basis of the similarity judgment ......

// Determine whether a tree is symmetric and homogeneous. Int symmtree (btnode * root) {If (root = NULL) // return 1 when the tree is empty; else return like (root-> left, root-> right )}

Complete code:

# Include <iostream> # include <queue> # include <stack> using namespace STD; typedef struct node {char data; node * left; node * right;} btnode; // create the binary tree void createbtree (btnode * & root, char STR []) {int I = 0, flag; btnode * P = NULL, * temp = NULL; stack <node *> S; while (STR [I]! = '\ 0') {Switch (STR [I]) {Case' (': Flag = 1; S. push (p); break; // indicates that the last created is the left son case ')': S. pop (); break; // The left and right sons of the top element of the stack are finished, and the output stack is case', ': Flag = 2; break; // The created result is the right son default: P = new node; P-> DATA = STR [I]; P-> left = p-> right = NULL; if (root = NULL) root = P; else {temp = S. top (); Switch (FLAG) {Case 1: temp-> left = P; break; Case 2: temp-> right = P; break ;}}} I ++ ;}/// determines whether it is a Complete Binary Tree int cmpbtree (btnode * root) {node * P; queue <btnode *> q; If (R Oot! = NULL) {q. Push (Root); // Through layered traversal, find the node where the first two sons do not both exist while (! Q. Empty () {P = Q. Front (); q. Pop (); If (p-> left! = NULL & P-> right! = NULL) {q. push (p-> left); q. push (p-> right);} else break;} // Judge 1: If the left son does not exist, the right son exists, if (p-> left = NULL & P-> right! = NULL) return 0; // other else {// If the left son is not empty, the incoming queue if (p-> left! = NULL) Q. Push (p-> left); // if the remaining node in the queue has a son, it is not a complete binary tree while (! Q. Empty () {P = Q. Front (); q. Pop (); If (p-> left! = NULL | p-> right! = NULL) return 0 ;}}return 1 ;}// determine whether the two binary trees are similar to int like (btnode * P1, btnode * P2) {If (p1 = NULL & p2 = NULL) return 1; else if (p1 = NULL | P2 = NULL) return 0; else return like (P1-> left, P2-> left) & like (P1-> right, P2-> right );} // determine whether a tree is symmetric and homogeneous. Int symmtree (btnode * root) {If (root = NULL) // return 1 when the tree is empty; else return like (root-> left, root-> right);} int main () {int N; char str1 [1000], str2 [1000]; btnode * root1 = NULL, * root2 = NULL; // determine whether the binary tree is completely CIN> str1; root1 = NULL; createbtree (root1, str1 ); if (cmpbtree (root1) cout <"yes. "<Endl; else cout <" no. "<Endl; // determine whether two binary trees are similar to CIN> str1> str2; root1 = root2 = NULL; createbtree (root1, str1); createbtree (root2, str2 ); if (like (root1, root2) cout <"yes. "<Endl; else cout <" no. "<Endl; // determine whether a binary tree is a similar homogeneous CIN> str1; root1 = NULL; createbtree (root1, str1); If (symmtree (root1 )) cout <"yes. "<Endl; else cout <" no. "<Endl ;}

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.