【数据结构】二叉树

来源:互联网 发布:大连知润信息科技骗局 编辑:程序博客网 时间:2024/06/03 19:06

二叉树节点

#pragma once#include <stdlib.h>template<class T>class BinaryTreeNode{public:T data;BinaryTreeNode<T>* leftchild;BinaryTreeNode<T>* rightchild;BinaryTreeNode():leftchild(NULL),rightchild(NULL){}BinaryTreeNode(T d):data(d),leftchild(NULL),rightchild(NULL){}};
二叉树的所有操作:建树,销毁树,先序后序中序便利的递归和非递归方式层序遍历
#include "BinaryTreeNode.h"#include <queue>#include <stack>#include <iostream>using namespace std;/*void CreatePreOrder(BinaryTreeNode<T>** root)void LevelOrderTraverse()//层序遍历void PreOrderTraverse(BinaryTreeNode<T>* root)//先序遍历递归void PreOrderTraverse()//先序遍历非递归算法void InOrderTraverse()//中序遍历的非递归方法void PostOrderTraverse()//后序遍历非递归方法int depth()//树深度BinaryTreeNode<T>* Parent(BinaryTreeNode<T>*root,BinaryTreeNode<T>*node)//返回节点的双亲BinaryTreeNode<T>* SiblingNode(BinaryTreeNode<T>*node)//返回兄弟节点*/template<class T>class BinaryTree{public:BinaryTreeNode<T>* root;BinaryTree():root(NULL){}//空树/********************************先序创建链表****************************************/void CreatePreOrder(BinaryTreeNode<T>** root)//或者BinaryTreeNode<T>* &root下面依次改变{char d;cin>>d;if (d=='#')(*root) = NULL;else{*root = new BinaryTreeNode<T>(d);CreatePreOrder(&(*root)->leftchild);CreatePreOrder(&(*root)->rightchild);} }/********************************层序遍历****************************************/void LevelOrderTraverse(){if (root==NULL)return;BinaryTreeNode<T>* p;queue<BinaryTreeNode<T>*> myQueue;myQueue.push(root);myQueue.push(NULL);while (!myQueue.empty()){p=myQueue.front();myQueue.pop();if (p==NULL){if (myQueue.empty())break;cout<<endl;myQueue.push(NULL);}else{cout<<p->data;if (p->leftchild)myQueue.push(p->leftchild);if (p->rightchild)myQueue.push(p->rightchild);}}}/********************************先序遍历递归****************************************/void PreOrderTraverse(BinaryTreeNode<T>* root){if (root){cout<<root->data;PreOrderTraverse(root->leftchild);PreOrderTraverse(root->rightchild);}}/********************************中序遍历递归****************************************/void InOrderTraverse(BinaryTreeNode<T>* root){if (root){InOrderTraverse(root->leftchild);cout<<root->data;InOrderTraverse(root->rightchild);}}/********************************后序遍历递归****************************************/void PostOrderTraverse(BinaryTreeNode<T>* root){if (root){PostOrderTraverse(root->leftchild);PostOrderTraverse(root->rightchild);cout<<root->data;}}/********************************先序遍历非递归****************************************/void PreOrderTraverse(){if (root){BinaryTreeNode<T>* p;stack<BinaryTreeNode<T>*> myStack;myStack.push(root);while (!myStack.empty()){p=myStack.top();myStack.pop();cout<<p->data;if (p->rightchild)myStack.push(p->rightchild);if (p->leftchild)myStack.push(p->leftchild);}}}/********************************中序遍历非递归****************************************/void InOrderTraverse(){if (root==NULL)return;BinaryTreeNode<T>* p;stack<BinaryTreeNode<T>*> myStack;myStack.push(root);while (!myStack.empty()){while (myStack.top())myStack.push(myStack.top()->leftchild);myStack.pop();if (!myStack.empty()){p=myStack.top();myStack.pop();cout<<p->data;myStack.push(p->rightchild);}}}/********************************后序遍历非递归****************************************/void PostOrderTraverse(){if (root==NULL)return;stack<BinaryTreeNode<T>*> myStack1;stack<BinaryTreeNode<T>*> myStack2;BinaryTreeNode<T> * p;myStack1.push(root);while (!myStack1.empty()){p=myStack1.top();myStack1.pop();myStack2.push(p);if (p->leftchild)myStack1.push(p->leftchild);if(p->rightchild)myStack1.push(p->rightchild);}while (!myStack2.empty()){p=myStack2.top();myStack2.pop();cout<<p->data;}}/********************************树的深度****************************************/int depth(BinaryTreeNode<T>* root){int dep;if (root==NULL)dep=0;else{dep=1+(depth(root->leftchild)>depth(root->rightchild)?depth(root->leftchild):depth(root->rightchild));}return dep;}/********************************返回双亲节点****************************************/BinaryTreeNode<T>* Parent(BinaryTreeNode<T>*root,BinaryTreeNode<T>*node){if (root==NULL || root==node)return NULL;if (root->leftchild==node||root->rightchild==node){return root;}else if(Parent(root->leftchild,node)){return Parent(root->leftchild,node);}elsereturn Parent(root->rightchild,node);}/********************************返回双亲节点(重载)****************************************/BinaryTreeNode<T>* ParentPre(BinaryTreeNode<T>*root,BinaryTreeNode<T>*node)//通过遍历树来搜索{if (root==node||root==NULL)return NULL;if(root){if (root->leftchild==node||root->rightchild==node)return root;if (ParentPre(root->leftchild,node))returnParentPre(root->leftchild,node);elsereturn ParentPre(root->rightchild,node);}}/********************************返回兄弟节点****************************************/BinaryTreeNode<T>* SiblingNode(BinaryTreeNode<T>*node){BinaryTreeNode<T>*p=Parent(root,node);if (p){if (p->leftchild==node)return p->rightchild;elsereturn p->leftchild;}return NULL;}/********************************销毁树****************************************/void DestroyTree(BinaryTreeNode<T>*root){if (root!=NULL){DestroyTree(root->leftchild);DestroyTree(root->rightchild);delete root;}root=NULL;}};


2 0