二叉树

来源:互联网 发布:2015年流行的网络歌曲 编辑:程序博客网 时间:2024/05/21 11:33

// 二叉树.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "BinaryTree.h"


int main()
{

 BinaryTree<char> obj('#');
 cout<<"请选择输入方式:/n";
 cout<<"/t1. 动手以前序遍历的方式输入二叉树/n";
 cout<<"/t2.电脑自动以字符串的形式输入已选定的二叉树/n";
    int x;
 cin>>x;
 if(x==1)
 {
  cout<<"/n以前序遍历的方式输入二叉树:"<<endl;
  obj.CreateTree();
 }
 else
 {//以字符串的方式输入二叉树
  cout<<"/n以字符串的方式输入二叉树'ABC##DE#G##F###'/n"<<endl;
  string str="ABC##DE#G##F###";
  obj.CreateBinTree(str);
 }
 //遍历
 obj.preOrder();
 cout<<endl;
 obj.inOrder();
 cout<<endl;
 obj.postOrder();
 cout<<endl;
 //高度 结点数
 cout<<"二叉树的高度 : "<<obj.Height()<<'/n'<<endl;
 cout<<"二叉树的结点数 : "<<obj.Size()<<'/n'<<endl;
 //
 cout<<obj<<endl;
 return 0;
}
工程:

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__3016BACF_3C87_432A_A8C4_AFFB7A642086__INCLUDED_)
#define AFX_STDAFX_H__3016BACF_3C87_432A_A8C4_AFFB7A642086__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers

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

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__3016BACF_3C87_432A_A8C4_AFFB7A642086__INCLUDED_)

 

// stdafx.cpp : source file that includes just the standard includes
// 二叉树.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

 

 

 

// BinTreeNode.h: interface for the BinTreeNode class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BINTREENODE_H__BF7A94D5_4FD7_4BD6_B1F1_2BA5F91D1A94__INCLUDED_)
#define AFX_BINTREENODE_H__BF7A94D5_4FD7_4BD6_B1F1_2BA5F91D1A94__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template<class T>
class BinTreeNode 
{
public:
 BinTreeNode():leftChild(NULL),rightChild(NULL){ }
 BinTreeNode(T x,BinTreeNode<T> *r1=NULL,BinTreeNode<T> *r2=NULL):data(x),leftChild(r1),rightChild(r2){ };
 ~BinTreeNode();
public:
 T data;
 BinTreeNode<T> *leftChild, *rightChild;

};
#include "BinTreeNode.cpp"
#endif // !defined(AFX_BINTREENODE_H__BF7A94D5_4FD7_4BD6_B1F1_2BA5F91D1A94__INCLUDED_)

// BinTreeNode.cpp: implementation of the BinTreeNode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BinTreeNode.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


template<class T>
BinTreeNode<T>::~BinTreeNode()
{

 

}

 

// BinaryTree.h: interface for the BinaryTree class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BINARYTREE_H__568645D2_F715_4CE0_A391_69B0ABEFE959__INCLUDED_)
#define AFX_BINARYTREE_H__568645D2_F715_4CE0_A391_69B0ABEFE959__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "BinTreeNode.h"

template<class T>
class BinaryTree 
{
public:
 BinaryTree();//不带构造函数
    BinaryTree(T value);//带一个参数的构造函数
 BinaryTree(BinaryTree<T> &p);//复制构造函数
 ~BinaryTree();//析构函数
 //建立二叉树
    void CreateTree();
 void CreateBinTree(const std::string &String);
    //返回结点
 BinTreeNode<T> *Parent(BinTreeNode<T> *current);//返回父结点
 BinTreeNode<T> *LeftChild(BinTreeNode<T> *current);//返回左子女
    BinTreeNode<T> *RightChild(BinTreeNode<T> *current);//返回右子女
    //
 int Height();//返回树的高度
 int Size();//返回结点数
 BinTreeNode<T> *getRoot();//取根
    //遍历操作
 void preOrder();//void preOrder(void(*visit)(BinTreeNode<T> *p));//前序遍历
 void inOrder(); //void inOrder(void(*visit)(BinTreeNode<T> *p));//中序遍历
 void postOrder();//void postOrder(void(*visit)(BinTreeNode<T> *p));//后序遍历
    void levelOrder();//void levelOrder(void(*visit)(BinTreeNode<T> *p));//层次遍历
    //
 int Insert(T item);//插入新的元素
 void Find(T item);//搜索
 bool IsEmpty();//判断二叉树是否为空
 void Traverse(BinTreeNode<T> *subTree,ostream &out);
protected:
 BinTreeNode<T> *root;//二叉树根的结点
 T StopValue;//数据输入停止数据
    //
 void CreateBinTree(const char* &String,BinTreeNode<T> *&subTree);
 //void CreateBinTree(istream &in,BinTreeNode<T> *&subTree);//从文件读入数据进行建树
 void CreateTree(BinTreeNode<T> *&subTree);
    //
 bool Insert(BinTreeNode<T> *&subTree,T &x);//插入
 void destroy(BinTreeNode<T> *&subTree);//删除
    bool Find(BinTreeNode<T> *subTree,T &x);//查找
 BinTreeNode<T> *Copy(BinTreeNode<T> *orignode);//复制   **前序遍历的应用**
    //
 BinTreeNode<T> *Parent(BinTreeNode<T> *subTree,BinTreeNode<T> *current);//返回父结点
 int Height(BinTreeNode<T> *subTree);//返回树的高度    **后序遍历的应用**
 int Size(BinTreeNode<T> *subTree);//返回结点数   **后序遍历的应用**
    //
 void preOrder( BinTreeNode<T>* current);//void preOrder(BinTreeNode<T> &subTree,void(*visit)(BinTreeNode<T> *p));//前序遍历
    void inOrder( BinTreeNode<T>* current);//void inOrder(BinTreeNode<T> &subTree,void(*visit)(BinTreeNode<T> *p));//中序遍历
    void postOrder( BinTreeNode<T>* current);//void postOrder(BinTreeNode<T> &subTree,void(*visit)(BinTreeNode<T> *p));//后序遍历     
 //
 //friend istream& operator >>(istream &in,BinaryTree<T>  &Tree);//重载输入操作
 friend ostream& operator <<(ostream &out,BinaryTree<T>  &Tree);//重载输出操作

};
#include "BinaryTree.cpp"
#endif // !defined(AFX_BINARYTREE_H__568645D2_F715_4CE0_A391_69B0ABEFE959__INCLUDED_)

 

// BinaryTree.cpp: implementation of the BinaryTree class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BinaryTree.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
//构造函数
//////////////////////////////////////////////////////////////////////////
template<class T>
BinaryTree<T>::BinaryTree()
{
 root=NULL;
}
//含参数的构造函数
template<class T>
BinaryTree<T>::BinaryTree(T value)
{
 StopValue=value;
 root=NULL;
}
//复制构造函数
template<class T>
BinaryTree<T>::BinaryTree(BinaryTree<T> &p)
{
 root=Copy(p.root);
}
//私有复制函数
template<class T>
BinTreeNode<T>*  BinaryTree<T>::Copy(BinTreeNode<T> *orignode)
{
 if (orignode!=NULL)
 {
  BinTreeNode<T> *temp=new BinTreeNode<T>;
  temp->data=orignode->data;
  temp->leftChild=Copy(orignode->leftChild);
  temp->rightChild=Copy(orignode->rightChild);
  return temp;
 }
 else  return NULL;
}
//////////////////////////////////////////////////////////////////////////
//析构函数
//////////////////////////////////////////////////////////////////////////
template<class T>
BinaryTree<T>::~BinaryTree()
{
 destroy(root);
}
//////////////////////////////////////////////////////////////////////////
//置空函数
//////////////////////////////////////////////////////////////////////////
template<class T>
bool BinaryTree<T>::IsEmpty()
{
 return(root==NULL) ? true:false;
}
//////////////////////////////////////////////////////////////////////////
//返回父结点
//////////////////////////////////////////////////////////////////////////
//公有
template<class T>
BinTreeNode<T>*  BinaryTree<T>::Parent(BinTreeNode<T> *current)
{
 return(root==NULL||root==current)?NULL:Parent(root,current);
}
//私有
template<class T>
BinTreeNode<T>*  BinaryTree<T>::Parent(BinTreeNode<T> *subTree,BinTreeNode<T> *current)
{
 if(subTree==NULL) return NULL;//父结点为空,返回空
 if(subTree->leftChild==current||subTree->rightChild==current)  return subTree;//找到返回父结点
 BinTreeNode<T> *p;
 if((p=Parent(subTree->leftChild,current))!=NULL)  return p;//递归在左子树中搜索
 else return Parent(subTree->rightChild,current);//递归在右子树中搜索
}
//////////////////////////////////////////////////////////////////////////
//返回左子女
//////////////////////////////////////////////////////////////////////////
template<class T>
BinTreeNode<T>*  BinaryTree<T>::LeftChild(BinTreeNode<T> *current)
{
 return(root==NULL||root==current) ? NULL:Parent(root,current);
}
//////////////////////////////////////////////////////////////////////////
//返回右子女
//////////////////////////////////////////////////////////////////////////
template<class T>
BinTreeNode<T>*  BinaryTree<T>::RightChild(BinTreeNode<T> *current)
{
 return(current!=NULL) ? current->rightChild: NULL;
}
//////////////////////////////////////////////////////////////////////////
//返回树的高度
//////////////////////////////////////////////////////////////////////////
//公有
template<class T>
int BinaryTree<T>::Height()
{
 return Height(root);
}
//私有
template<class T>
int BinaryTree<T>::Height(BinTreeNode<T> *subTree)
{
 int n=0,i=0,j=0;
 if (subTree!=NULL)
 {
  i=Height(subTree->leftChild);
  j=Height(subTree->rightChild);
  if(i>j)  n=i+1;
  else     n=j+1;

 }
 else n=0;
 return n;
}
//////////////////////////////////////////////////////////////////////////
//计算结点数
//////////////////////////////////////////////////////////////////////////
//公有
template<class T>
int BinaryTree<T>::Size()
{
 return Size(root);
}
//私有
template<class T>
int BinaryTree<T>::Size(BinTreeNode<T> *subTree)
{
 int n;
 if(subTree!=NULL) n=1+Size(subTree->leftChild)+Size(subTree->rightChild);//计算结点数
 else              n=0;//若为空树,则结点树等于0
 return n;
}
//////////////////////////////////////////////////////////////////////////
//取根
//////////////////////////////////////////////////////////////////////////
template<class T>
BinTreeNode<T>*  BinaryTree<T>::getRoot()
{
 return root;
}
//////////////////////////////////////////////////////////////////////////
//前序遍历操作
//////////////////////////////////////////////////////////////////////////
//公有
template <class Type>
void BinaryTree<Type>::preOrder() 
{
 cout<<"前序遍历树:/n";
 preOrder (root);
 cout<<endl;   
}
//私有
template <class Type>
void BinaryTree<Type>::preOrder( BinTreeNode<Type>* current)
{
 if(current != NULL)
 {
  cout<<current->data;
  preOrder(current->leftChild);
  preOrder(current->rightChild); 
 }
 else  cout <<"#"; 
 
}
//////////////////////////////////////////////////////////////////////////
//中序遍历操作
//////////////////////////////////////////////////////////////////////////
//公有
template <class T>
void BinaryTree<T>::inOrder()
{
 cout<<"中序遍历树:/n";
 inOrder( root );
 cout<<endl;      

//私有
template <class T>
void BinaryTree<T>::inOrder( BinTreeNode<T>* current)
{
 if(current != NULL)
 {
  inOrder(current->leftChild);
  cout<<current->data;    
  inOrder(current->rightChild);    
 } 
 else  cout<<"#"; 
}
//////////////////////////////////////////////////////////////////////////
//后序遍历操作
//////////////////////////////////////////////////////////////////////////
//公有
template <class T>
void BinaryTree<T>::postOrder()
{
 cout<<"后序遍历树:/n";
 postOrder(root);
    cout<<endl;   
}
//私有
template <class T>
void BinaryTree<T>::postOrder( BinTreeNode<T>* current)
{
 if(current != NULL )
 {
  postOrder(current->leftChild); 
  postOrder(current->rightChild);
  cout<<current->data; 
 }
 else   cout<<"#"; 
}
//////////////////////////////////////////////////////////////////////////
//层次序遍历
//////////////////////////////////////////////////////////////////////////
template<class T>
void BinaryTree<T>::levelOrder()
{
 
}
//////////////////////////////////////////////////////////////////////////
//插入新的元素
//////////////////////////////////////////////////////////////////////////
//公有
template<class T>
int BinaryTree<T>::Insert(T item)
{
 return 0;
}
//私有
template<class T>
bool BinaryTree<T>::Insert(BinTreeNode<T> *&subTree,T &x)
{
 return true;
}
//////////////////////////////////////////////////////////////////////////
//搜索
//////////////////////////////////////////////////////////////////////////
//公有
template<class T>
void BinaryTree<T>::Find(T item)
{
    Find(root,item);
}
//私有
template<class T>
bool BinaryTree<T>::Find(BinTreeNode<T> *subTree,T &x)
{
 return true;
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
template<class T>
void BinaryTree<T>::CreateBinTree(const std::string &String)
{
    const char* stop=String.c_str();
 if(*stop!='#')//以#开头表示字符串不是先序表达式
  CreateBinTree(stop,root);  
 else
  cout << "输入的字符串错误!/n"; 
}
template<class T>
void BinaryTree<T>::CreateBinTree(const char* &String,BinTreeNode<T> *&subTree)
{
 if(*String!=StopValue)
 {
  subTree=new BinTreeNode<T>(*String);  
  if(subTree==NULL){ cerr<<"存储分配失败!"<<endl; exit(1); }
  CreateBinTree(++String,subTree->leftChild);
  CreateBinTree(++String,subTree->rightChild);
 }
 else  subTree=NULL;
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
template<class T>
void BinaryTree<T>::CreateTree()
{
 CreateTree(root);
}
//
template<class T>
void BinaryTree<T>::CreateTree(BinTreeNode<T> *&subTree)
{
 T item;
 cin>>item;
    if(item!='0')
 {
  if (item!=StopValue)
  {
   subTree=new BinTreeNode<T>(item);
   if(subTree==NULL) { cerr<<"存储分配失败!"<<endl; exit(1); }
   CreateTree(subTree->leftChild);
   CreateTree(subTree->rightChild);
  }
  else subTree=NULL;
 }
}
//////////////////////////////////////////////////////////////////////////
//删除
//////////////////////////////////////////////////////////////////////////
template<class T>
void BinaryTree<T>::destroy(BinTreeNode<T> *&subTree)
{//若指针subTree不为空,则递归调用本身进行删除根为subTree的树
 if (subTree!=NULL)
 {
  destroy(subTree->leftChild);//递归进行删除subTree的左子树
  destroy(subTree->rightChild);//递归进行删除subTree的右子树
  delete subTree;//递归进行删除subTree
 }
}
//////////////////////////////////////////////////////////////////////////
//利用前序遍历的方法输出二叉树
//////////////////////////////////////////////////////////////////////////
template<class T>
void BinaryTree<T>::Traverse(BinTreeNode<T> *subTree,ostream &out)//
{

 if (subTree!=NULL)//subTree不为空的时候
 {
  out<<subTree->data<<"  ";//输出数据
  Traverse(subTree->leftChild,out);//递归调用Traverse函数,并输出左子树
        Traverse(subTree->rightChild,out);//递归调用Traverse函数,并输出右子树
 }
}
//////////////////////////////////////////////////////////////////////////
//重载输入操作
//////////////////////////////////////////////////////////////////////////
// template<class T>
// istream& operator >>(istream &in,BinaryTree<T> &Tree)
// {//输入数据建立一棵二叉树Tree
//     CreateBinTree(in,Tree.root);
//  return in;
// }
//////////////////////////////////////////////////////////////////////////
//重载输出操作
//////////////////////////////////////////////////////////////////////////
template<class T>
ostream& operator <<(ostream &out,BinaryTree<T>  &Tree)
{//输出一棵二叉树Tree
 out<<"利用二叉树的前序遍历进行输出树:/n/n";
 BinTreeNode<T> *sub_Tree=Tree.getRoot();
 Tree.Traverse(sub_Tree,out);//输出
 out<<endl;
 return out;
 }

原创粉丝点击