写于2016-06-18 16:21
#include<iostream>
using namespace std;
struct xuqi
{
string data;
xuqi*L,*R;
};
///先序创建二叉树//
void create( xuqi * &T )
{
string a;
cin>>a;
if(a.compare("#")==0)T=NULL;
else{
T=new xuqi;
T->data=a;
create(T->L);
create(T->R);
}
}
前序遍历//
void preorderTraversal(xuqi * & T)
{
if(T)
{cout<<"遍历到的数值为:"<<T->data<<endl;
preorderTraversal(T->L);
preorderTraversal(T->R);
}
}
///中序遍历二叉树///
void inorderTraversal(xuqi * & T)
{
if(T)
{inorderTraversal(T->L);
cout<<"遍历到的数值为:"<<T->data<<endl;
inorderTraversal(T->R);
}
}
//后序遍历//
void postorderTraversal(xuqi * & T)
{
if(T)
{ postorderTraversal(T->L);
postorderTraversal(T->R);
cout<<"遍历到的数值为:"<<T->data<<endl ;
}
}
/ 计算二叉树深度/
int depthOfBinaryTree( xuqi *&T)
{
if(T==NULL)
return 0;
int left=1;
int right=1;
left+=depthOfBinaryTree(T->L);
right+=depthOfBinaryTree(T->R);
cout<<"目前left和right分别为:"<<left<<" "<<right<<endl;
if(left>right)return left;
else return right;
}
//层次遍历/
int levelTraverse( xuqi *&T ,int c)
{
if(T==NULL)
{
cout<<"此节点为空"<<endl;
return 0;
}
else if(c==0)
{
cout<<T->data<<" ";
return 1;
}
return levelTraverse(T->L,c-1)+levelTraverse(T->R,c-1);
}
void levelchange(xuqi * &T)//修改层操作
{
int i=0;
while(true)
{
if( levelTraverse(T,i++)==0)break;
cout<<endl<<"第"<<i<<"层结束"<<endl;
}
}
int main()
{
xuqi* T;
create(T); //先序 构造二叉树
cout<<"先序遍历:"<<endl;
preorderTraversal(T);
cout<<"中先序遍历:"<<endl;
inorderTraversal(T);
cout<<"后序遍历:"<<endl;
postorderTraversal(T);
int c= depthOfBinaryTree(T);
cout<<"二叉树的深度为:"<<c<<endl;
cout<<"层次遍历:"<<endl;
levelchange(T);
return 0;
/*
测试数据:
12
9
3
#
#
10
#
#
15
13
#
#
18
17
#
#
21
#
#
*/
}
上面四图为:测试数据+三序遍历+二叉树深度+层次遍历
----------------------------------------------------------------------------------------分割线--------------------------------------------------------------------------------
/*
要求:二叉树题目:根据前序和中序遍历构建二叉树
*/
#include<iostream>
using namespace std;
struct xuqi {
char data;
xuqi * L,*R;
};
//根据先中序列 构建二叉树
void CreatBiTree(xuqi * &root,string preOrder,string inOrder)
{
if(preOrder.length()==0)
{
root=NULL;//
return;
}
char first=preOrder[0];
int index=inOrder.find(first);//找到该字符在中序串中的下标
string leftChildInOrder=inOrder.substr(0,index);
string rightChildInOrder=inOrder.substr(index+1);
int leftChildLength=leftChildInOrder.length();//获取左半部分长度
int rightChildLength= rightChildInOrder.length();//获取右半部分长度
string leftChildPreOrder=preOrder.substr(1,leftChildLength);
string rightChildPreOrder=preOrder.substr(leftChildLength+1);
root=new xuqi;
root->data=first;
cout<<" 遍历到的值"<<first<<" index:"<<index<<endl;
cout<<" leftChildInOrder and rightChildInOrder:"<<leftChildInOrder<<" and "<<rightChildInOrder<<endl;
cout<<" leftChildLength and rightChildLength:"<<leftChildLength<<" "<< rightChildLength<<endl;
cout<<" leftChildPreOrder and rightChildPreOrder:"<<leftChildPreOrder<<" and "<< rightChildPreOrder<<endl;
CreatBiTree(root->L,leftChildPreOrder,leftChildInOrder);
CreatBiTree(root->R,rightChildPreOrder,rightChildInOrder);
}
前序遍历//
void preorderTraversal(xuqi * T)
{
if(T)
{
cout<<T->data;//输出遍历到的值
preorderTraversal(T->L);
preorderTraversal(T->R);
}
}
int main()
{
string preOrderStr="ABCDEFGHIJKLMN";//先序序列
string InOrderStr ="CBEDAHGJIFKMNL";//中序序列
cout<<"先序序列串为:"<<preOrderStr<<endl<<"中序序列串为:"<< InOrderStr<<endl;
//创建二叉树
xuqi *root;
CreatBiTree(root,preOrderStr,InOrderStr);//构建二叉树
cout<<"先序遍历结果为:"<<endl;preorderTraversal(root);//前序遍历
}