用c++语言实现的二叉树基本操作,包括二叉树的创建、二叉树的遍历(包括前序、中序、后序递归和非递归算法)、求二叉树高度,计数叶子节点数、计数度为1的节点数等基本操作。
IDE:vs2013
具体实现代码如下:
#include "stdafx.h"
#include <malloc.h>
#include <stack>
#include <iostream>
#define MAXSIZE 100
using namespace std;
typedef struct node //二叉树结构体
{
int data;
struct node *lchild;
struct node *rchild;
}Bnode,*BTree;
BTree CreateBinaryTree(BTree &tree){ //创建二叉树
int inputdata;
cin >> inputdata;
if (-1 == inputdata)
{
tree = NULL;
}
else
{
if (!(tree = (Bnode*)malloc(sizeof(Bnode))))
{
cout<<"ERROR";
}
tree->data = inputdata;
tree->lchild=CreateBinaryTree(tree->lchild);
tree->rchild=CreateBinaryTree(tree->rchild);
}
return tree;
}
void preorderTraverse(BTree tree