Basic operations on Binary Trees

Source: Internet
Author: User

Due to job practice requirements, the basic operations of a tree are written, including first, middle, back-order recursion and non-recursion, calculation height, calculation of the number of left subtree, no other use, alert yourself ..
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <stack>
Using namespace std;
 
// Tree node
Typedef struct TreeNode {
Char data;
Struct TreeNode * lChild;
Struct TreeNode * rChild;
} Node;
 
Int n;
Stack <Node *> s;
 
// Create tree in the first order
Void createTree (Node ** t, char * c)
{
N ++;
If (n> strlen (c)-1) return;
If (c [n] = '0') * t = NULL;
Else
{
(* T) = (Node *) malloc (sizeof (Node ));
(* T)-> data = c [n];
CreateTree (& (* t)-> lChild, c );
CreateTree (& (* t)-> rChild, c );
}
}
// First-order recursive access
Void preVisit (Node * t)
{
If (t)
{
Printf ("% c", t-> data );
PreVisit (t-> lChild );
PreVisit (t-> rChild );
}
}
// Recursive sequential access
Void midVisit (Node * t)
{
If (t)
{
MidVisit (t-> lChild );
Printf ("% c", t-> data );
MidVisit (t-> rChild );
}
}
// Recursive post-Order Access
Void lastVisit (Node * t)
{
If (t)
{
LastVisit (t-> lChild );
LastVisit (t-> rChild );
Printf ("% c", t-> data );
}
}
 
 
// Non-recursive first-order access
Void uPre (Node * head)
{
While (! S. empty () s. pop ();
While (head |! S. empty ())
{
While (head)
{
Printf ("% c", head-> data );
S. push (head );
Head = head-> lChild;
}
If (! S. empty ())
{
Head = s. top ();
S. pop ();
Head = head-> rChild;
}
}
}
 
// Non-recursive sequential access
Void uMid (Node * head)
{
While (! S. empty () s. pop ();
While (head |! S. empty ())
{
While (head)
{
S. push (head );
Head = head-> lChild;
}
If (! S. empty ())
{
Head = s. top ();
Printf ("% c", head-> data );
S. pop ();
Head = head-> rChild;
}
}
 
}
// Non-recursive post-Order Access
Void uLast (Node * head)
{
Node * p = head;
Head = NULL;
While (! S. empty () s. pop ();
Do
{
While (p)
{
S. push (p );
P = p-> lChild;
}
P = s. top ();
P = p-> rChild;
If (head = p | p = NULL)
{
Head = s. top ();
S. pop ();
Printf ("% c", head-> data );
P = NULL;
}
} While (! S. empty ());
}
// Computing height
Int calH (Node * head)
{
If (! Head) return 0;
Int left = calH (head-> lChild );
Int right = calH (head-> rChild );
Int h;
If (left> right ){
H = left + 1;
}
Else
{
H = right + 1;
}
Return h;
}
 
// Calculate the number of left sub-Leaves
Int calLeftChild (Node * t)
{
If (! T) return 0;
Else if (t-> lChild &&! T-> lChild &&! T-> lChild-> rChild) return 1;
Else return calLeftChild (t-> lChild) + calLeftChild (t-> rChild );
}
 
Int main ()
{
Char a [1000];
Int c;
Scanf ("% d", & c );
While (c --)
{
N =-1;
Scanf ("% s", );
Node * head;
CreateTree (& head, );
PreVisit (head );
Putchar ('\ n ');
MidVisit (head );
Putchar ('\ n ');
LastVisit (head );
Putchar ('\ n ');
Printf ("non-recursive First Order \ n ");
UPre (head );
Putchar ('\ n ');
Printf ("\ n non-recursive Middle Order \ n ");
UMid (head );
Putchar ('\ n ');
Printf ("\ n non-recursive post-order \ n ");
ULast (head );
Putchar ('\ n ');
Printf ("\ n % d \ n", calH (head ));
Printf ("% d \ n", calLeftChild (head ));
}
Return 0;
}

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.