数据结构之二叉树 (构造 拷贝构造 以及前序中序后续三种遍历方法)

首先二叉树的节点定义如下:
struct BinaryNode
{

                 BinaryNode *_left;
                 BinaryNode *_right;
                 T _data;
                BinaryNode( T data ) :_data(data), _left( NULL), _right(NULL )
                {};
};

二叉树的结构以及接口如下
template<class T>
class BinaryTree
{
                 typedef BinaryNode <T> Node;
public:
                BinaryTree() :_head( NULL)
                {};
                BinaryTree( const T * a, size_t size, const T & valiue)
                ~BinaryTree()
                BinaryTree( BinaryTree &b )
                void PrevOder()
                 void InOder()
                 void PostOder()
private:
public:
                 void LevalOder()
                 size_t depth()
                 size_t size()
                 size_t learsize()
                 void _LevalOder(BinaryNode <T> *root);
                 size_t _depth(BinaryNode <T> *root);
                 void _size(BinaryNode <T> *root, int *p);
                 void _leafsize(BinaryNode <T> *root, size_t *p);
                 int Leafsize(BinaryNode <T> *root);
                 void PrevOder_Nor();
                 void InOder_Nor();
                 void PostOder_Nor();
                 void Distory(Node *_root)
                 Node* _Copy(Node *cur);
private:
                 BinaryNode<T > *_root;
};

二叉树的建立(构造函数)
                BinaryTree( const T * a, size_t size, const T & valiue)
                {
                                 size_t index = 0;
                                _root = _CreatTree( a, size , index, valiue);
                }
                 BinaryNode<T >* _CreatTree(const T* a, size_t size, size_t &index, const T &valiue)
                {
                                 BinaryNode<T > *root = NULL;
                                 if (index < size&& a[index ] != valiue)
                                {
                                                root = new BinaryNode <T>(a[index]);
                                                root->_left = _CreatTree(a, size , ++index, valiue);
                                                root->_right = _CreatTree(a, size , ++index, valiue);
                                }
                                 return root;
                }
二叉树的销毁(析构函数)
                ~BinaryTree()
                {
                                Distory(_root);
                                cout << " ~BinaryTree()" << endl;
                }
                 void Distory(Node *_root)
                {
                                 if (_root == NULL)
                                                 return;
                                Distory( _root->_left);
                                Distory( _root->_right);
                                 if (_root )
                                                 delete _root ;
                                 _root == NULL ;

                }
二叉树的拷贝(拷贝构造)
                BinaryTree( BinaryTree &b )
                {
                                _root = _Copy( b._root);
                }
BinaryNode<T >* BinaryTree< T>::_Copy(Node *cur)
{
                 if (cur == NULL)
                                 return NULL ;
                 Node *tmp = new Node(cur->_data);
                tmp->_left=_Copy( cur->_left);
                tmp->_right=_Copy( cur->_right);
                 return tmp;

}
求叶子节点个数(两种方法)
一:
int BinaryTree <T>::Leafsize( BinaryNode<T > *root)
{
                 int count;
                 if (root == NULL)
                                count = 0;
                 else if (root->_left == NULL&&root ->_right == NULL)
                {
                                count = 1;
                }
                 else  count = Leafsize(root ->_left) + Leafsize(root->_right);
                 return count;
}
二:
void BinaryTree <T>::_leafsize( BinaryNode<T > *root, size_t * p)
{
                 if (root != NULL)
                {
                                _leafsize( root->_left,p );
                                _leafsize( root->_right,p );
                                 if (root ->_left == NULL&& root->_right == NULL )
                                {
                                                ++ *p;
                                }

                }
}

二叉树前序遍历递归
                 void _PrevOder(BinaryNode <T> * root)
                {
                                 if (root == NULL)
                                                 return;
                                cout << root->_data << " " ;
                                _PrevOder( root->_left);
                                _PrevOder( root->_right);
                }
二叉树前序遍历非递归
void BinaryTree <T>::PrevOder_Nor()
{
                 BinaryNode<T > *cur = _root;
                 stack<BinaryNode <T>*> s;
                 if (cur == NULL )
                                 return;
                s.push(cur);
                 while (!s.empty())
                {
                                 BinaryNode<T > *tmp = s.top();
                                cout << tmp->_data << " ";
                                s.pop();
                                 if (tmp->_right)
                                {
                                                s.push(tmp->_right);
                                }
                                 if (tmp->_left)
                                {
                                                s.push(tmp->_left);
                                }
                }
                cout << endl;
                
}
二叉树层序遍历(队列实现)
void BinaryTree <T>::_LevalOder( BinaryNode<T > *root)
{
                 deque<BinaryNode <T>*> q;
                q.push_back( root);
                 while (q.size())
                {
                                 BinaryNode<T > *pNode = q.front();
                                q.pop_front();
                                cout << pNode->_data << " ";
                                 if (pNode->_left)
                                {
                                                q.push_back(pNode->_left);
                                }
                                 if (pNode->_right)
                                {
                                                q.push_back(pNode->_right);
                                }
                }
                

}
二叉树中序遍历递归
                 void _InOder(BinaryNode <T> * root)
                {
                                 if (root == NULL)
                                                 return;
                                _InOder( root->_left);
                                cout << root->_data << " " ;
                                _InOder( root->_right);
                }
二叉树中序遍历非递归
void BinaryTree <T>::InOder_Nor()
{
                 if (_root == NULL )
                                 return;
                 Node *cur = _root;
                 stack<Node *> s;
                 while (cur||!s.empty())
                {
                                 while (cur)
                                {
                                                s.push(cur);
                                                cur = cur->_left;
                                }
                                 Node *tmp = s.top();
                                cout << tmp->_data << " ";
                                s.pop();
                                cur = tmp->_right;
                }
                cout << endl;

}
二叉树后序遍历递归
                 void  _PostOder(BinaryNode <T> * root)
                {
                                 if (root == NULL)
                                                 return;
                                _PostOder( root->_left);
                                _PostOder( root->_right);
                                cout << root->_data << " " ;
                }
二叉树后序遍历非递归
void BinaryTree <T>::PostOder_Nor()
{
                 if (_root == NULL )
                                 return;
                 Node *cur = _root;
                 stack<Node *> s;
                 Node *prev = NULL ;
                 while (cur||!s.empty())
                {
                                 while (cur)
                                {
                                                s.push(cur);
                                                cur = cur->_left;
                                }
                                 Node *tmp = s.top();
                                 if (tmp->_right == NULL || tmp->_right == prev)
                                {
                                                cout << tmp->_data << " " ;
                                                s.pop();
                                                prev = tmp;
                                }
                                 else
                                {
                                                cur = tmp->_right;
                                }
                }
                cout << endl;
                
}
二叉树的深度
size_t BinaryTree <T>::_depth( BinaryNode<T > *root)
{
                 int hleft;
                 int hright;
                 int max;
                 if (root )
                {
                                hleft = _depth( root->_left);
                                hright = _depth( root->_right);
                                max = hleft > hright ? hleft : hright;
                                 return max + 1;

                }
                 else
                {
                                 return 0;
                }
}
二叉树的大小
                 size_t size()
                {
                                 int count = 0;
                                _size(_root,&count);
                                 return count;
                }
void BinaryTree <T>::_size( BinaryNode<T > *root,int *p)
{
                 if (root )
                {
                                ++(* p);
                                _size( root->_left, p );
                                _size( root->_right, p );
                }
                 return ;
}
时间: 2024-10-12 23:20:31

数据结构之二叉树 (构造 拷贝构造 以及前序中序后续三种遍历方法)的相关文章

创建二叉树的两种方法以及三种遍历方法

二叉树的两种创建方法和三种遍历方法 这里的两种创建方法,一种值得是 数据结构上面的创建方法: 方法一 代码如下: 二叉树的结构定义如下: typedef struct BinaryTreeNode{ char value; struct BinaryTreeNode *left; struct BinaryTreeNode *right; }; - c语言版 void CreateBinaryTree(BinaryTreeNode **T) { char data; scanf("%d"

公交车站捡垃圾之二叉树的三种遍历方法

# 二叉树的遍历 今天下午看了二叉树的三种遍历方式,虽然能写出代码,但是理解可能不太到位,感觉很容易忘,所以想到一个形象的方法,把每个节点当作公交车站,而访问节点则是在这个公交车站捡垃圾,右子树和左子树则表示岔路.然后这个捡垃圾的人钟爱左边这个方向,所以一直以左优先.甲乙丙三个人,都爱捡垃圾,但是思考方式不同,所以捡垃圾的方法有点不同. 先序遍历 先序遍历最简单,秉承的原则是,甲很小心谨慎,每次经过公交车站,怕别人捡了,都把垃圾先捡到手,直到左边的路走完了,再往回走,但是回来的过程中,在公交车站

数据结构之二叉树(前序 中序 后续线索话非递归方式)

节点: enum LinkType {                  THREAD,                  LINK }; template<class T> struct ThredBinaryNode {                  ThredBinaryNode *_left;                  ThredBinaryNode *_right;                  LinkType _left_tag;                 

二叉树的前序/中序/后续遍历(递归+非递归)

这几日又把二叉树的递归写了一遍,原来是用C写的,自己写了一个栈,这一次直接用的C++,使用了自带的栈结构.代码如下: 1 /************************************************************************* 2 > Author: Yves 3 > E-mail: [email protected] 4 > File Name: BiTreeNew.cpp 5 > Description: ... 6 > Cre

二叉树遍历,深度有限遍历,广度优先遍历,前序中序后续优先遍历,层次遍历

首先明白两个概念: 1. 深度遍历包括前中后序遍历三种: 2. 广度优先遍历就是层次遍历. PS: 前中后序遍历,如果使用递归遍历,都很简单易理解: 如果使用非递归方式,首先想到的就应该是使用栈结构来控制整个过程,因为递归也是利用栈来实现的: 前中后序遍历的非递归方式中,后序遍历的非递归方式相比较而言,略复杂. 直接上代码: #include "stdlib.h" #include <iostream> #include <stack> #include <

二叉树的实现以及三种遍历方法--代码

1 #include<stdio.h> 2 #include <stdlib.h> 3 4 /*树的数据类型*/ 5 typedef char TElemType; 6 /*定义二叉树的结构*/ 7 typedef struct BiTNode 8 { 9 TElemType data; //数据 10 struct BiTNode *left,*right; //左右孩子节点 11 }BiTNode,*BiTree; 12 13 void createBiTree(BiTree*

数据结构 二叉树的建立及三种遍历方法

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; typedef struct Binode{ char data; struct Binode *lchild,*rchild; }Binode,*Bitree; void CreatTREE(Bitree &T) { char ch; scanf("%c&

二叉树的前序/中序/后序遍历方法的递归与循环的实现

对于二叉树的三种遍历方法, 递归方法实现起来简单,明白.但是效率不好,并且不安全,可能会栈溢出.循环的实现,肯定是基于栈的数据结构来实现,要复杂一些.代码如下: 前序遍历的实现: // 前序遍历 ----基于递归 void PreorderTraversal(BinaryTreeNode* pRoot_) { // 为空时,直接返回了 if (!pRoot_) return; std::cout << pRoot_->m_nValue << " "; Pr

POJ2255 TreeRecovery(二叉树的三种遍历)

Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / \ B E / \ \ A C G / F To recor