数据结构——17 二叉树(递归)
Posted langtaol
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构——17 二叉树(递归)相关的知识,希望对你有一定的参考价值。
二叉树——递归法
建立树时,按照左子树小于树根,右子树大于树根,这样中序遍历就是有序表
#include<iostream>
using namespace std;
class node //树节点定义
public:
int data; //元素值
node *parent; //父节点
node *left; //左子节点
node *right; //右子节点
public:
// node():data(-1),parent(NULL),left(NULL),right(NULL);
node(int num):data(num),parent(NULL),left(NULL),right(NULL); //构造函数,初始化类成员变量
;
class tree //树定义
public:
tree(int num[],int len); //构造函数
void insertNode(int data); //安左低右高插入树的节点
void preOrderTree(); //前序
void inOrderTree(); //中序
void postOrderTree(); //后序
private:
void insertNode(node *root,int data); //插入递归
void preOrderTree(node *current); //前序递归
void inOrderTree(node *current); //中序递归
void postOrderTree(node *current); //后序递归
node *root; //树的根节点
;
tree::tree(int num[],int len) //构造函数
root=new node(num[0]);
for(int i=1;i<len;i++)
insertNode(num[i]); //调用插入函数
void tree::insertNode(int data)
if(root!=NULL)
insertNode(root,data);
void tree::insertNode(node *current,int data) //递归插入函数树节点函数
if(data<= current->data)
if(current->left==NULL)
current->left =new node(data);
current->left->parent=current;
else
insertNode(current->left,data);
else if(data> current->data)
if(current->right==NULL)
current->right =new node(data);
current->right->parent=current;
else
insertNode(current->right,data);
return;
void tree::preOrderTree() //前序遍历
if(root==NULL)
return;
preOrderTree(root);
void tree::preOrderTree(node *current)
if(current!=NULL)
cout<<current->data<<" ";
preOrderTree(current->left);
preOrderTree(current->right);
void tree::inOrderTree() //中序遍历
if(root==NULL)
return;
inOrderTree(root);
void tree::inOrderTree(node *current)
if(current!=NULL)
inOrderTree(current->left);
cout<<current->data<<" ";
inOrderTree(current->right);
void tree::postOrderTree() //后序遍历
if(root==NULL)
return;
postOrderTree(root);
void tree::postOrderTree(node *current)
if(current!=NULL)
postOrderTree(current->left);
postOrderTree(current->right);
cout<<current->data<<" ";
int main()
int num[8]=5,3,7,2,4,6,8,1;
tree t(num,8);
cout<<"前序遍历: ";
t.preOrderTree();
cout<<endl<<endl;
cout<<"中序遍历: ";
t.inOrderTree();
cout<<endl<<endl;
cout<<"后序遍历: ";
t.postOrderTree();
cout<<endl;
return 0;
前面为了安全,程序写的比较麻烦,递归方法都放在private里,然后通过public里的接口调用,现在可以直接写成public的,容易理解。
#include<iostream>
using namespace std;
class node //树节点定义
public:
int data; //元素值
node *left; //左子节点
node *right; //右子节点
public:
// node():data(-1),parent(NULL),left(NULL),right(NULL);
node(int num):data(num),left(NULL),right(NULL); //构造函数,初始化类成员变量
;
class tree //树定义
public:
node *root; //树的根节点
tree(int num[],int len); //构造函数
void insertNode(node *root,int data); //插入递归 按左低右高插入树的节点
void preOrderTree(node *current); //前序递归
void inOrderTree(node *current); //中序递归
void postOrderTree(node *current); //后序递归
;
tree::tree(int num[],int len) //构造函数
root=new node(num[0]);
for(int i=1;i<len;i++)
insertNode(root,num[i]); //调用插入函数
void tree::insertNode(node *current,int data) //递归插入函数树节点函数
if(data<= current->data)
if(current->left==NULL)
current->left =new node(data);
else
insertNode(current->left,data);
else
if(current->right==NULL)
current->right =new node(data);
else
insertNode(current->right,data);
void tree::preOrderTree(node *current) //前序遍历
if(current!=NULL)
cout<<current->data<<" ";
preOrderTree(current->left);
preOrderTree(current->right);
void tree::inOrderTree(node *current) //中序遍历
if(current!=NULL)
inOrderTree(current->left);
cout<<current->data<<" ";
inOrderTree(current->right);
void tree::postOrderTree(node *current) //后序遍历
if(current!=NULL)
postOrderTree(current->left);
postOrderTree(current->right);
cout<<current->data<<" ";
int main()
int num[8]=5,3,7,2,4,6,8,1;
tree t(num,8);
cout<<"前序遍历: ";
t.preOrderTree(t.root);
cout<<endl<<endl;
cout<<"中序遍历: ";
t.inOrderTree(t.root);
cout<<endl<<endl;
cout<<"后序遍历: ";
t.postOrderTree(t.root);
cout<<endl;
return 0;
以上是关于数据结构——17 二叉树(递归)的主要内容,如果未能解决你的问题,请参考以下文章