最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++如何實(shí)現(xiàn)二叉樹鏈表

 更新時(shí)間:2022年07月26日 08:46:16   作者:develbai  
這篇文章主要介紹了C++如何實(shí)現(xiàn)二叉樹鏈表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C++二叉樹鏈表

Node.h

#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
class Node
{
public:
? ? Node();
? ? ~Node();
? ? Node *SearchNode(int nodeIndex);
? ? void DeleteNode();
? ? void PreordeTraverse();
? ? void InorderTraverse();
? ? void PostorderTraverse();
? ? int index;
? ? int data;
? ? Node *pLChild;
? ? Node *pRChild;
? ? Node *pParent;
private:
};
Node::Node()
{
? ? index = 0;
? ? data = 0;
? ? pLChild = NULL;
? ? pRChild = NULL;
? ? pParent = NULL;
}
Node::~Node()
{
}
Node *Node::SearchNode(int nodeIndex)
{
? ? Node *temp = NULL;
? ? ? ? if (this->index == nodeIndex)
? ? ? ? {
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? if (this->pLChild != NULL)
? ? ? ? {
? ? ? ? ? ? if (this->pLChild->index == nodeIndex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return this->pLChild;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? temp = this->pLChild->SearchNode(nodeIndex);
? ? ? ? ? ? ? ? if (temp != NULL)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return temp;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (this->pRChild != NULL)
? ? ? ? {
? ? ? ? ? ? if (this->pRChild->index == nodeIndex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return this->pRChild;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this->pRChild->SearchNode(nodeIndex);
? ? ? ? ? ? ? ? if (temp != NULL)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return temp;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? return NULL;
}
void Node::DeleteNode()
{
? ? if (this->pLChild != NULL)
? ? {
? ? ? ? this->pLChild->DeleteNode();
? ? }
? ? if (this->pRChild != NULL)
? ? {
? ? ? ? this->pRChild->DeleteNode();
? ? }
? ? if (this->pParent != NULL)
? ? {
? ? ? ? if (this->pParent->pLChild == this)
? ? ? ? {
? ? ? ? ? ? this->pParent->pLChild = NULL;
? ? ? ? }
? ? ? ? if (this->pParent->pRChild == this)
? ? ? ? {
? ? ? ? ? ? this->pParent->pRChild = NULL;
? ? ? ? }
? ? }
? ? delete this;
}
void Node::PreordeTraverse()
{
? ? cout << this->index << " " << this->data << endl;
? ? if (this->pLChild != NULL)
? ? {
? ? ? ? this->pLChild->PreordeTraverse();
? ? }
? ? if (this->pRChild != NULL)
? ? {
? ? ? ? this->pRChild->PreordeTraverse();
? ? }
}
void Node::InorderTraverse()
{
? ? if (this->pLChild != NULL)
? ? {
? ? ? ? this->pLChild->InorderTraverse();
? ? }
? ? cout << this->index << " " << this->data << endl;
? ? if (this->pRChild != NULL)
? ? {
? ? ? ? this->pRChild->InorderTraverse();
? ? }
}
void Node::PostorderTraverse()
{
? ? if (this->pLChild != NULL)
? ? {
? ? ? ? this->pLChild->PostorderTraverse();
? ? }
? ? if (this->pRChild != NULL)
? ? {
? ? ? ? this->pRChild->PostorderTraverse();
? ? }
? ? cout << this->index << " " << this->data << endl;
}
#endif // !NODE_H

Tree.h

#ifndef TREE_H
#define TREE_H
#include "Node.h"
#include <iostream>
using namespace std;
class Tree
{
public:
? ? Tree(); ? ? ? ? ? ? ? ? ? ? ? ? //創(chuàng)建樹
? ? ~Tree(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //銷毀樹
? ? Node *SearchNode(int nodeIndex); ? ? ? ? ? ? ? ? ? ? ? ?//根據(jù)索引尋找節(jié)點(diǎn)
? ? bool AddNode(int nodeIndex, int direction, Node *pNode); ? ? ? ? ? ?//添加節(jié)點(diǎn)
? ? bool DeleteNode(int nodeIndex, Node *pNode); ? ? ? ? ? ?//刪除節(jié)點(diǎn)
? ? void PreordeTraverse(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //前序遍歷
? ? void InorderTraverse(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //中序遍歷
? ? void PostorderTraverse(); ? ? ? ? ? ? ? ? ? ? ? ? ? //后序遍歷
private:
? ? Node *m_pRoot;
};
Tree::Tree()
{
? ? m_pRoot = new Node();
}
Tree::~Tree()
{
? ? m_pRoot->DeleteNode();
}
Node *Tree::SearchNode(int nodeIndex)
{
? ? return m_pRoot->SearchNode(nodeIndex);
}
bool Tree::AddNode(int nodeIndex, int direction, Node *pNode)
{
? ? Node *temp = SearchNode(nodeIndex);
? ? if (temp != NULL)
? ? {
? ? ? ? Node *currentNode = new Node;
? ? ? ? if (currentNode == NULL)
? ? ? ? {
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? currentNode->index = pNode->index;
? ? ? ? currentNode->data = pNode->data;
? ? ? ? currentNode->pParent = temp;
? ? ? ? if (direction == 0)
? ? ? ? {
? ? ? ? ? ? temp->pLChild = currentNode;
? ? ? ? }
? ? ? ? if (direction == 1)
? ? ? ? {
? ? ? ? ? ? temp->pRChild = currentNode;
? ? ? ? }
? ? ? ? return true;
? ? }
? ? return false;
}
bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
? ? Node *temp = SearchNode(nodeIndex);
? ? if (temp == NULL)
? ? {
? ? ? ? return false;
? ? }
? ? if (pNode != NULL)
? ? {
? ? ? ? pNode->data = temp->data;
? ? }
? ? temp->DeleteNode();
? ? return true;
}
void Tree::PreordeTraverse()
{
? ? m_pRoot->PreordeTraverse();
}
void Tree::InorderTraverse()
{
? ? m_pRoot->InorderTraverse();
}
void Tree::PostorderTraverse()
{
? ? m_pRoot->PostorderTraverse();
}
#endif

main.cpp

#include "Tree.h"
#include "Node.h"
int main()
{
? ? Tree *pTree = new Tree;
? ? Node *e1 = new Node;
? ? e1->index = 1;
? ? e1->data = 1;
? ? Node *e2 = new Node;
? ? e2->index = 2;
? ? e2->data = 2;
? ? Node *e3 = new Node;
? ? e3->index = 3;
? ? e3->data = 3;
? ? Node *e4 = new Node;
? ? e4->index = 4;
? ? e4->data = 4;
? ? Node *e5 = new Node;
? ? e5->index = 5;
? ? e5->data = 5;
? ? Node *e6 = new Node;
? ? e6->index = 6;
? ? e6->data = 6;
? ? Node *e7 = new Node;
? ? e7->index = 7;
? ? e7->data = 7;
? ? Node *e8 = new Node;
? ? e8->index = 8;
? ? e8->data = 8;
? ? pTree->AddNode(0, 0, e1);
? ? pTree->AddNode(0, 1, e2);
? ? pTree->AddNode(1, 0, e3);
? ? pTree->AddNode(1, 1, e4);
? ? pTree->AddNode(2, 0, e5);
? ? pTree->AddNode(2, 1, e6);
? ? pTree->AddNode(3, 0, e7);
? ? pTree->AddNode(4, 1, e8);
? ? //pTree->DeleteNode(2, NULL);
? ? pTree->PreordeTraverse();
? ? cout << endl;
? ? pTree->InorderTraverse();
? ? cout << endl;
? ? pTree->PostorderTraverse();
? ? delete pTree;
? ? system("pause");
? ? return 0;
}

C++二叉樹轉(zhuǎn)鏈表

給定一個(gè)二叉樹,將該二叉樹就地(in-place)轉(zhuǎn)換為單鏈表。單鏈表中節(jié)點(diǎn)順序?yàn)槎鏄淝靶虮闅v順序。

如果不要求就地轉(zhuǎn)鏈表,可以借助于一個(gè)vector將二叉樹轉(zhuǎn)為鏈表。

代碼如下:

#include<vector>
struct TreeNode
{
?int val;
?TreeNode* left;
?TreeNode* right;
?TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
?Solution() {};
?~Solution() {};
?void flatten(TreeNode * root)
?{
? std::vector<TreeNode*> node_vec;
? preorder(root,node_vec);
? for (int i = 1; i < node_vec.size(); i++)
? {
? ?node_vec[i - 1]->left = NULL;
? ?node_vec[i - 1]->right = node_vec[i];
? }
?}
private:
?void preorder(TreeNode* node, std::vector<TreeNode*>& node_vec)
?{
? if (!node)
? {
? ?return;
? }
? node_vec.push_back(node);
? preorder(node->left, node_vec);
? preorder(node->right, node_vec);
?}
};
int main()
{
?TreeNode a(1);
?TreeNode b(2);
?TreeNode c(5);
?TreeNode d(3);
?TreeNode e(4);
?TreeNode f(6);
?a.left = &b;
?a.right = &c;
?b.left = &d;
?b.right = &e;
?c.right = &f;
?Solution solve;
?solve.flatten(&a);
?TreeNode* head = &a;
?while (head)
?{
? if (head->left)
? {
? ?printf("Error\n");
? }
? printf("[%d]", head->val);
? head = head->right;
?}
?printf("\n");
?return 0;
}

運(yùn)行結(jié)果:

[1][2][3][4][5][6]

因?yàn)橐缶偷貙⒍鏄滢D(zhuǎn)為鏈表,因此不能借助于vector。

#include<vector>
struct TreeNode
{
?int val;
?TreeNode* left;
?TreeNode* right;
?TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
?Solution() {};
?~Solution() {};
?void flatten(TreeNode* root)
?{
? TreeNode* last = NULL;
? preorder(root,last);
?}
private:
?void preorder(TreeNode* node, TreeNode* & last)
?{
? if (!node)
? {
? ?return;
? }
? if (!node->left&&!node->right)
? {
? ?last = node;
? ?return;
? }
? TreeNode* left = node->left;
? TreeNode* right = node->right;
? TreeNode* left_last =NULL;
? TreeNode* right_last = NULL;
? if (left)
? {
? ?preorder(left, left_last);
? ?node->left = NULL;
? ?node->right = left;
? ?last = left_last;
? }
? if (right)
? {
? ?preorder(right, right_last);
? ?if (left)
? ?{
? ? left_last->right = right;
? ?}
? ?last = right_last;
? }
?}
};
int main()
{
?TreeNode a(1);
?TreeNode b(2);
?TreeNode c(5);
?TreeNode d(3);
?TreeNode e(4);
?TreeNode f(6);
?a.left = &b;
?a.right = &c;
?b.left = &d;
?b.right = &e;
?c.right = &f;
?Solution solve;
?solve.flatten(&a);
?TreeNode* head = &a;
?while (head)
?{
? if (head->left)
? {
? ?printf("Error\n");
? }
? printf("[%d]", head->val);
? head = head->right;
?}
?printf("\n");
?return 0;
}

運(yùn)行結(jié)果為:

[1][2][3][4][5][6]

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解析C++多文件編程問(wèn)題

    解析C++多文件編程問(wèn)題

    在某些場(chǎng)景中,考慮到編譯效率和可移植性,#pragma once 和 #ifndef 經(jīng)常被結(jié)合使用來(lái)避免頭文件被 重復(fù)引入,這里介紹用 _Pragma 操作符避免頭文件重復(fù)引入的問(wèn)題,感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽(tīng)多個(gè)端口

    c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽(tīng)多個(gè)端口

    這篇文章主要介紹了c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽(tīng)多個(gè)端口的方法,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下
    2021-03-03
  • C/C++中不同數(shù)據(jù)類型之間的轉(zhuǎn)換詳解

    C/C++中不同數(shù)據(jù)類型之間的轉(zhuǎn)換詳解

    這篇文章主要介紹了C/C++中不同數(shù)據(jù)類型之間的轉(zhuǎn)換詳解,數(shù)據(jù)類型轉(zhuǎn)換是計(jì)算機(jī)編程中常見(jiàn)的操作,用于將一個(gè)數(shù)據(jù)類型轉(zhuǎn)換為另一個(gè)數(shù)據(jù)類型,本文將對(duì)不同數(shù)據(jù)類型之間的轉(zhuǎn)換作出說(shuō)明,需要的朋友可以參考下
    2023-10-10
  • while和for可以相互轉(zhuǎn)換的例子分享

    while和for可以相互轉(zhuǎn)換的例子分享

    這篇文章主要介紹了while和for可以相互轉(zhuǎn)換的例子,需要的朋友可以參考下
    2014-02-02
  • C語(yǔ)言宏函數(shù)container of()簡(jiǎn)介

    C語(yǔ)言宏函數(shù)container of()簡(jiǎn)介

    這篇文章介紹了C語(yǔ)言宏函數(shù)container of(),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • C++實(shí)現(xiàn)矩陣對(duì)稱正交化的示例代碼

    C++實(shí)現(xiàn)矩陣對(duì)稱正交化的示例代碼

    這篇文章主要介紹了C++實(shí)現(xiàn)矩陣對(duì)稱正交化,分為python代碼和C++的eigen庫(kù)實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Unity3D實(shí)現(xiàn)經(jīng)典小游戲Pacman

    Unity3D實(shí)現(xiàn)經(jīng)典小游戲Pacman

    這篇文章主要介紹了基于Unity3D制作一做個(gè)經(jīng)典小游戲Pacman,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Unity3D有一定的幫助,感興趣的小伙伴可以了解一下
    2021-12-12
  • C++?STL?iota?和?atoi?用法示例詳解

    C++?STL?iota?和?atoi?用法示例詳解

    atoi是一個(gè)C/C++標(biāo)準(zhǔn)庫(kù)中的函數(shù),用于將一個(gè)以ASCII字符串表示的整數(shù)轉(zhuǎn)換為整數(shù)類型,這篇文章主要介紹了C++?STL?iota?和?atoi?用法,需要的朋友可以參考下
    2024-08-08
  • C語(yǔ)言雙向鏈表的原理與使用操作

    C語(yǔ)言雙向鏈表的原理與使用操作

    雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。本文主要介紹了C語(yǔ)言算法中雙向鏈表的實(shí)現(xiàn),需要的可以參考一下
    2022-05-05
  • C語(yǔ)言小程序 如何判斷兩個(gè)日期之差

    C語(yǔ)言小程序 如何判斷兩個(gè)日期之差

    輸入兩個(gè)日期,計(jì)算之間相差多少天。 用了兩種方法實(shí)現(xiàn),第二種利用結(jié)構(gòu)體,代碼比較清晰,其余的都一樣
    2013-07-07

最新評(píng)論

吐鲁番市| 海伦市| 虞城县| 大港区| 通道| 富平县| 永善县| 海淀区| 茶陵县| 威远县| 嵊州市| 印江| 彭泽县| 云林县| 沂水县| 历史| 阿合奇县| 石棉县| 武穴市| 宝鸡市| 紫阳县| 那坡县| 客服| 田阳县| 胶南市| 林西县| 富源县| 循化| 寻乌县| 札达县| 桐庐县| 独山县| 韶山市| 怀安县| 永泰县| 防城港市| 大荔县| 永川市| 莎车县| 千阳县| 灌南县|