# 幾個練習
```
#include<stdlib.h>
#include<queue>
#include<stdio.h>
#include <assert.h>
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* left, *right;
/*節點建構函式*/
Node(int data)
{
this->data = data;
left = right = NULL;
}
};
/*定義節點指標*/
typedef struct Node* NodePointer;
int y = 1, x = -1;
#define Insert(r,n){\
if((r)==NULL){\
(r)=new Node(n);\
}else{\
insert((r),(n));\
}\
}
void insert(NodePointer root, int key)
{
if(root->data == key)
return ;
/*情況2.如果這個值小於樹根*/
if(key < root->data)
{
/*同時左節點為空*/
if(root->left == NULL)
{
y++;
/*插入節點*/
root->left = new Node(key);
return ;
}
else
{
insert(root->left, key);
}
}
/*情況3.如果這個值大於樹根*/
if(key > root->data)
{
/*同時左節點為空*/
if(root->right == NULL)
{
/*插入節點*/
y++;
root->right = new Node(key);
return ;
}
else
{
insert(root->right, key);
}
}
}
void printPreorder(struct Node* node)
{
if (node == NULL)
return;
x++;
cout << node->data << " ";
assert(y != x);
printPreorder(node);
printPreorder(node->right);
}
int main()
{
NodePointer root = NULL;
root = new Node(34);
Insert(root, 82)
Insert(root, 77);
Insert(root, 40);
Insert(root, 10);
Insert(root, 19);
Insert(root, 92);
Insert(root, 83);
/*前序*/
printPreorder(root);
return 0;
}
```