資料結構學習3---二叉樹,資料結構學習3---
二叉樹節點
#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;}};