二叉树操作实现常见操作

来源:互联网 发布:演讲与口才 知乎 编辑:程序博客网 时间:2024/05/01 09:41

大概实现的功能:

1、 创建二叉树类。二叉树的存储结构使用链表。

2、 提供操作:前序遍历、中序遍历、后序遍历、层次遍历、删除指定元素、计算二叉树结点数目、计算二叉树高度。//删除操作采用搜索二叉树的删除

3、 对建立好的二叉树,执行上述各操作。

4、 接收键盘录入的二叉树前序序列和中序序列(各元素各不相同),输出该二叉树的后序序列。


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

二叉树类

#pragma once#include "LinkedQueue.h"#include <iostream>using namespace std ;template <class T>class BinaryTree ;template <class T>class BinaryTreeNode{friend BinaryTree<T> ;public:BinaryTreeNode(void){ LeftChild = RightChild = 0 ;}BinaryTreeNode(const int& e){data = e ; LeftChild = RightChild = 0 ;}BinaryTreeNode(const int& e , BinaryTreeNode<T> * l , BinaryTreeNode<T> * r){data = e ; LeftChild = l ; RightChild = r ;}~BinaryTreeNode() {};private:T data ;BinaryTreeNode<T> * LeftChild , * RightChild ;};template <class T>class BinaryTree{public:BinaryTree(void){ root = 0 ; }~BinaryTree() ;bool IsEmpty()const {return ((root)?false:true);}bool Root(int& t)const ;void MakeTree(const int& element , BinaryTree<T>& left , BinaryTree<T>& right) ;void BreakTree(int& element , BinaryTree<T>& left , BinaryTree<T>& right) ;void PreOrder(void (*Visit)(BinaryTreeNode<T> * u)){ PreOrder(Visit , root) ;}void InOrder(void (*Visit)(BinaryTreeNode<T> * u)){ InOrder(Visit , root) ;}void PostOrder(void(*Visit)(BinaryTreeNode<T> * u)){ PostOrder(Visit , root) ;}void LevelOrder(void(*Visit)(BinaryTreeNode<T> * u)) ;int getHight() const { return Height(root) ;}void PreOutput() { PreOrder(Output , root) ; cout << endl ;}void InOutput() { InOrder(Output , root) ; cout << endl ;}void PostOutput() { PostOrder(Output , root); cout << endl ;}void LevelOutput() { LevelOrder(Output); cout << endl ;}BinaryTree<T>& Delete(const T& k , T e) ;int GetLR(T * Pre , T * In , int i , T data) ;void AddTreeNode(BinaryTreeNode<T> ** root , T * PreOrder , T * InOrder , int i) ;void PreInMakeTree(T * PreOrder , T * InOrder , int length ){for(int i = 0 ; i < length ; i++)AddTreeNode(&root , PreOrder , InOrder , i) ; }private:BinaryTreeNode<T> * root ;void PreOrder(void (*Visit)(BinaryTreeNode<T> * u ) , BinaryTreeNode<T> * t ) ;void InOrder(void (*Visit)(BinaryTreeNode<T> * u ) , BinaryTreeNode<T> * t ) ;void PostOrder(void(*Visit)(BinaryTreeNode<T> * u ) , BinaryTreeNode<T> * t) ;int Height(BinaryTreeNode<T> * t)const ;static void Output(BinaryTreeNode<T> * t){ cout << t->data << "" ;}};template <class T>BinaryTree<T>::~BinaryTree(){}template <class T>bool BinaryTree<T>::Root(int& x)const{if(root){x = root->data ;return true ;}elsereturn false ;}template <class T>void BinaryTree<T>::MakeTree(const int& element ,BinaryTree<T>& left , BinaryTree<T>& right){root = new BinaryTreeNode<T>(element , left.root , right.root) ;left.root = right.root = 0 ;}template <class T>void BinaryTree<T>::BreakTree(int& element , BinaryTree<T>& left , BinaryTree<T>& right){if(!root)throw "badiput" ;element = root->data ;left.root = root->LeftChild ;right.root = root->RightChild ;delete root ;root = 0 ;}template <class T>void BinaryTree<T>::PreOrder(void(*Visit)(BinaryTreeNode<T> * u) , BinaryTreeNode<T> * t){if(t){Visit(t) ;PreOrder(Visit , t->LeftChild) ;PreOrder(Visit , t->RightChild) ;}}template <class T>void BinaryTree<T>::InOrder(void(*Visit)(BinaryTreeNode<T> * u) , BinaryTreeNode<T> * t){if(t){InOrder(Visit , t->LeftChild) ;Visit(t) ;InOrder(Visit , t->RightChild) ;}}template <class T>void BinaryTree<T>::PostOrder(void(*Visit)(BinaryTreeNode<T> * u) , BinaryTreeNode<T> * t){if(t){PostOrder(Visit , t->LeftChild) ;PostOrder(Visit , t->RightChild) ;Visit(t) ;}}template <class T>void BinaryTree<T>::LevelOrder(void(*Visit)(BinaryTreeNode<T> * u)){LinkedQueue<BinaryTreeNode<T>*> Q ;BinaryTreeNode<T> *t ;t = root ;while(t){Visit(t) ;if(t->LeftChild)Q.Add(t->LeftChild) ;if(t->RightChild)Q.Add(t->RightChild) ;try{Q.Delele(t) ;}catch(char * ch){return ;}}}template <class T>int BinaryTree<T>::Height(BinaryTreeNode<T> * t)const{if(!t)return 0 ;int hl = Height(t->LeftChild) ;int hr = Height(t->RightChild) ;if(hl>hr)return ++hl ;elsereturn ++hr ;}template<class T>BinaryTree<T>& BinaryTree<T>::Delete(const T& k , T e){BinaryTreeNode<T> *p = root ,*pp = 0 ;while(p&&p->data!=k){pp = p ;if(k<p->data) p = p ->LeftChild ;else  p = p->RightChild ;}if(!p) throw "wrong" ;e = p->data ;//将删除元素左子树的中最大元素放到pif(p->LeftChild&&p->RightChild){BinaryTreeNode<T> *s = p->LeftChild , *ps = p ;while(s->RightChild){ps = s ;s = s->RightChild ;}p->data = s->data ;p = s ;pp = ps ;}//p只有一个子树的时候BinaryTreeNode<T> *c ;if(p->LeftChild) c = p->LeftChild ;else c = p->RightChild ;if(p == root) root = c ;else{if(p == pp->LeftChild) pp->LeftChild = c ;else pp->RightChild = c ;}delete p ;return *this ;}template <class T>int BinaryTree<T>::GetLR(T * Pre , T * In , int i , T data){int j = 0 , k = 0 ;while(Pre[i]!=In[j])j ++ ;while(data!=In[k])k ++ ;if(j<k)return 1 ;else return 0 ;}template <class T>void BinaryTree<T>::AddTreeNode(BinaryTreeNode<T> ** root , T * PreOrder , T * InOrder , int i){if(NULL == *root){*root = new BinaryTreeNode<T>(PreOrder[i]) ;/*cout << root->data << endl ;*/}elseif(GetLR(PreOrder , InOrder , i , (**root).data))AddTreeNode(&(**root).LeftChild , PreOrder , InOrder , i) ;else AddTreeNode(&(**root).RightChild , PreOrder , InOrder , i) ;}



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


二叉树的层次便利用到了队列

给出链表实现:

#pragma oncetemplate<class T>class LinkedQueue ;template<class T>class Node{public:friend LinkedQueue<T> ;private:T data ;Node<T>* link ;};template<class T>class LinkedQueue{public:LinkedQueue() {front = rear = 0 ;}~LinkedQueue(void) {}bool IsEmpty()const {return ((front)?false:true) ;}bool IsFull() ;T First() const ;T Last() const ;LinkedQueue<T>& Add(const T& x) ;LinkedQueue<T>& Delele(T& x) ;private:Node<T>* front ;Node<T>* rear ;};template<class T>bool LinkedQueue<T>::IsFull() {try{p = new Node ;delete p ;return false ;}catch(char* ch){return true ;}}template<class T>T LinkedQueue<T>::Last() const {if(IsEmpty())throw "wrong" ;return rear->data ;}template<class T>T LinkedQueue<T>::First() const {if(IsEmpty())throw "wrong" ;return front->data ;}template<class T>LinkedQueue<T>& LinkedQueue<T>::Add(const T& x) {Node<T> *p = new Node<T> ;p->data = x ;p->link = 0 ;if(front)rear->link = p ;elsefront = p ;rear = p ;return *this ;}template<class T>LinkedQueue<T>& LinkedQueue<T>::Delele(T& x) {if(IsEmpty())throw "wrong" ;x = front->data ;Node<T> * p = front ;front = front->link ;delete p ;return *this ;}




---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
最后是入口函数:
#include "stdafx.h"#include <iostream>#include "BinaryTree.h"#include <math.h>using namespace std ;int count = 0 ;BinaryTree<int> a , x , y , z  , h ;void ct(BinaryTreeNode<int> *t){ count ++ ;}int _tmain(int argc, _TCHAR* argv[]){y.MakeTree(1 , a , a) ;z.MakeTree(2 , a , a) ;x.MakeTree(3 , y , z) ;h.MakeTree(5 , a , a) ;y.MakeTree(4 , x , h) ;cout << "前序" ;y.PreOutput() ;cout << "中序" ;y.InOutput() ;cout << "后序" ;y.PostOutput() ;cout << "逐层" ;y.LevelOutput() ;int hight = y.getHight() ;cout << "高度" <<  hight << endl ;y.PreOrder(ct) ;cout << "Node数目" << count << endl ;count = 0 ;cout << "删除元素3" << endl ;int delement = 3 ;int temp = 0 ;y.Delete(3 ,temp) ;cout << "删除元素3后前序输出" << endl ;y.PreOutput() ;//-----------------------------------------------------------------//接受键盘输入cout << "输入节点个数" << endl ;int Number = 0 ;cin >> Number ;cout << "输入前序遍历的顺序" << endl ;int * Pre = new int[Number] ;for(int i = 0 ; i < Number ; i ++)cin >> Pre[i] ;cout << "输入中序遍历的顺序" << endl ;int * In = new int[Number] ;for(int i = 0 ; i < Number ; i ++)cin >> In[i] ;BinaryTree<int> g ;g.PreInMakeTree( Pre , In , Number) ;g.PostOutput() ;system("pause") ;cout << "" << endl ;return 0 ;}



0 0
原创粉丝点击