Three traversal applications of binary tree (expression, depth, number of leaves, node count, build of binary tree, copy)

Source: Internet
Author: User

Representation of an expression

Two-fork Tree expression: a+b* (c-d)-e/f

If the binary tree is traversed sequentially, the nodes are arranged in the order of the access nodes, and the its first sequence is listed as: (Polish, prefix expression)-+a*b-cd/ef

Traversing in the middle order, where the order is listed as: a+b*c-d-e/f (infix expression)

Post-order traversal, followed by sequence: abcd-*+ef/-(inverse polish, suffix expression)

Note: People like the infix form of arithmetic expressions, for computers, using suffixes easy to evaluate

Querying a node in a binary tree

Query traversal using the first-order traversal algorithm

//if the binary tree exists with the same element as X, then p points to the node and returns OK, otherwise it returns FALSE .BOOLPreorder (Bitree T,intX, Binode *p) {    //If the binary tree is not empty, start querying the node.    if(T) {//If the root node is the target junction.        if(T->data = =x) {//p points to the node and returns True.p =T; return true; }        Else        {            //recursive invocation, which is the first order traversal            if(Preorder (t->lchild, X, p)) {               return true; }            Else            {                return(Preorder (t->rchild, X, p))            ; }        }//End of Else}//End of If    Else    {        return false; }}

Statistics on the number of leaf nodes in binary tree

Or the process of first order traversal

//Statistics of the number of the final nodes in the binary tree, that is, the number of leaf nodesvoidCountleaf (Bitree T,int*count) {    //If the tree is not empty    if(T! =NULL) {        //if the left and right subtrees of the tree are empty, then the leaf node is +1        if((! T->lchild) && (! T->rchild)) {            //count the leaf nodes.count++; }        //Otherwise, continue to traverse recursivelyCountleaf (t->Lchild, Count); Countleaf (T-Rchild, Count); } //if}

Statistics of the number of nodes in a binary tree

//returns the number of nodes in the two-fork tree that the pointer T refers to.//or a pre-order traversalintCount (Bitree T) {//if T is empty    if(!T) {//the description is empty tree and returns 0 nodes.        return 0; }    //if the left and right sub-tree of T is empty, it means that there is only one node, the root.    if(! T->lchild &&! T->rchild) {       return 1; }    Else{        //Otherwise, recursive traversal        intm = Count (t->lchild); intn = Count (t->rchild); return(m + n +1); } //Else}

Finding the depth of a binary tree (post-order traversal)

//To find the depth of a binary tree, the use of subsequent traversalintDepth (Bitree T) {intdepth; intDepthleft; intDepthright; //If the binary tree is empty    if(!T) {depth=0; }    Else{depthleft= Depth (t->lchild); Depthright= Depth (t->rchild); Depth=1+ (Depthleft > depthright?)depthleft:depthright); }        returndepth;}

Copy the binary tree (also the post-order traversal), its basic operation is: Generate a node.

//Generate a binary tree node (its data field is item, the left pointer field is LPTR, and the right pointer field is Rptr) .Binode * Gettreenode (intItem, Binode *lptr, Binode *rptr)    {Bitree T; //If the new node fails    if(! (T =NewBinode)) {        //ExitExit1); }    //new node SuccessT->data =item; //T->lchild =lptr; T->rchild =Rptr; //returns a pointer to a newly created node .    returnT;} Binode* Copytree (Binode *T) {Binode*NewT; Binode*newlptr; Binode*newrptr; //If the source tree is empty    if(!T) {//return Empty       returnNULL; }    //if the left sub-tree of the root is not empty    if(t->lchild) {Newlptr= Copytree (T->lchild);//Copy left dial hand tree    }    Else    {        //left dial hand tree is nullNewlptr =NULL; }    //if the right sub-tree of the root is not empty    if(t->rchild) {Newrptr= Copytree (T->rchild);//Copy Right sub-tree    }    Else    {        //Otherwise, the right subtree is nullNewrptr =NULL; }    //The new node of a binary tree, the process of subsequent traversal.NewT = Gettreenode (t->data, newlptr, newrptr); returnNewT;}

A two-tree storage structure is built, and a two-fork tree is built recursively.

The order of the input node values must correspond to the order of the first order traversal of the two-fork tree nodes. It is also agreed to end the recursion by using values that are not likely to appear in the input sequence as values for empty nodes. For example, use "@" or "1" to denote a sequence of characters or positive integer sequences of empty nodes.

The sequential traversal order of the two-fork tree is

A B C @ @ D E @ G @ @ F @ @ @

//Recursive establishment of two-fork treeBOOLCreatebitree (Bitree T) {Charch; //the value of the input nodescanf (&ch); //If you enter a null character    if(ch = =' ')    {        //represents an empty node.T =NULL; }    Else{T= (binode*) malloc (sizeof(Binode)); //If the new node fails        if(!T) {exit (1); }        //success. Establishing a two-fork tree in the order of first order traversalT->data =ch; Createbitree (T-lchild); Createbitree (T-rchild); }        return true;}

Three traversal applications of binary tree (expression, depth, number of leaves, node count, build of binary tree, copy)

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.