二叉樹線索化以及線索化的先序、中序、後序遍歷

首先,什麼是二叉樹的線索化,爲什麼要對二叉樹線索化?

二叉樹是一種非線性結構,遍歷二叉樹幾乎都是通過遞歸或者用棧輔助實現非遞歸的遍歷。用二叉樹作爲存儲結構時,取到一個節點,只能獲取節點的左孩子和右孩子,不能直接得到節點的任一遍歷序列的前驅或者後繼。
爲了保存這種在遍歷中需要的信息,我們利用二叉樹中指向左右子樹的空指針來存放節點的前驅和後繼信息

n個節點的二叉樹中含有n+1個空指針域。利用二叉樹中的空指針域 來存放在某種遍歷次序下的前驅和後繼 ,這種指針叫“線索”。這種加上了線索的二叉樹稱爲線索二叉樹。

根據線索的性質的不同,線索二叉樹分爲:前序線索二叉樹 , 中序線索二叉樹 , 後序線索二叉樹(本篇博客主要講述前面兩種 , 後面會專門對後序線索二叉樹分析 )


                            線索二叉樹節點       
typedef enum  
2.{  
3.    Link,    
4.    Thread  
5.} PointTag;  
6.  
7.typedef struct  BinaryTreeNode  
8.{  
9.    BinaryTreeNode(const char data)  
10.    :_data(data)  
11.    , pLeft(NULL)  
12.    , pRight(NULL)  
13.    , Ltag(Link)  
14.    , Rtag(Link)  
15.    {}  
16.    char _data;  
17.    struct  BinaryTreeNode *pLeft , * pRight;  
18.    PointTag  Ltag, Rtag;   
19.}BiTreeNode;  
 Ltag  標記是否有左子樹  ,  Ltag 是Link 則表示有左子樹 ; Ltag是Thread表示沒有左子樹(有前驅)

 Rtag 標記是否有右子樹  ,  Rtag是Link 則表示有右子樹 ; Rtag是Thread 表示沒有右子樹(有後繼)

首先,在線索化二叉樹之前,你得有二叉樹吧驚訝 還是 按之前的方式:先序創建二叉樹

void  _CreatTree(BiTreeNode*& Root, const char* arr, size_t size, size_t& index)  
2.    {  
3.        if (arr == NULL || size == 0)  
4.        {  
5.            cout << "輸入有誤 " << endl;  
6.            return;  
7.        }  
8.        if (index < size && arr[index] != '#')  
9.        {  
10.            Root = new BiTreeNode(arr[index]);  
11.            _CreatTree(Root->pLeft, arr, size, ++index);  
12.            _CreatTree(Root->pRight, arr, size, ++index);  
13.        }  
14.    }  

以下面的二叉樹爲例:


1、先序線索化二叉樹和遍歷
先序遍歷的順序:0 1 2 3 4

先序線索化二叉樹

分析:首先對於左子樹存在的節點,就不會有前驅;同樣,節點的右子樹存在,那麼不存在後繼。那麼我就先一直找尋節的左子樹,判斷左右子樹是否爲空 , 爲空了纔會考慮前驅和後繼的問題。後繼倒是很好找(反正要遍歷二叉樹),那麼前驅的位置呢?如果遇到左子樹不存在的節點,怎麼才能找到已經已經掃面過得節點呢?所以我們需要創建一個節點指針,每次記住上一次掃描的節點 。 對於右子樹就很好辦了 , 如果當前節點的前一個節點不爲空且右子樹不存在,那麼我們就放心大膽的鏈上吧 。然後循環得了。


假設 記錄前一個節點的指針爲:

  1. BiTreeNode* Prev =NULL;  
BiTreeNode* Prev =NULL;

沒聽懂描述麼? 沒關係,如果所示:


上代碼

  1. void _PreOrderThreading(BiTreeNode*& Root)  
  2.     {  
  3.         if (Root == NULL)  
  4.         {  
  5.             return;  
  6.         }  
  7.         if (Root->pLeft == NULL) //沒有左子樹  
  8.         {  
  9.              Root->pLeft = Prev;   //前驅  
  10.             Root->Ltag = Thread;  
  11.         }  
  12.         if (Prev != NULL && Prev->pRight == NULL) // 上一個節點有沒有  右子樹  
  13.         {  //Prev第一次進來 爲空    
  14.             Prev->pRight = Root;   //後繼  
  15.             Prev->Rtag = Thread;  
  16.         }  
  17.         Prev = Root;//前驅  , 每次記住上次的節點  
  18.   
  19.         //判斷Root是否有左右孩子  
  20.         if (Root->Ltag == Link)  
  21.             _PreOrderThreading(Root->pLeft);  
  22.         if (Root->Rtag == Link)  
  23.             _PreOrderThreading(Root->pRight);  
  24.     }  
void _PreOrderThreading(BiTreeNode*& Root)
	{
		if (Root == NULL)
		{
			return;
		}
		if (Root->pLeft == NULL) //沒有左子樹
		{
			 Root->pLeft = Prev;   //前驅
			Root->Ltag = Thread;
		}
		if (Prev != NULL && Prev->pRight == NULL) // 上一個節點有沒有  右子樹
		{  //Prev第一次進來 爲空  
			Prev->pRight = Root;   //後繼
			Prev->Rtag = Thread;
		}
		Prev = Root;//前驅  , 每次記住上次的節點

		//判斷Root是否有左右孩子
		if (Root->Ltag == Link)
			_PreOrderThreading(Root->pLeft);
		if (Root->Rtag == Link)
			_PreOrderThreading(Root->pRight);
	}


對於先序線索二叉樹,我想提醒的是:每次只會把當前節點的左子樹前驅鏈上,這一次的 後繼 不會在本次鏈上,當pCur指向下一個節點的時候,纔會把上一次的後繼鏈上

結果


那麼 怎麼 寫先序遍歷線索二叉樹

  1. void _PreOrder(BiTreeNode* Root)  
  2.     {  
  3.         if (Root == NULL)  
  4.         {  
  5.             return;  
  6.         }  
  7.         BiTreeNode* pCur = Root;  
  8.         while (pCur != NULL)  
  9.         {  
  10.             while (pCur->pLeft != NULL && pCur->Ltag == Link)//找到最左邊的節點,左標記一直爲Link  
  11.             {  
  12.                 cout << pCur->_data << ' ';  
  13.                 pCur = pCur->pLeft;  
  14.             }  
  15.             //到這來,左邊的的節點還沒有訪問  
  16.             cout << pCur->_data << ' ';  
  17.   
  18.             if (pCur->Ltag == Thread)//遇到線索 就看右節點  
  19.             {  
  20.                 pCur = pCur->pRight;  
  21.             }  
  22.             while (pCur != NULL)//循環右節點  
  23.             {  
  24.                 if (pCur->pLeft != NULL && pCur->Ltag == Link)//遇到左節點存在 , 則訪問  
  25.                 {  
  26.                     break;  
  27.                 }  
  28.                 cout << pCur->_data << ' ';  
  29.                 pCur = pCur->pRight;  
  30.             }  
  31.         }  
  32.     }  
void _PreOrder(BiTreeNode* Root)
	{
		if (Root == NULL)
		{
			return;
		}
		BiTreeNode* pCur = Root;
		while (pCur != NULL)
		{
			while (pCur->pLeft != NULL && pCur->Ltag == Link)//找到最左邊的節點,左標記一直爲Link
			{
				cout << pCur->_data << ' ';
				pCur = pCur->pLeft;
			}
			//到這來,左邊的的節點還沒有訪問
			cout << pCur->_data << ' ';

			if (pCur->Ltag == Thread)//遇到線索 就看右節點
			{
				pCur = pCur->pRight;
			}
			while (pCur != NULL)//循環右節點
			{
				if (pCur->pLeft != NULL && pCur->Ltag == Link)//遇到左節點存在 , 則訪問
				{
					break;
				}
				cout << pCur->_data << ' ';
				pCur = pCur->pRight;
			}
		}
	}

代碼解釋:



2、中序線索化二叉樹和遍歷

中序遍歷的順序:213 0 4

中序遍歷線索化二叉樹

分析:還是和先序很像的 ,中序的順序是左-根-右,我們同樣可以使用上面的遞歸方式;

話不多說,上代碼:

  1. void _InOrderThreading(BiTreeNode*& Root)  
  2.     {  
  3.         if (Root == NULL)  
  4.         {  
  5.             return;  
  6.         }  
  7.         _InOrderThreading(Root->pLeft);    // 左  
  8.                   
  9.         if (Root->pLeft == NULL) //根  
  10.         {  
  11.             Root->Ltag = Thread;  
  12.             Root->pLeft = Prev;  
  13.         }  
  14.         if (Prev != NULL && Prev->pRight == NULL)  
  15.         {  
  16.             Prev->pRight = Root;  
  17.             Prev->Rtag = Thread;  
  18.         }  
  19.         Prev = Root;  
  20.         _InOrderThreading(Root->pRight);   //右  
  21.     }  
void _InOrderThreading(BiTreeNode*& Root)
	{
		if (Root == NULL)
		{
			return;
		}
		_InOrderThreading(Root->pLeft);    // 左
				
		if (Root->pLeft == NULL)	//根
		{
			Root->Ltag = Thread;
			Root->pLeft = Prev;
		}
		if (Prev != NULL && Prev->pRight == NULL)
		{
			Prev->pRight = Root;
			Prev->Rtag = Thread;
		}
		Prev = Root;
		_InOrderThreading(Root->pRight);   //右
	}

中序遍歷線索二叉樹

  1. void _InOrder(BiTreeNode* Root)  
  2.     {  
  3.         if (Root == NULL)  
  4.         {  
  5.             return;  
  6.         }  
  7.         BiTreeNode* pCur = Root;  
  8.         while (pCur )  
  9.         {  
  10.             while (pCur->Ltag == Link) //找最左邊的節點  
  11.             {  
  12.                 pCur = pCur->pLeft;  
  13.             }  
  14.             cout << pCur->_data << ' ';  
  15.             while ( pCur && pCur->Rtag == Thread )//找中序後繼節點  
  16.             {  
  17.                 pCur = pCur->pRight;  
  18.                 cout << pCur->_data << ' ';  
  19.             }  
  20.             //沒有後繼,有右子樹     
  21.             pCur = pCur->pRight;  
  22.         }  
  23.     }  
void _InOrder(BiTreeNode* Root)
	{
		if (Root == NULL)
		{
			return;
		}
		BiTreeNode* pCur = Root;
		while (pCur )
		{
			while (pCur->Ltag == Link) //找最左邊的節點
			{
				pCur = pCur->pLeft;
			}
			cout << pCur->_data << ' ';
			while ( pCur && pCur->Rtag == Thread )//找中序後繼節點
			{
				pCur = pCur->pRight;
				cout << pCur->_data << ' ';
			}
			//沒有後繼,有右子樹   
			pCur = pCur->pRight;
		}
	}

其實,中序遍歷和先序遍歷二叉樹還是很像的,首先按照中序遍歷的順序,找到二叉樹的最左邊的節點,判斷是否有前驅,有則遍歷訪問,沒有則看右子樹和後繼的情況。

此處,可以按照之前的畫圖過程繼續一步步來。


全部代碼:

  1. #define _CRT_SECURE_NO_WARNINGS 1  
  2. #include<iostream>  
  3. using namespace std;  
  4. #include<assert.h>  
  5.   
  6. //線索二叉樹   
  7. typedef enum  
  8. {  
  9.     Link,  
  10.     Thread  
  11. } PointTag;  
  12.   
  13. typedef struct  BinaryTreeNode  
  14. {  
  15.     BinaryTreeNode(const char data)  
  16.     :_data(data)  
  17.     , pLeft(NULL)  
  18.     , pRight(NULL)  
  19.     , Ltag(Link)  
  20.     , Rtag(Link)  
  21.     {}  
  22.     char _data;  
  23.     struct  BinaryTreeNode *pLeft , * pRight;  
  24.     PointTag  Ltag, Rtag;  
  25. }BiTreeNode;  
  26.   
  27. class Thread_BiTree  
  28. {  
  29. public:  
  30.     //先序 --創建樹  
  31.     Thread_BiTree(const char* arr, size_t Size)  
  32.         :_pRoot(NULL)  
  33.         , Prev(NULL)  
  34.     {  
  35.         size_t index = 0;  
  36.         _CreatTree(_pRoot, arr, Size , index);//創建二叉樹  
  37.     }  
  38. protected:  
  39.     void  _CreatTree(BiTreeNode*& Root, const char* arr, size_t size, size_t& index)  
  40.     {  
  41.         if (arr == NULL || size == 0)  
  42.         {  
  43.             cout << "輸入有誤 " << endl;  
  44.             return;  
  45.         }  
  46.         if (index < size && arr[index] != '#')  
  47.         {  
  48.             Root = new BiTreeNode(arr[index]);  
  49.             _CreatTree(Root->pLeft, arr, size, ++index);  
  50.             _CreatTree(Root->pRight, arr, size, ++index);  
  51.         }  
  52.     }  
  53. public:  
  54.     //先序--線索化二叉樹  
  55.     void PreOrderThreading()  
  56.     {  
  57.         _PreOrderThreading(this->_pRoot);  
  58.     }  
  59.     //先序--遍歷 線索二叉樹  
  60.     void PreOrder()  
  61.     {  
  62.         _PreOrder(this->_pRoot);  
  63.     }  
  64. protected:  
  65.     //先序--線索化二叉樹--C    
  66.     //思路:先看左子樹, 找下一個節點的時候,在檢測上一個節點的右節點  
  67.     void _PreOrderThreading(BiTreeNode*& Root)  
  68.     {  
  69.         if (Root == NULL)  
  70.         {  
  71.             return;  
  72.         }  
  73.         if (Root->pLeft == NULL) //沒有左子樹  
  74.         {  
  75.              Root->pLeft = Prev;   //前驅  
  76.             Root->Ltag = Thread;  
  77.         }  
  78.         if (Prev != NULL && Prev->pRight == NULL) // 上一個節點有沒有  右子樹  
  79.         {  //Prev第一次進來 爲空    
  80.             Prev->pRight = Root;   //後繼  
  81.             Prev->Rtag = Thread;  
  82.         }  
  83.         Prev = Root;//前驅  , 每次記住上次的節點  
  84.   
  85.         //判斷Root是否有左右孩子  
  86.         if (Root->Ltag == Link)  
  87.             _PreOrderThreading(Root->pLeft);  
  88.         if (Root->Rtag == Link)  
  89.             _PreOrderThreading(Root->pRight);  
  90.     }  
  91.     //先序--遍歷 線索二叉樹--C  
  92.     void _PreOrder(BiTreeNode* Root)  
  93.     {  
  94.         if (Root == NULL)  
  95.         {  
  96.             return;  
  97.         }  
  98.         BiTreeNode* pCur = Root;  
  99.         while (pCur != NULL)  
  100.         {  
  101.             while (pCur->pLeft != NULL && pCur->Ltag == Link)//找到最左邊的節點,左標記一直爲Link  
  102.             {  
  103.                 cout << pCur->_data << ' ';  
  104.                 pCur = pCur->pLeft;  
  105.             }  
  106.             //到這來,左邊的的節點還沒有訪問  
  107.             cout << pCur->_data << ' ';  
  108.   
  109.             if (pCur->Ltag == Thread)//遇到線索 就看右節點  
  110.             {  
  111.                 pCur = pCur->pRight;  
  112.             }  
  113.             while (pCur != NULL)//循環右節點  
  114.             {  
  115.                 if (pCur->pLeft != NULL && pCur->Ltag == Link)//遇到左節點存在 , 則訪問  
  116.                 {  
  117.                     break;  
  118.                 }  
  119.                 cout << pCur->_data << ' ';  
  120.                 pCur = pCur->pRight;  
  121.             }  
  122.         }  
  123.     }  
  124.   
  125. public:  
  126.     //中序--線索化二叉樹  
  127.     void InOrderThreading()  
  128.     {  
  129.         _InOrderThreading(_pRoot);  
  130.     }  
  131.     //中序--遍歷線索二叉樹  
  132.     void InOrder()  
  133.     {  
  134.         _InOrder(this->_pRoot);  
  135.     }  
  136. protected:  
  137.     //中序--線索化二叉樹--C  
  138.     //思路:按 左-根-右的順序   先找到最左邊的節點-> 和先序線索一樣 ,先鏈接左子樹,執行到下一個節點在看上次節點的右子樹 -> 右子樹  
  139.     void _InOrderThreading(BiTreeNode*& Root)  
  140.     {  
  141.         if (Root == NULL)  
  142.         {  
  143.             return;  
  144.         }  
  145.         _InOrderThreading(Root->pLeft);    // 左  
  146.                   
  147.         if (Root->pLeft == NULL) //根  
  148.         {  
  149.             Root->Ltag = Thread;  
  150.             Root->pLeft = Prev;  
  151.         }  
  152.         if (Prev != NULL && Prev->pRight == NULL)  
  153.         {  
  154.             Prev->pRight = Root;  
  155.             Prev->Rtag = Thread;  
  156.         }  
  157.         Prev = Root;  
  158.         _InOrderThreading(Root->pRight);   //右  
  159.     }  
  160.     //中序--遍歷二叉樹--C  
  161.     //思路:找到中序開始的節點(最左邊的節點)-> (後繼 )它的根節點,若沒有則找右節點  
  162.     void _InOrder(BiTreeNode* Root)  
  163.     {  
  164.         if (Root == NULL)  
  165.         {  
  166.             return;  
  167.         }  
  168.         BiTreeNode* pCur = Root;  
  169.         while (pCur )  
  170.         {  
  171.             while (pCur->Ltag == Link) //找最左邊的節點  
  172.             {  
  173.                 pCur = pCur->pLeft;  
  174.             }  
  175.             cout << pCur->_data << ' ';  
  176.   
  177.             while ( pCur && pCur->Rtag == Thread )//找中序後繼節點  
  178.             {  
  179.                 pCur = pCur->pRight;  
  180.                 cout << pCur->_data << ' ';  
  181.             }  
  182.             //沒有後繼,有右子樹     
  183.             pCur = pCur->pRight;  
  184.         }  
  185.     }  
  186. public:  
  187.     //後序--線索二叉樹  
  188.     void PostOrderThreading()  
  189.     {  
  190.         _PostOrderThreading(_pRoot);  
  191.     }  
  192. protected:  
  193.     //後序--線索二叉數--C  
  194.     //思路:左-右-根  和前面的一樣  
  195.     void _PostOrderThreading(BiTreeNode*& Root)  
  196.     {  
  197.         if (Root == NULL)  
  198.         {  
  199.             return;  
  200.         }  
  201.         _PostOrderThreading(Root->pLeft);  
  202.         _PostOrderThreading(Root->pRight);  
  203.   
  204.         if (Root->pLeft == NULL)  
  205.         {  
  206.             Root->pLeft = Prev;  
  207.             Root->Ltag = Thread;  
  208.         }  
  209.         if (Prev != NULL && Prev->pRight == NULL)  
  210.         {  
  211.             Prev->pRight = Root;  
  212.             Prev->Rtag = Thread;  
  213.         }  
  214.         Prev = Root;  
  215.     }  
  216. private:  
  217.     BiTreeNode* _pRoot;  
  218.     BiTreeNode* Prev;  //記錄  
  219. };  
  220.   
  221. int main()  
  222. {  
  223.     //char * arr = "013##4##25##6##";  
  224.     char * arr = "012##3##4##";  
  225.     Thread_BiTree tree(arr, strlen(arr));  
  226.   
  227.     tree.PreOrderThreading();            //先序線索化  
  228.     tree.PreOrder();                   //遍歷先序線索二叉樹  
  229.     cout << endl << "------------------------" << endl;  
  230.   
  231.     char * arr1 = "013##4##25##6##";  
  232.     Thread_BiTree tree1(arr1, strlen(arr1));  
  233.     tree1.InOrderThreading();          //中序線索化  
  234.     tree1.InOrder();                //遍歷中序線索二叉樹  
  235.     cout << endl << "------------------------" << endl;  
  236.   
  237.     char * arr2 = "013##4##25##6##";  
  238.     Thread_BiTree tree2(arr2, strlen(arr2));  
  239.     tree2.PostOrderThreading();  
  240.     tree2.PostOrder();  
  241.     cout << endl << "------------------------" << endl;  
  242.     return 0;  
  243. }  


發佈了134 篇原創文章 · 獲贊 240 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章