数据结构探险——树篇
生活随笔
收集整理的這篇文章主要介紹了
数据结构探险——树篇
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以下內(nèi)容源于慕課網(wǎng)的學(xué)習(xí)整理,如有侵權(quán),請(qǐng)告知?jiǎng)h除。
樹存在概念中,是對(duì)數(shù)組或者鏈表的一種操作方式的概念。
一、與樹有關(guān)的一些基礎(chǔ)概念
(1)樹
- 有限節(jié)點(diǎn)的集合;
(2)度
- 某個(gè)節(jié)點(diǎn)的直接孩子數(shù)目;
(3)葉節(jié)點(diǎn)
- 終端節(jié)點(diǎn)
(4)祖先
- 所有在它之上的節(jié)點(diǎn)
(5)深度
- 節(jié)點(diǎn)的深度(節(jié)點(diǎn)所處的位置)
- 樹的深度(整棵樹的深度)
(6)二叉樹
- 所有節(jié)點(diǎn)的度都小于等于2
(7)二叉樹的遍歷
- 前中后,是針對(duì)“根”來(lái)說(shuō)的。
(8)作用實(shí)例
- 人機(jī)對(duì)戰(zhàn)
二、二叉樹的數(shù)組實(shí)現(xiàn)
1、換算公式
- 父節(jié)點(diǎn)下標(biāo)*2+1,得到,父節(jié)點(diǎn)的左孩子節(jié)點(diǎn)的下標(biāo);
- 父節(jié)點(diǎn)下標(biāo)*2+2,得到,父節(jié)點(diǎn)的右孩子節(jié)點(diǎn)的下標(biāo)
2、示意圖
- 括號(hào)里面表示的是索引,或者說(shuō)節(jié)點(diǎn)序號(hào),外面的是數(shù)據(jù)。
- 慕課網(wǎng)中只是按照這個(gè)圖,來(lái)進(jìn)行查找(遍歷查找)、插入(已經(jīng)確定在哪個(gè)位置上出入了)、刪除(刪除后賦值為0)等操作。
三、二叉樹的鏈表實(shí)現(xiàn)
1、節(jié)點(diǎn)要素
- 數(shù)據(jù),左孩子指針,右孩子指針,父指針,索引(這里用來(lái)表征節(jié)點(diǎn)的序號(hào))
2、刪除元素
- 要求把對(duì)應(yīng)的子節(jié)點(diǎn)也刪除掉;
- 也可能要求只刪除該點(diǎn),然后該點(diǎn)的孩子節(jié)點(diǎn)指向該點(diǎn)的父節(jié)點(diǎn);
3、前序遍歷(根左右)
4、中序遍歷(左根右)
首先是左526,然后是根0,接著是右897;
然后對(duì)526進(jìn)行同樣的操作,即左是2,根是5,右是6,則排序結(jié)果是256;
同理對(duì)897進(jìn)行同樣的操作,即左是9,根是8,右是7,則排序結(jié)果是987;
最后合成為256 0 987。
5、后序遍歷(左右根)
首先是左526,然后是右897,接著是根0;
然后對(duì)526進(jìn)行同樣的操作,即左是2,右是6,根是5,則排序結(jié)果是265;
同理對(duì)897進(jìn)行同樣的操作,即左是9,右是7,根是8,則排序結(jié)果是978;
最后合成為265 ?978 0。
6、編碼實(shí)現(xiàn)
- 樹類、節(jié)點(diǎn)類;
- 節(jié)點(diǎn)類包含五要素:數(shù)據(jù),左孩子指針,右孩子指針,父指針,索引(這里用來(lái)表征節(jié)點(diǎn)的序號(hào))
- 前序遍歷(中序遍歷、后序遍歷就調(diào)換相應(yīng)的位置即可)。
- 對(duì)樹的遍歷操作,落實(shí)在根節(jié)點(diǎn)調(diào)用遍歷函數(shù)。
node.h
#ifndef NODE_H #define NODE_H class Node{ public :Node();int index;int data;Node *pLNOde;Node *pRNode;Node *pParent;Node *SerchNode(int index);void deleteNode();void Preorder();void Midorder();void Postorder(); }; #endifnode.cpp #include "Node.h" #include <iostream> using namespace std;Node::Node() {index=0;data=0;pLNOde=NULL;pRNode=NULL;pParent=NULL; };Node* Node::SerchNode(int index) {Node *temp=NULL;if (this->index==index)return this;if (this->pLNOde!=NULL){ temp=this->pLNOde->SerchNode(index);if (temp!=NULL){return temp;}}if (this->pRNode!=NULL){temp=this->pRNode->SerchNode(index);if (temp!=NULL){return temp;}}return NULL; };void Node::deleteNode() {if(this->pLNOde!=NULL) {this->pLNOde->deleteNode();}if (this->pRNode!=NULL){this->pRNode->deleteNode();}if (this->pParent!=NULL){if (this->pParent->pLNOde==this){this->pParent->pLNOde=NULL;}if (this->pParent->pRNode==this){this->pParent->pRNode=NULL;}}delete this; };void Node::Preorder() {cout<<this->data<<" "<<this->index<<endl;if (this->pLNOde!=NULL){this->pLNOde->Preorder();}if (this->pRNode!=NULL){this->pRNode->Preorder();} };void Node::Midorder() {if (this->pLNOde!=NULL){this->pLNOde->Midorder();}cout<<this->data<<" "<<this->index<<endl;if (this->pRNode!=NULL){this->pRNode->Midorder();} };void Node::Postorder() {if (this->pLNOde!=NULL){this->pLNOde->Postorder();}if (this->pRNode!=NULL){this->pRNode->Postorder();}cout<<this->data<<" "<<this->index<<endl; };
tree.cpp #include "Tree.h" #include "Node.h" #include <iostream> using namespace std;Tree::Tree() {p_node=new Node(); };Tree::~Tree() {deleteNode(0,NULL);//這里調(diào)用的是tree中的刪除節(jié)點(diǎn)函數(shù),從根節(jié)點(diǎn)(0)開始 };Node *Tree::SerchNode(int index) {return p_node->SerchNode(index);};bool Tree::deleteNode(int index,Node *node) {Node *temp=SerchNode(index);if (temp!=NULL){if (node!=NULL)//傳入的Node可以為null。是null時(shí),表明不需要把要?jiǎng)h除的節(jié)點(diǎn)的數(shù)據(jù)保存。{node->index=temp->index;node->data=temp->data;}temp->deleteNode();return true;}elsereturn false; };bool Tree::addNode(int index,int direction,Node *node) {Node *temp=SerchNode(index);if (temp){Node *NewNode=new Node();if (NewNode==NULL){return false;}NewNode->data=node->data;NewNode->index=node->index;if (direction==0){temp->pLNOde=NewNode;NewNode->pParent=temp;}if (direction==1){NewNode->pParent=temp;temp->pRNode=NewNode;}return true;}return false; };void Tree::Preorder() {p_node->Preorder(); }void Tree::Midorder() {p_node->Midorder(); }void Tree::Postorder() {p_node->Postorder(); }
test.cpp #include "Node.h" #include "Tree.h" #include <iostream>using namespace std; /*0(0)1(1) 2(2)5(3) 7(4) 6(5) 9(6)*/ int main() {Tree *p=new Tree;//這里的p是指向根節(jié)點(diǎn)的指針Node *n2=new Node;n2->data=1;n2->index=1;Node *n3=new Node;n3->data=2;n3->index=2;Node *n4=new Node;n4->data=3;n4->index=3;Node *n5=new Node;n5->data=4;n5->index=4;Node *n6=new Node;n6->data=5;n6->index=5;Node *n7=new Node;n7->data=6;n7->index=6;Node *n8=new Node;n8->data=8;n8->index=8;Node *n9=new Node;n9->data=9;n9->index=9; Node *n10=new Node;n10->data=10;n10->index=10;Node *n11=new Node;n11->data=11;n11->index=11;p->addNode(0,0,n2);p->addNode(0,1,n3);p->addNode(1,0,n4);p->addNode(1,1,n5);p->addNode(2,0,n6);p->addNode(2,1,n7); // p->addNode(4,0,n8); // p->addNode(4,1,n9); // p->addNode(6,0,n10); // p->addNode(6,1,n11);Node *n18=new Node;n18=p->SerchNode(10);if (n18!=NULL){cout<<"index:"<<n18->index<<endl;}//Node *n12=new Node;//p->deleteNode(6,n12);p->Preorder();cout<<"========================="<<endl;p->Postorder();cout<<"========================="<<endl;p->Midorder();delete p;p=NULL;system("pause");return 0; }
總結(jié)
以上是生活随笔為你收集整理的数据结构探险——树篇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 解决Anaconda第三方库下载慢
- 下一篇: Dev-cpp5.4.0的详细安装步骤