1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      線索二叉樹的前序、中序

          二叉樹是一種非線性結(jié)構(gòu),遍歷二叉樹幾乎都是通過遞歸或者用棧輔助實現(xiàn)非遞歸的遍歷。用二叉樹作為存儲結(jié)構(gòu)時,取到一個節(jié)點,只能獲取節(jié)點的左孩子和右孩子,不能直接得到節(jié)點的任一遍歷序列的前驅(qū)或者后繼。

      創(chuàng)新互聯(lián)企業(yè)建站,10年網(wǎng)站建設(shè)經(jīng)驗,專注于網(wǎng)站建設(shè)技術(shù),精于網(wǎng)頁設(shè)計,有多年建站和網(wǎng)站代運營經(jīng)驗,設(shè)計師為客戶打造網(wǎng)絡(luò)企業(yè)風(fēng)格,提供周到的建站售前咨詢和貼心的售后服務(wù)。對于成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)中不同領(lǐng)域進(jìn)行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設(shè)中充分了解客戶行業(yè)的需求,以靈動的思維在網(wǎng)頁中充分展現(xiàn),通過對客戶行業(yè)精準(zhǔn)市場調(diào)研,為客戶提供的解決方案。

          而線索二叉樹利用二叉樹中指向左右子樹的空指針來存放節(jié)點的前驅(qū)和后繼信息

      結(jié)點信息如下

      enum PointerTag{ THREAD, LINK };
      
      template
      struct BinaryTreeNodeThd
      {
      	T _data;                      //數(shù)據(jù)
      	BinaryTreeNodeThd* _left;  //左孩子
      	BinaryTreeNodeThd* _right; //右孩子
      	PointerTag _leftTag;          //左孩子線索標(biāo)志
      	PointerTag _rightTag;         //右孩子線索標(biāo)志
      };

      其前序結(jié)構(gòu)如下

      線索二叉樹的前序、中序

      其中序結(jié)構(gòu)如下

      線索二叉樹的前序、中序

      程序?qū)崿F(xiàn):

      #include
      
      using namespace std;
      
      enum PointerTag{ THREAD, LINK };
      
      template
      struct BinaryTreeNodeThd
      {
      	T _data;                      //數(shù)據(jù)
      	BinaryTreeNodeThd* _left;  //左孩子
      	BinaryTreeNodeThd* _right; //右孩子
      	PointerTag _leftTag;          //左孩子線索標(biāo)志
      	PointerTag _rightTag;         //右孩子線索標(biāo)志
      	
      	BinaryTreeNodeThd(const T& x)
      		:_data(x)
      		, _left(NULL)
      		, _right(NULL)
      		, _leftTag(LINK)
      		, _rightTag(LINK)
      	{}
      };
      
      template
      class BinaryTreeThd
      {
      	typedef BinaryTreeNodeThd Node;
      public:
      	BinaryTreeThd()
      		:_root(NULL)
      	{}
      	BinaryTreeThd(const T*a, size_t size, const T& invalid)
      	{
      		size_t index = 0;
      		_root = _CreateTree(a, size, index, invalid);
      	}
      	void InOrderThreading()//中序線索化
      	{
      		Node*prev = NULL;
      		_InOrderThreading(_root, prev);
      	}
      	void PrevOderThreading()//前序線索化
      	{
      		Node*prev = NULL;
      		_PrevOderThreading(_root, prev);
      	}
      		
      	void InOrderThd()//中序遍歷
      	{
      		_InOrderThd(_root);
      	}
      	void PrevOrderThd()//前序遍歷
      	{
      	_PrevOrderThd(_root);
      	}
      protected:
      	Node* _CreateTree(const T*a, size_t size, size_t& index, const T& invalid)
      	{
      		Node* _root = NULL;
      		
      		if (index < size&&a[index] != invalid)
      		{
      			_root = new Node(a[index]);
      			_root->_left = _CreateTree(a, size, ++index, invalid);
      			_root->_right = _CreateTree(a, size, ++index, invalid);
      		}
      		return _root;
      	}
      	
      	void _PrevOderThreading(Node* root, Node*& prev)//前序線索化
      	{
      		if (root == NULL)
      			return;
      		
      		if (root->_left == NULL)
      		{
      			root->_leftTag = THREAD;
      			root->_left	= prev;
      		}
      		
      		if (prev&&prev->_right == NULL)
      		{
      			prev->_rightTag = THREAD;
      			prev->_right = root;
      		}
      		prev = root;
      		if (root->_leftTag == LINK)//遞歸
      		{
      			_PrevOderThreading(root->_left,prev);//線索化左子樹
      		}
      
      		if (root->_rightTag == LINK)
      		{
      			_PrevOderThreading(root->_right,prev);//線索化右子樹
      		}
      	}
      	
      	void _PrevOrderThd(Node* root)
      	{
      		Node*cur = root;
      		
      		while (cur)
      		{
      			while (cur->_leftTag == LINK)
      			{
      				cout << cur->_data << " ";
      				cur = cur->_left;
      			}
      			cout << cur->_data << " ";
      			cur = cur->_right;
      		}
      	}
      	/*方法二
      	void _PrevOrderThd(Node* root)
      	{
      		Node*cur = root;
      		
      		while (cur)
      		{
      			while (cur->_leftTag==LINK)
      			{
      				cout << cur->_data << " ";
      				cur = cur->_left;
      			}
      			cout << cur->_data << " ";
      			while (cur->_rightTag == THREAD)
      			{
      				cur = cur->_right;
      				cout << cur->_data << " ";
      			}
      			if (cur->_leftTag == LINK)
      			{
      				cur = cur->_left;
      			}
      			else
      			{
      				cur = cur->_right;
      			}
      		}
      	}*/
      	void _InOrderThreading(Node* _root, Node* &prev)//中序線索化
      	{
      		if (_root == NULL)
      		{
      			return;
      		}
      		if (_root->_leftTag==LINK)
      			_InOrderThreading(_root->_left,prev);
      		//線索化
      		if (_root->_left == NULL)//左孩子為空
      		{
      			_root->_leftTag = THREAD;
      			_root->_left = prev;
      		}
      		if (prev != NULL&&prev->_right == NULL)//前驅(qū)的右孩子為空
      		{
      			prev->_rightTag = THREAD;
      			prev->_right = _root;
      		}
      		prev = _root;
      
      		if (_root->_rightTag==LINK)//線索化右孩子
      			_InOrderThreading(_root->_right,prev);
      	}
      
      	void _InOrderThd(Node* _root)                  //中序遍歷
      	{
      		Node* cur = _root;
      		while (cur)
      		{
      			while (cur->_leftTag == LINK)
      			{
      				cur = cur->_left;
      			}
      			cout << cur->_data << " ";
      			while (cur->_rightTag == THREAD)
      			{
      				cur = cur->_right;
      				cout << cur->_data << " ";
      			}
      			cur = cur->_right;
      		}
      		cout << endl;
      	}
      protected:
      	Node* _root;
      };

      測試

      int main()
      {
      	int a1[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
      	BinaryTreeThd t1(a1, 10, '#');
      	cout << endl << "中序遍歷:";
      	t1.InOrderThreading();
      	t1.InOrderThd();
      	cout << "前序遍歷" << endl;
      	BinaryTreeThdt2(a1, 10, '#');
      	t2.PrevOderThreading();
      	t2.PrevOrderThd();
      	getchar();
      	return 0;
      }

      網(wǎng)站名稱:線索二叉樹的前序、中序
      分享地址:http://www.ef60e0e.cn/article/pdhccs.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        隆尧县| 沙洋县| 万宁市| 陆丰市| 凤山市| 石城县| 蚌埠市| 庆城县| 白城市| 巴中市| 墨玉县| 舟曲县| 朝阳区| 齐齐哈尔市| 云林县| 磐石市| 清丰县| 新营市| 孟津县| 锦屏县| 高雄县| 怀柔区| 南投市| 塘沽区| 高阳县| 竹北市| 班戈县| 九台市| 湘乡市| 阳原县| 南汇区| 富平县| 波密县| 当阳市| 沅江市| 丰都县| 都兰县| 奎屯市| 五大连池市| 和平区| 将乐县|