// Binary_tree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int ElemType;
//typedef char ElemType;
typedef struct TreeNode_s
{
ElemType Data;
struct TreeNode_s *pLeft;
struct TreeNode_s *pRight;
}*Ptree,Btree;
//二叉树的创建
Btree * CreateBiTree(Btree *root, ElemType data)
//void CreateBiTree(Btree *root, ElemType data)
{
Btree* pNewNode = NULL;
pNewNode = new Btree;
pNewNode->Data = data;
pNewNode->pLeft = NULL;
pNewNode->pRight = NULL;
if (root == NULL)
{
root = pNewNode;
return root;
}
else
{
Btree* pBackNode = NULL;
Btree* pCurrentNode = root;
while(pCurrentNode != NULL)
{
pBackNode = pCurrentNode;
//cout << pCurrentNode->Data<<endl;
if(pCurrentNode->Data > data) //查找左子树
{
pCurrentNode = pCurrentNode->pLeft;
}
else if (pCurrentNode->Data < data) //查找右子树
{
pCurrentNode = pCurrentNode->pRight;
}
else
{
delete pNewNode;
return pCurrentNode;
}
}
//cout << pBackNode->Data<<endl;
//比较插入数据与当前叶子节点,如果大于插入左子树,否则插入右子树
if (pBackNode->Data > data)
{