二叉樹遍曆演算法集合

來源:互聯網
上載者:User

二叉樹遍曆演算法集合(前中後序遍曆的遞迴和非遞迴演算法,層序遍曆演算法)

費了兩天時間寫的,包括前中後序遍曆的遞迴和非遞迴演算法,還有層序遍曆總共2*3 + 1 = 7中遍曆二叉樹的演算法,感覺其中後序遍曆的非遞迴演算法比較困難,想了很久最後的實現還是不夠優雅,請大家指正~~

總共三個檔案,一個標頭檔,一個對應的cpp檔案,還有一個用於測試的檔案.

標頭檔:

/**//********************************************************************
created:    2006/07/04
filename:     BinaryTree.h
author:        李創
http://www.cppblog.com/converse/

purpose:    示範二叉樹的演算法
*********************************************************************/

#ifndef BinaryTree_H
#define BinaryTree_H

#include <stdlib.h>
#include <stack>

class BinaryTree
{
private:
typedef int Item;
typedef struct TreeNode
{
Item        Node;
TreeNode*   pRight;
TreeNode*   pLeft;

TreeNode(Item node = 0, TreeNode* pright = NULL, TreeNode* pleft = NULL)
: Node(node)
, pRight(pright)
, pLeft(pleft)
{
}

}TreeNode, *PTreeNode;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.