日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构探险——树篇

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构探险——树篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下內容源于慕課網的學習整理,如有侵權,請告知刪除。


樹存在概念中,是對數組或者鏈表的一種操作方式的概念。


一、與樹有關的一些基礎概念

(1)樹

  • 有限節點的集合;

(2)度

  • 某個節點的直接孩子數目;

(3)葉節點

  • 終端節點

(4)祖先

  • 所有在它之上的節點

(5)深度

  • 節點的深度(節點所處的位置)
  • 樹的深度(整棵樹的深度)

(6)二叉樹

  • 所有節點的度都小于等于2

(7)二叉樹的遍歷

  • 前中后,是針對“根”來說的。

(8)作用實例

  • 人機對戰


二、二叉樹的數組實現

1、換算公式

  • 父節點下標*2+1,得到,父節點的左孩子節點的下標;
  • 父節點下標*2+2,得到,父節點的右孩子節點的下標

2、示意圖

  • 括號里面表示的是索引,或者說節點序號,外面的是數據。
  • 慕課網中只是按照這個圖,來進行查找(遍歷查找)、插入(已經確定在哪個位置上出入了)、刪除(刪除后賦值為0)等操作。


三、二叉樹的鏈表實現

1、節點要素

  • 數據,左孩子指針,右孩子指針,父指針,索引(這里用來表征節點的序號)

2、刪除元素

  • 要求把對應的子節點也刪除掉;
  • 也可能要求只刪除該點,然后該點的孩子節點指向該點的父節點;

3、前序遍歷(根左右)



4、中序遍歷(左根右)

首先是左526,然后是根0,接著是右897;

然后對526進行同樣的操作,即左是2,根是5,右是6,則排序結果是256;

同理對897進行同樣的操作,即左是9,根是8,右是7,則排序結果是987;

最后合成為256 0 987。


5、后序遍歷(左右根)

首先是左526,然后是右897,接著是根0;

然后對526進行同樣的操作,即左是2,右是6,根是5,則排序結果是265;

同理對897進行同樣的操作,即左是9,右是7,根是8,則排序結果是978;

最后合成為265 ?978 0。



6、編碼實現

  • 樹類、節點類;
  • 節點類包含五要素:數據,左孩子指針,右孩子指針,父指針,索引(這里用來表征節點的序號)
  • 前序遍歷(中序遍歷、后序遍歷就調換相應的位置即可)。
  • 對樹的遍歷操作,落實在根節點調用遍歷函數。

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(); }; #endif
node.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.h

#ifndef Tree_H #define Tree_H #include "Node.h"class Tree { public:Tree();~Tree();Node *SerchNode(int index);//查找索引為index的那個節點,并返回指向該節點的指針bool addNode(int index,int direction,Node *node);//添加,在索引為index的節點上,添加一個節點node//左右方向由direction決定bool deleteNode(int index,Node *node);//刪除void Preorder();//前序void Midorder();//中序void Postorder();//后序 private:Node *p_node;};#endif

tree.cpp

#include "Tree.h" #include "Node.h" #include <iostream> using namespace std;Tree::Tree() {p_node=new Node(); };Tree::~Tree() {deleteNode(0,NULL);//這里調用的是tree中的刪除節點函數,從根節點(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時,表明不需要把要刪除的節點的數據保存。{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是指向根節點的指針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; }


總結

以上是生活随笔為你收集整理的数据结构探险——树篇的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。