# 二叉樹

## 算法

1.首先創建一個新節點

2.以root身份插入第一個值。
3.如果要在root的左側插入下一個值，請以root-> left插入，或者如果要向右插入，請以root-> right插入。
4.重複步驟3，將節點插入樹的右位置。 例如，要將節點插入到根的左側，請以root-> left-> left插入。

## C ++程序

```#include <stdlib.h>
#include <stdio.h>

struct node
{
char data;
struct node *left;
struct node *right;
};
struct node* newNode(char data)
{
// Allocate memory for new node
struct node* node = (struct node*)malloc(sizeof(struct node));

// Assign data to this node
node->data = data;

// Initialize left and right children as NULL
node->left = NULL;
node->right = NULL;
return(node);
}
void prinTree(struct node* node)	//Using PreOrder Traversal for Printing a Tree
{
if(node == NULL)
return;
printf("%c\n",node->data );
prinTree(node->left);
prinTree(node->right);

}

int main()
{				//Creating a Tree with nodes A, B, C, D, E, F
struct node *root = newNode('A'); // Creating a root with left and right pointers Null

root->left	 = newNode('B');    // Creating a left node to A
root->right	 = newNode('C');	// Creating a right node to A

root->left->left = newNode('D');    //Creating a left left node to A
root->left->right = newNode('E');	//Creating a left right node to A
root->right->left = newNode('F');	//Creating a right left node to A
prinTree(root);  //It will print the Tree
return 0;
}```