Data structure Practice topics (Binary tree section)

Source: Internet
Author: User

First, determine if binary tree A has the same subtree as B

#include <iostream> #include <stdlib.h> #include <malloc.h> #include <stack> using namespace std
;
    struct Binarytreenode {char value;
    Binarytreenode *left;
Binarytreenode *right;

};
    Class BinaryTree {public:binarytreenode* root;  BinaryTree () {}//constructor Binarytreenode * Createbinarytree ();

Build two-fork tree};
    Binarytreenode * Binarytree::createbinarytree ()//achievement function {Binarytreenode *t;
    char data;
    CIN >> data;
    if (data = = ' # ') T = NULL;
        else {T = new Binarytreenode;
        T->value = data;
        T->left = Createbinarytree ();
    T->right = Createbinarytree ();
} return T; } bool Issametree (Binarytreenode *roott, Binarytreenode * ROOTB)//Determine if the tree is the same {if (Roott = = NULL && ROOTB = NU
    LL)//The child is NULL, true return else if ((Roott = = NULL && ROOTB! = null) | |
        (Roott = null && ROOTB = = null))
    return false; else if (Roott->value = = ROOTB->value)//If same, go to next layer comparison {return Issametree (Roott->left, Rootb->left) && Issametree (roott-&gt
    ; right, rootb->right);
} return false; } bool Issubtree (Binarytreenode *roota, Binarytreenode * ROOTB)//Determines if the subtree {if (Issametree (Roota))///If the two trees are the same, return T
    Rue return true;
    if (ROOTB = = null)//b is empty and returns true return true;
    if (Roota = = null)//a is empty, it returns false to return false; if (Roota->value = = rootb->value)//If same, go to next Level compare return Issubtree (Roota->left, rootb->left) | |
    Issubtree (Roota->right, rootb->right); Return Issubtree (Roota->left, ROOTB) | |
Issubtree (Roota->right, ROOTB);
    Similar only need a part of the same, so you can compare the left and right sub-tree parts, take both of the two} int main () {BinaryTree A, B;
    A.root=a.createbinarytree ();
    B.root = B.createbinarytree ();
    if (Issametree (A.root, b.root)) cout << "Two trees same \ n";
    if (Issubtree (A.root, b.root)) cout << "B is a subtree \ n";
return 0; }

Second, write an algorithm that links all the leaf nodes of the two-prong tree from left to right into a single-linked list

#include <iostream> #include <stdlib.h> #include <malloc.h> #include <stack> #include <queue
> Using namespace std;
    struct Binarytreenode {//node struct defines char value;
    Binarytreenode *left;
Binarytreenode *right;

};
    struct ListNode {//Linked list node defines char data;
ListNode *next;
};

ListNode *l = new ListNode;
    binarytreenode* Createtree ()//achievements {Binarytreenode * T;
    Char ch;
    CIN >> ch;
    if (ch = = ' # ') T = NULL;
        else {T = new Binarytreenode;
        T->value = ch;
        T->left = Createtree ();
    T->right = Createtree ();
} return T;  } void Creatlist (BINARYTREENODE*T,LISTNODE*&AMP;L)//Use the sequence traversal recursion to string the nodes with the list L {if (T! = NULL) {if (T->left = =  Null&&t->right = = null)//If the left and right subtree is empty, then the leaf node {listnode *temp = new ListNode;
            String up Temp->data = t->value;
            L->next = temp;    

        L = l->next;
   }     Creatlist (t->left,l);    Recursive left subtree creatlist (t->right,l);
Recursive right subtree} l->next = NULL; } int main () {//abd# #E # #CF # #G #//A//B C//D E F G binarytre
    Enode * A;
    A = Createtree ();
    ListNode *first = L;//first as the first address of L creatlist (a,l);
    First = first->next;
    for (; first!=null&&first->next!=null;)
        {if (first = = NULL) break;

        cout << first->data;
    First = first->next;
} return 0;
 }

Three, set a binary tree with a binary linked list, write an algorithm to determine whether the binary tree is a complete binary tree

#include <iostream> #include <stdlib.h> #include <malloc.h> #include <stack> #include <queue
> Using namespace std;
Enum Tag {L, R};
    struct Binarytreenode {char value;
    Binarytreenode *left;
Binarytreenode *right;

};

    Binarytreenode * Createbinarytree ()//achievements {Binarytreenode *t;
    char data;

    CIN >> data;
    if (data = = ' # ') T = NULL;
        else {T = new Binarytreenode;
        T->value = data;
        T->left = Createbinarytree ();
    T->right = Createbinarytree ();
} return T; } int Compbtnode (Binarytreenode *b) {queue<binarytreenode*> q;//defines a queue for layering judgment Binarytreenode * p=new Binar
    Ytreenode;                       int cm = 1;                       Cm=1 represents a binary tree for a complete binary tree int bj = 1;
        Bj=1 that all the nodes so far have left and right children if (b! = NULL) {Q.push (b);
            while (!q.empty ()) {p = Q.front ();
            Q.pop (); if (P->left = = NULL)//*p node no left child {bj = 0;
            if (p->right! = NULL)//No left child but has right child, does not meet complete binary tree nature cm = 0;
                The Else//*p node has left child {if (BJ = = 1)//node has left child.
                    {//Left child in Team Q.push (P->left);
                    if (p->right = = NULL)//*p has left child but no right child, then bj=0 BJ = 0;
                    else//*p have right child, then continue to judge {Q.push (p->right);//Right child into the team         }} else//bj=0: To date, there are no left or right children cm = 0;
    At this point the *p node has left child}} return cm;
} return 1;
    } int main () {Binarytreenode *t = NULL;
    A B C # # # D # # T = Createbinarytree ();
    if (Compbtnode (T)) cout << "Yes";
    else cout << "NO";
return 0;
 }

Iv. the width of the statistical binary tree

int Maxx = 0;//stores the maximum number of nodes
int wid[100] = {0};//stores the number of nodes per layer
int getwidth (binarytreenode*t)//recursive computation binary tree width
{

    int static floor = 1;
    if (T! = NULL)
    {
        if (floor = = 1)//When it is the root node
        {
            wid[floor]++;
            floor++;
            if (t->left) wid[floor]++; There is a left subtree, the value of the array plus 1
            if (t->right) wid[floor]++;//There is a right subtree, the value of the array plus 1
        }
        else
        {
            floor++;
            if (t->left) wid[floor]++; There is a left subtree, the value of the array plus 1
            if (t->right) wid[floor]++;//There is a right subtree, the value of the array plus 1
        } if
        (Wid[floor] > Maxx)//If it is greater than Maxx, Replace the value of Maxx
            Maxx = Wid[floor];
        Maxx=getwidth (t->left); 
        floor--;     Visited to reduce one
        maxx=getwidth (t->right);
    }
        Return Maxx;
}

Five, before the order of output of a binary tree all nodes of the data value and node level

//abd# #E # #CF # #G #//A//B C//D E F G int NF
Loor = 1;
        void GetNode (binarytreenode*t) {if (T! = NULL) {cout << t->value<< "";
        cout << Nfloor << Endl;
        nfloor++;  GetNode (T->left);
    Recursive search left subtree getnode (t->right);//recursive search right subtree nfloor--; }
}

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.