C++二叉树的建立、中序遍历的递归和非递归方法

来源:百度知道 编辑:UC知道 时间:2024/05/29 04:13:28
这是我写的程序 但是运行时总是出现错误,请高手帮忙改正,最好是在原程序的基础上改而且经过测试成功,可以的话顺便说下运行时的输入方法,谢谢
\BinaryTree.h\
#ifndef BinaryTree_H
#define BinaryTree_H

#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T>
class BinTreeNode{
public:
T data;
BinTreeNode<T> *leftChild,*rightChild;
};

template<class T>
class BinaryTree{
public:
BinaryTree():root(NULL){}
//~BinaryTree(){destroy(root);}
void createBinaryTree(BinTreeNode<T> *p);
void create(BinTreeNode<T> *p);
void inorderR();
void inorder(BinTreeNode<T> *p);
void destroy(BinTreeNode<T> *root);
protected:
BinTreeNode<T> *root;
};
#endif
\BinaryTree.cpp\
#include"BinaryTree.h"

template<class T>
void BinaryTree<T>::createBinaryTree(BinTreeNode<T> *root){
cre

如二叉树:
A
/ \
B C
则输入
AB##C##
即可创建。

if(x='#'){ // 改为 if (x != '#') {
p=new BinTreeNode<T>;
p->data=x;
create(p->leftChild);
create(root->rightChild);
}

可以看看这个:http://www.cppblog.com/liyuxia713/archive/2009/04/28/81285.html
这个二叉树的功能算很全面了~~
由给定的完全二叉树形式存储的数组(如"12345 6"),构造二叉树
提供:复制构造函数和赋值操作符重载,递归和非递归形式的中、前、后序遍历方法,求一个节点的父节点,左右兄弟结点的函数以及 求二叉树深度和结点个数的函数。