- 创建二叉树
#include<iostream>
class Node{
public:
int data;
Node *lchild, *rchild;
Node(int _data){
data = _data;
lchild = NULL;
rchild = NULL;
}
};
class BinaryTree{
private:
Node *root;
public:
BinaryTree(){
root = NULL;
}
};
int main(){
BinaryTree binarytree;
return 0;
}
- 存储二叉树
#include<iostream>
class Node {
public:
int data;
Node *lchild, *rchild;
Node(int _data) {
data = _data;
lchild = NULL;
rchild = NULL;
}
~Node(){
if (lchild != NULL)
{
delete lchild;
}
if (rchild != NULL)
{
delete rchild;
}
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree(){
delete root;
}
void build_demo()
{
root = new Node(1);
root->lchild = new Node(2);
root->rchild = new Node(3);
root->lchild->lchild = new Node(4);
root->lchild->rchild = new Node(5);
root->rchild->rchild = new Node(6);
}
};
int main() {
BinaryTree binarytree;
binarytree.build_demo();
return 0;
}
- 已知先序和中序求后序
#include<iostream>
#include<string>
using namespace std;
class Node {
public:
int data;
Node *lchild, *rchild;
Node(int _data) {
data = _data;
lchild = NULL;
rchild = NULL;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void postorder() {
if (lchild != NULL) {
lchild->postorder();
}
if (rchild != NULL) {
rchild->postorder();
}
cout << data << " ";
}
Node *build(const string &pre_str, const string &in_str, int len)
{
Node *p = new Node(pre_str[0] - '0');
int pos = in_str.find(pre_str[0]);
if (pos > 0)
{
p->lchild = build(pre_str.substr(1, pos), in_str.substr(0, pos), pos);
}
if (len - pos - 1 > 0)
{
p->rchild = build(pre_str.substr(pos + 1), in_str.substr(pos + 1), len - pos - 1);
}
return p;
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
delete root;
}
BinaryTree(const string &pre_str, const string &in_str, int len){
root = root->build(pre_str, in_str, len);
}
void postorder() {
root->postorder();
}
};
int main() {
string pre_str = "136945827";
string in_str = "963548127";
BinaryTree binarytree(pre_str, in_str, in_str.length());
binarytree.postorder();
cout << endl;
return 0;
}