读万卷书,行万里路——木子成
Hello world: 你可能需要保存一颗(!浮躁)的心来看这篇文章
类模板知识这里不单独作介绍啦!!!
tree.h
/*
一个节点的左子树:index * 2 + 1
一个节点的右子树:index * 2 + 2
一个节点的父节点:(index - 1) / 2
*/
/*
leftORright = -1 : 父节点
leftORright = 0 : 该节点
leftORright = 1 : 左子树
leftORright = 2 : 右子树
*/
#include<iostream>
using namespace std;
template<typename T>
class Node
{
public:
Node(T value = T(0)) { this->value = value; } // 默认为零,代表该节点没有元素
T value;
};
template<typename T>
class Tree
{
public:
Tree(const T& element)
{
spaceSize = 10;
space = new Node<T>[spaceSize];
space[0].value = element; // 根节点
lastIndex = 0;
}
~Tree() { delete[] space; }
int get_lastIndex();
Tree<T>& addNode(int index, int leftORright, T value);
T& getNode(int index, int leftOFright);
// 遍历
void preOrder(int i = 0); // 前序
void inOrder(int i = 0); // 中序
void postOrder(int i = 0); // 后序
void levelOrder(int i = 0); // 层次
private:
Node<T> *space;
int lastIndex;
int spaceSize;
};
tree.cpp
#include"tree.h"
template<typename T>
int Tree<T>::get_lastIndex()
{
return lastIndex;
}
template<typename T>
Tree<T>& Tree<T>::addNode(int index, int leftORright, T value)
{
if (index < 0 || index > lastIndex * 2 + 2) // 索引小于零和大于最后一个节点的右子树
return *this;
if ((leftORright == -1) && index == 0) // 根节点无父节点
return *this;
if (lastIndex * 2 + 2 > spaceSize) // 检测最后一个节点的左子树是否有空间
{
Node<T> *newSpace = new Node<T>[2 * spaceSize];
copy(space, space + spaceSize, newSpace);
delete[] space;
space = newSpace;
spaceSize *= 2;
}
switch (leftORright)
{
case -1: space[(index - 1) / 2].value = value; break;
case 0:
space[index].value = value;
if (index > lastIndex) // 确定最后一个元素
lastIndex = index;
break;
case 1:
space[index * 2 + 1].value = value;
if (index * 2 + 1 > lastIndex) // 确定最后一个元素
lastIndex = index * 2 + 1;
break;
case 2:
space[index * 2 + 2].value = value;
if (index * 2 + 2 > lastIndex) // 确定最后一个元素
lastIndex = index * 2 + 2;
}
return *this;
}
template<typename T>
T& Tree<T>::getNode(int index, int leftORright)
{
if (index < 0 || index > lastIndex * 2 + 2) // 索引小于零和大于最后一个节点的右子树
return space[spaceSize].value;
if(leftORright < -1 || leftORright > 2)
return space[spaceSize].value;
if ((leftORright == -1) && index == 0) // 根节点无父节点
return space[spaceSize].value;
switch (leftORright) // 要返回的值
{
case -1: return space[(index - 1) / 2].value;
case 0: return space[index].value;
case 1: return space[index * 2 + 1].value;
case 2: return space[index * 2 + 2].value;
}
}
template<typename T>
void Tree<T>::preOrder(int i)
{
if (space[i].value != T(0) && !(i > lastIndex))
{
cout << space[i].value << " ";
preOrder(i * 2 + 1);
preOrder(i * 2 + 2);
}
}
template<typename T>
void Tree<T>::inOrder(int i) // 中序
{
if (space[i].value != T(0) && !(i > lastIndex))
{
inOrder(i * 2 + 1);
cout << space[i].value << " ";
inOrder(i * 2 + 2);
}
}
template<typename T>
void Tree<T>::postOrder(int i) // 后序
{
if (space[i].value != T(0) && !(i > lastIndex))
{
postOrder(i * 2 + 1);
postOrder(i * 2 + 2);
cout << space[i].value << " ";
}
}
template<typename T>
void Tree<T>::levelOrder(int i) // 层次
{
for (int i = 0; i <= lastIndex; i++)
if (space[i].value != T(0))
cout << space[i].value << " ";
}
测试.cpp
#include<iostream>
#include"tree.cpp"
int main()
{
Tree<int> t(1);
t.addNode(0, 0, 1);
t.addNode(0, 1, 2);
t.addNode(0, 2, 3);
t.addNode(1, 2, 4);
t.addNode(2, 2, 5);
t.addNode(6, 1, 6);
cout << "二叉树前序遍历:";
t.preOrder();
cout << endl;
cout << "二叉树后序遍历:";
t.inOrder();
cout << endl;
cout << "二叉树后序遍历:";
t.postOrder();
cout << endl;
cout << "二叉树层次遍历:";
t.levelOrder();
return 0;
}