二叉樹的遍歷方法總結與c 實現

2021-10-07 17:24:57 字數 4619 閱讀 8198

二叉樹的遍歷方式分為:

深度遍歷(前序,中序,後序)

廣度遍歷(層次遍歷)

二叉樹是一種非常重要的資料結構,很多其它資料結構都是基於二叉樹的基礎演變而來的。對於二叉樹,有深度遍歷和廣度遍歷,深度遍歷有前序、中序以及後序三種遍歷方法,廣度遍歷即我們平常所說的層次遍歷。因為樹的定義本身就是遞迴定義,因此採用遞迴的方法去實現樹的三種遍歷不僅容易理解而且**很簡潔,而對於廣度遍歷來說,需要其他資料結構的支撐,比如堆了。所以,對於一段**來說,可讀性有時候要比**本身的效率要重要的多。

四種主要的遍歷思想為:

前序遍歷:根結點 ---> 左子樹 ---> 右子樹   (此處所謂的根節點是以當前點作為根節點,並不是指整個二叉樹的根節點)

中序遍歷:左子樹---> 根結點 ---> 右子樹     (此處所謂的根節點是以當前點作為根節點,並不是指整個二叉樹的根節點)

後序遍歷:左子樹 ---> 右子樹 ---> 根結點    (此處所謂的根節點是以當前點作為根節點,並不是指整個二叉樹的根節點)

層次遍歷:只需按層次遍歷即可

例如,求下面二叉樹的各種遍歷

前序遍歷:1  2  4  5  7  8  3  6 

中序遍歷:4  2  7  5  8  1  3  6

後序遍歷:4  7  8  5  2  6  3  1

層次遍歷:1  2  3  4  5  6  7  8

bstree.h

/**

* c++ 語言: 二叉查詢樹

* * @author skywang

* @date 2013/11/07

*/#ifndef _binary_search_tree_hpp_

#define _binary_search_tree_hpp_

#include #include using namespace std;

template class bstnode

};template class bstree ;

/* * 建構函式

*/template bstree::bstree():mroot(null)

/* * 析構函式

*/template bstree::~bstree()

/* * 前序遍歷"二叉樹"

*/template void bstree::preorder(bstnode* tree) const

}template void bstree::preorder()

/* * 中序遍歷"二叉樹"

*/template void bstree::inorder(bstnode* tree) const

}template void bstree::inorder()

/* * 後序遍歷"二叉樹"

*/template void bstree::postorder(bstnode* tree) const

}template void bstree::postorder()

/* * (遞迴實現)查詢"二叉樹x"中鍵值為key的節點

*/template bstnode* bstree::search(bstnode* x, t key) const

template bstnode* bstree::search(t key)

/* * (非遞迴實現)查詢"二叉樹x"中鍵值為key的節點

*/template bstnode* bstree::iterativesearch(bstnode* x, t key) const

return x;

}template bstnode* bstree::iterativesearch(t key)

/* * 查詢最小結點:返回tree為根結點的二叉樹的最小結點。

*/template bstnode* bstree::minimum(bstnode* tree)

template t bstree::minimum()

/* * 查詢最大結點:返回tree為根結點的二叉樹的最大結點。

*/template bstnode* bstree::maximum(bstnode* tree)

template t bstree::maximum()

/* * 找結點(x)的後繼結點。即,查詢"二叉樹中資料值大於該結點"的"最小結點"。

*/template bstnode* bstree::successor(bstnode*x)

return y;}/*

* 找結點(x)的前驅結點。即,查詢"二叉樹中資料值小於該結點"的"最大結點"。

*/template bstnode* bstree::predecessor(bstnode*x)

return y;}/*

* 將結點插入到二叉樹中

* * 引數說明:

* tree 二叉樹的根結點

* z 插入的結點

*/template void bstree::insert(bstnode* &tree, bstnode* z)

z->parent = y;

if (y==null)

tree = z;

else if (z->key < y->key)

y->left = z;

else

y->right = z;}/*

* 將結點(key為節點鍵值)插入到二叉樹中

* * 引數說明:

* tree 二叉樹的根結點

* key 插入結點的鍵值

*/template void bstree::insert(t key)

/* * 刪除結點(z),並返回被刪除的結點

* * 引數說明:

* tree 二叉樹的根結點

* z 刪除的結點

*/template bstnode* bstree::remove(bstnode* &tree, bstnode*z)

/* * 刪除結點(z),並返回被刪除的結點

* * 引數說明:

* tree 二叉樹的根結點

* z 刪除的結點

*/template void bstree::remove(t key)

/* * 銷毀二叉樹

*/template void bstree::destroy(bstnode* &tree)

template void bstree::destroy()

/* * 列印"二叉查詢樹"

* * key -- 節點的鍵值

* direction -- 0,表示該節點是根節點;

* -1,表示該節點是它的父結點的左孩子;

* 1,表示該節點是它的父結點的右孩子。

*/template void bstree::print(bstnode* tree, t key, int direction)

}template void bstree::print()

#endif

main.cpp

/**

* c++ 語言: 二叉查詢樹

* * @author skywang

* @date 2013/11/07

*/#include #include "bstree.h"

using namespace std;

static int arr= ;

#define tbl_size(a) ( (sizeof(a)) / (sizeof(a[0])) )

int main()

cout << "\n== 前序遍歷: ";

tree->preorder();

cout << "\n== 中序遍歷: ";

tree->inorder();

cout << "\n== 後序遍歷: ";

tree->postorder();

cout << endl;

cout << "== 最小值: " << tree->minimum() << endl;

cout << "== 最大值: " << tree->maximum() << endl;

cout << "== 樹的詳細資訊: " << endl;

tree->print();

cout << "\n== 刪除根節點: " << arr[3];

tree->remove(arr[3]);

cout << "\n== 中序遍歷: ";

tree->inorder();

cout << endl;

// 銷毀二叉樹

tree->destroy();

return 0;

}

參考文章:

二叉樹遍歷(C 實現)

二叉樹3種深度優先遍歷 遞迴 非遞迴 層次遍歷,最簡潔 最好記!include include includeusing namespace std 節點定義 struct node 先序遍歷 遞迴 void pre order recursive node root 中序遍歷 遞迴 void mi...

二叉樹遍歷 c實現

這裡主要是三種遍歷,先序 preorder,nlr 中序 inorder,lnr 後序 postorder,lrn n node,l left,r right 基本排序 先序 nlr,節點,左,右 中序 lnr,左,節點,右 後序 lrn,左,右,節點 要點 在每一種排序裡,必須遵守基本排序。看圖 ...

二叉樹的遍歷 二叉樹遍歷與儲存

在資料結構中,二叉樹是非常重要的結構。例如 資料庫中經常用到b 樹結構。那麼資料庫是如何去單個查詢或者範圍查詢?首先得理解二叉樹的幾種遍歷順序 先序 中序 後序 層次遍歷。先序 根節點 左子樹 右子樹 中序 左子樹 根節點 右子樹 後序 左子樹 右子樹 根節點 按層級 class node if c...