Create a binary tree (recursion)

Source: Internet
Author: User

Stack is required for non-recursive Binary Tree creation, which is indeed too annoying. Here, only the method for recursively creating a binary tree is provided.

[Cpp]
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Typedef struct BiTreeNode
{
Char data;
BiTreeNode * left;
BiTreeNode * right;
} BiTreeNode, * BiTree;
 
// Both are created in the first order, that is, input in the first order, and input in the null node '#'
BiTreeNode * Create ()
{
Char ch;
Cin> ch;
BiTreeNode * root;
 
If (ch = '#')
Return NULL;
Else
{
Root = new BiTreeNode;
Root-> data = ch;
Root-> left = Create ();
Root-> right = Create ();
Return root;
}
}
 
Void CreateBiTree (BiTree & t) // required &
{
Char ch;
Cin> ch;
If (ch = '#')
T = NULL;
Else
{
T = new BiTreeNode;
If (! T)
Return;
T-> data = ch;
CreateBiTree (t-> left );
CreateBiTree (t-> right );
}
}
 
// First-order traversal
Void PreOrderTraverse (BiTreeNode * t)
{
If (t)
{
Cout <t-> data <"";
PreOrderTraverse (t-> left );
PreOrderTraverse (t-> right );
}

}
Int _ tmain (int argc, _ TCHAR * argv [])
{
BiTree t = NULL;
// T = Create (); // method 1
CreateBiTree (t); // method 2
PreOrderTraverse (t );
Cout <endl;
Return 0;
}
The preceding two methods are provided to create a binary tree recursively. The input must be a Complete Binary Tree.


Here we add a method to create a binary tree, as shown in the following code:
[Cpp]
// Node. cpp: defines the entry point of the console application.
//
 
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
 
Struct node
{
Char value;
Node * left;
Node * right;
Node (char v, node * l = NULL, node * r = NULL): value (v), left (l), right (r)
{
}
};
 
Void preordertraverse (node * t)
{
If (t)
{
Cout <t-> value <"";
Preordertraverse (t-> left );
Preordertraverse (t-> right );
}

}
 
// Recursive deletion: delete a new node to prevent memory leakage
Void deletetree (node * t)
{
If (t-> left = NULL & t-> right = NULL)
{
Delete t;
T = NULL;
}
If (! T)
Return;
Else
{
If (t-> left)
Deletetree (t-> left );
If (t-> right)
Deletetree (t-> right );
If (t-> left = NULL & t-> right = NULL)
{
Delete t;
T = NULL;
}
}
}
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Node * t1 = new node ('7 ');
Node * t2 = new node ('8 ');
Node * t3 = new node ('5', t1, t2 );
Node * t4 = new node ('4 ');
Node * t5 = new node ('6 ');
Node * t6 = new node ('2', t4, t3 );
Node * t7 = new node ('3', NULL, t5 );
Node * t8 = new node ('1', t6, t7 );
 
Preordertraverse (t8 );

Deletetree (t8 );
Www.2cto.com
Cout <endl;
Return 0;
}

This method is convenient to process the relationship between nodes through the constructor method.

Related Article

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.