二叉树的递归定义:二叉树要么为空,要么由根节点,左子树(Left Subtree),右子树(Right Subtree)构成。左右两棵子树又分别是一棵二叉树。
每个结点的定义
struct TreeNode{
ElementType data;
TreeNode *leftChild;
TreeNode *rightChild;
};
遍历
PreOrder
void PreOrder(TreeNode *root){
if(root==NULL) return;
visit(root->data);
PreOrder(root->leftChild);
PreOrder(root->rightChild);
return;
}
InOrder
if(root==NULL) return;
InOrder(root->leftChild);
visit(root->data);
InOrder(root->rightChild);
return;
}
PostOrder
void PosOrder(TreeNode *root){
if(root==NULL) return;
PosOrder(root->leftChild);
PosOrder(root->rightChild);
visit(root->data);
return;
}
LeverOrder
void LevelOrder(TreeNode *root){
queue<TreeNode*> myQueue;
if(root!=NULL){
myQueue.push(root);
}
while(!myQueue.empty()){
TreeNode *current=myQueue.front();
myQueue.pop();
visit(current->data);
if(current->leftChild!=NULL) myQueue.push(current->leftChild);
if(current->rightChild!=NULL) myQueue.push(current->rightChild);
}
return;
}
熟悉下 建树+遍历 的程序
建树 首先结点定义要写一个默认构造函数(初始化data) ,然后Build返回值为TreeNode* ,然后根结点TreeNode *root=new TreeNode( c );
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct TreeNode{
char data;
TreeNode *leftChild;
TreeNode *rightChild;
TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL){}
};
TreeNode* Build(int &position,string str){
char c=str[position++];
if(c=='#') return NULL;
TreeNode *root=new TreeNode(c);
root->leftChild=Build(position,str);
root->rightChild=Build(position,str);
return root;
}
void InOrder(TreeNode* root){
if(root==NULL) return;
InOrder(root->leftChild);
cout<<root->data<<" ";
InOrder(root->rightChild);
return;
}
int main(){
string str;
while(cin>>str){
int position=0;
TreeNode * root=Build(position,str);
InOrder(root);
cout<<endl;
}
}
由前序序列和中序序列构树
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct TreeNode{
char data;
TreeNode *leftChild;
TreeNode *rightChild;
TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL){}
};
TreeNode *Build(string a,string b){
if(a.size()==0) return NULL;
char c=a[0];
TreeNode *root=new TreeNode(c);
int position=b.find(c);
root->leftChild=Build(a.substr(1,position),b.substr(0,position));
root->rightChild=Build(a.substr(position+1),b.substr(position+1));
return root;
}
void PostOrder(TreeNode *root){
if(root==NULL) return;
PostOrder(root->leftChild);
PostOrder(root->rightChild);
cout<<root->data;
}
int main(){
string a,b;
while(cin>>a>>b){
TreeNode *root=Build(a,b);
PostOrder(root);
cout<<endl;
}
}
二叉排序(搜索)树 Binary Search Tree![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/89224b78b8f112e30d0a528f1945fcaf.jpeg)
Insert()二叉排序树的建树+输出【父节点】的操作:+参数father
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/440747a0d42aa5b4a186aa07855a1a31.png)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
struct TreeNode{
int a;
TreeNode *leftChild;
TreeNode *rightChild;
TreeNode(int n):a(n),leftChild(NULL),rightChild(NULL){};
};
TreeNode * Insert(TreeNode* root,int x,int father){
if(root==NULL){
root=new TreeNode(x);
cout<<father<<endl;
}
else{
if(x<root->a){
root->leftChild=Insert(root->leftChild,x,root->a);
}
else if(x>root->a){
root->rightChild=Insert(root->rightChild,x,root->a);
}
}
return root;
}
int main(){
int n;
while(cin>>n){
TreeNode *root=NULL;
for(int i=0;i<n;i++){
int x;
cin>>x;
root=Insert(root,x,-1);
}
}
}