实现二叉树的前中后遍历,将树结构也打了一遍
#include<iostream>
#include<queue>
using namespace std;
template<class T>
struct binaryTreeNode{
T element;
binaryTreeNode *leftChild,
*rightChild;
binaryTreeNode(){ leftChild = rightChild = NULL; }
binaryTreeNode(const T& theElement ){ leftChlid = rightChild = NULL;
element(theElement);
}
binaryTreeNode(const T& theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild){
element(theElement);
leftChild = theLeftChild;
rightChild = theRightChild;
}
};
template<class T>
class binaryTree{
public:
binaryTree(){
root = NULL;
treeSize = 0;
}
~binaryTree(){ erase(); };
bool empty(){ return treeSize == 0; };
int size(){ return treeSize; };
void preOrder(void(*theVisit)(binaryTreeNode<T>*))
{
visit = theVisit; preOrder(root);
}
void inOrder(void(*theVisit)(binaryTreeNode<T>*))
{
visit = theVisit; inOrder(root);
}
void postOrder(void(*theVisit)(binaryTreeNode<T>*))
{
visit = theVisit; postOrder(root);
}
void visit(binaryTreeNode<T> *x){
cout << x->element << " ";
}
void levelOrder(binaryTreeNode<T> *t){
queue<binaryTreeNode<T>> q;
while (t != NULL)
{
visit(t);
if (t->leftChild != NULL)
{
q.push(t->leftChild);
}
if (t->rightChild != NULL)
{
q.push(t->rightChild);
}
if ((t = q.front()) == NULL);
{return; }
q.pop(t);
}
}
void erase(){
postOrder(dispose);
root = NULL;
treeSize = 0;
}
void addNodetoroot(T element){
binaryTreeNode *p = new binaryTreeNode(element);
if (root == NULL)
root = p;
}
binaryTreeNode<T> *root;
int treeSize;
void preOrder(binaryTreeNode<T> *t);
void inOrder(binaryTreeNode<T> *t);
void postOrder(binaryTreeNode<T> *t);
void dispose(binaryTreeNode<T> *t){ delete t };
};
template<class T>
void binaryTree<T>:: preOrder(binaryTreeNode<T> *t){
if (t != NULL){
visit(t);
preOrder(t->leftChild);
preOrder(t->rightChild);
}
}
template<class T>
void binaryTree<T>::inOrder(binaryTreeNode<T> *t){
if (t != NULL){
inOrder(t->leftChild);
visit(t);
inOrder(t->rightChild);
}
}
template<class T>
void binaryTree<T>::postOrder(binaryTreeNode<T> *t){
if (t != NULL){
postOrder(t->leftChild);
postOrder(t->rightChild);
visit(t);
}
}
2.实现一个二叉查找树,并且支持插入、删除、查找操作,查找二叉查找树中某个节点的后继、前驱节点
打了整个一个晚上,结果这个还是在指针上崩溃了啊啊啊啊啊,怎么办,好烦的指针
#include<iostream>
#include<queue>
using namespace std;
template<class T>
struct binaryTreeNode{
T element;
binaryTreeNode *leftChild,
*rightChild, *parent;
binaryTreeNode(){};
binaryTreeNode(const T& theElement) :
element(theElement){
leftChild = rightChild = parent = NULL;
}
};
template<class T>
class binSearchTree{
binaryTreeNode<T>* root;
public:
void insert(binaryTreeNode<T>* z){
binaryTreeNode<T>* p = root;
binaryTreeNode<T>* q = NULL;
while (p != NULL){
q = p;
if (z->element <= p->element)
p = p->leftChild;
else
p = p->rightChild;
}
z->parent = q;
if (q == NULL)
root = z;
else if (z->element <= q->element)
p->leftChild = z;
else
p->rightChild = z;
}
void insert(T element){
binaryTreeNode<T>* z = new binaryTreeNode<T>(element);
insert(z);
}
binaryTreeNode<T>* find(T element){
return find(root, element);
}
binaryTreeNode<T>* find(binaryTreeNode<T>* z, T element){
if (z == NULL)
return z;
else if (element = z->element){
return z;
}
if (element <= z->element)
return find(z->leftChild, element);
else
return find(z->rightChild, element);
}
binaryTreeNode<T>* predecessor(binaryTreeNode<T>* x){
if (x->leftChild != NULL)
return maximum(x->leftChild);
binaryTreeNode<T>* y = x->parent;
while ((y != NULL) && (x == y->leftChild)){
x = y;
y = y->parent;
}
return y;
}
binaryTreeNode<T>* succesor(binaryTreeNode<T>* x){
if (x->rightChild != NULL)
return minimum(x->rightChild);
binaryTreeNode<T>* y = x->parent;
while ((y != NULL) && (x == y->rightChild)){
x = y;
y = y->parent;
}
}
binaryTreeNode<T>* maximum(binaryTreeNode<T>* x){
if (x == NULL)
return NULL;
while (x->rightChild != NULL)
{
x = x->rightChild;
}
return x;
}
T maximum(){
binaryTreeNode<T>* x = maximum(root);
if (x != NULL)
return x->element;
return (T)NULL;
}
binaryTreeNode<T>* minimum(binaryTreeNode<T>* x){
if (x == NULL)
return NULL;
while (x->leftChild != NULL)
{
x = x->leftChild;
}
return x;
}
T minimum(){
binaryTreeNode<T>* x = minimum(root);
if (x != NULL)
return x->element;
return (T)NULL;
}
void deleteNode(T element){
binaryTreeNode<T>* x;
binaryTreeNode<T>* z = find(element);
if (z != NULL){
if ((x = deleteNode(root, z) )!= NULL)
delete x;
}
}
binaryTreeNode<T>* deleteNode(binaryTreeNode<T>* &tree, binaryTreeNode<T>* z){
binaryTreeNode<T>* x = NULL;
binaryTreeNode<T>* y = NULL;
if ((z->leftChild == NULL) || (z->rightChild == NULL))//z有一根子树,或者是树叶时
y = z;
else
y = succesor(z);//有双子树时,找到后驱或前驱节点
if (y->leftChild != NULL)//后驱节点不是树叶时,即有左子树时
x = y->leftChild;
else
x = y->rightChild;//x是y的后代
if (x != NULL)
x->parent = y->parent;//y的父亲赋给x的父亲
if (y->parent == NULL)
tree = x;
else if (y == y->parent->leftChild)
y->parent->leftChild = x;
else y->parent->rightChild = x;//x代替了y
if (y != z)
z->element = y->element;
return y;
}
void preOrder(binaryTreeNode<T>* x) const
{
if (x != NULL)
{
cout << x->element << " ";
preOrder(x->leftChild);
preOrder(x->rightChild);
}
}
void preOrder()
{
binaryTreeNode<T>* x = root;
preOrder(x);
}
void inOrder(binaryTreeNode<T>* x) const
{
if (x != NULL)
{
cout << x->element << " ";
inOrder(x->leftChild);
inOrder(x->rightChild);
}
}
void inOrder()
{
binaryTreeNode<T>* x = root;
inOrder(x);
}
void postOrder(binaryTreeNode<T>* x) const
{
if (x != NULL)
{
cout << x->element << " ";
postOrder(x->leftChild);
postOrder(x->rightChild);
}
}
void postOrder()
{
binaryTreeNode<T>* x = root;
postOrder(x);
}
};
void main(){
binSearchTree<char> Tree;
Tree.insert('a');
Tree.insert('b');
Tree.insert('g');
Tree.insert('y');
Tree.insert('r');
Tree.insert('E');
Tree.insert('F');
Tree.preOrder();
cout << endl;
Tree.inOrder();
cout << endl;
Tree.postOrder();
cout << endl;
Tree.deleteNode('a');
cout << endl;
Tree.preOrder();
cout << endl;
Tree.inOrder();
cout << endl;
Tree.postOrder();
cout << endl;
system("pause");
return;
}