-
二叉樹的順序存儲(數組存儲): 索引為i的結點的左孩子索引為2*i+1,右孩子結點為2*i+2查看全部
-
#include<stdlib.h> #include"Tree.h" #include<iostream> using namespace std; /******************************************************************** 【數據結構探險---樹篇】 ***************************************************/ int main(void) { int root=3; Tree *pTree=new Tree(); Node *node1= new Node(); node1->index=1; node1->data=5; Node *node2= new Node(); node2->index=2; node2->data=8; Node *node3= new Node(); node3->index=3; node3->data=2; Node *node4= new Node(); node4->index=4; node4->data=6; Node *node5= new Node(); node5->index=5; node5->data=9; Node *node6= new Node(); node6->index=6; node6->data=7; pTree->AddNode(0,0,node1); pTree->AddNode(0,1,node2); pTree->AddNode(1,0,node3); pTree->AddNode(1,1,node4); pTree->AddNode(2,0,node5); pTree->AddNode(2,1,node6); cout<<"前序遍歷"<<endl; pTree->PreorderTravers(); cout<<"中序遍歷"<<endl; pTree->InorderTravers(); cout<<"后序遍歷"<<endl; pTree->PostorderTravers(); delete pTree; pTree=NULL; system("pause"); return 0; }查看全部
-
#include"stdlib.h" #include "Node.h" Node::Node() { index=0; data=0; pLchild=NULL; pRchild=NULL; pParent=NULL; } Node *Node::SearchNode(int nodeindex) { if(this->index==nodeindex) { return this; } if(this->pLchild!=NULL) { if(this->pLchild->index==nodeindex) { return this->pLchild; } if(this->pRchild!=NULL) { if(this->pRchild->index==nodeindex) { return this->pRchild; } return NULL; } }查看全部
-
#include<stdlib.h> #include"Tree.h" #include<iostream> using namespace std; /******************************************************************** 【數據結構探險---樹篇】 Tree(int size,int *pRoot); //創建樹 ~Tree(); //銷毀樹 int * SearchNode(int nodeindex);//搜索結點 bool AddNode(int nodeindex,int direction,int *pNode);//添加結點 bool DeleteNode(int nodeindex,int * pNode);//刪除結點 void PreorderTravers();//前序遍歷 void InorderTravers(); //中序遍歷 void PostorderTravers();//后序遍歷 -------------------------------------------------------------------- 二叉樹--鏈表實現 (0) 左孩子索引=父節點索引*2+1 5(1) 8(2) 右孩子索引=父節點索引*2+2 2(3) 6(4) 9(5) 7(6) 前序遍歷:根左右0134256 中序遍歷:左根右3140526 后序遍歷:左右根 3415620 **********************************************************************/ int main(void) { int root=3; Tree *pTree=new Tree(10,&root); delete pTree; pTree=NULL; system("pause"); return 0; }查看全部
-
#include<stdlib.h> #include"Tree.h" #include<iostream> using namespace std; /******************************************************************** /* 數組---樹 Tree【】=3 5 8 2 6 9 7 3(0) 左孩子小標=父節點下標*2+1 5(1) 8(2) 右孩子小標=父節點下標*2+2 2(3) 6(4) 9(5) 7(6) **********************************************************************/ int main(void) { int root=3; Tree *pTree=new Tree(10,&root); int node1=5; int node2=8; pTree->AddNode(0,0,&node1); pTree->AddNode(0,1,&node2); int node3=2; int node4=6; int node5=9; int node6=7; pTree->AddNode(1,0,&node3); pTree->AddNode(1,1,&node4); pTree->AddNode(2,0,&node5); pTree->AddNode(2,1,&node6); int temp; pTree->DeleteNode(6,&temp); cout<<"node="<<temp<<endl; pTree->TreeTravers(); cout<<endl<<"node="<<*pTree->SearchNode(2)<<endl; delete pTree; pTree=NULL; system("pause"); return 0; }查看全部
-
#include"Tree.h" #include<iostream> using namespace std; Tree::Tree(int size) { m_iSize=size; m_pTree=new int[size]; for(int i=0;i<m_iSize;i++) { m_pTree[i]=0; } } Tree::~Tree() { delete []m_pTree; m_pTree=NULL; } int *Tree:: SearchNode(int nodeindex) { if(nodeindex<0 || nodeindex>=m_iSize) { return NULL; } if(m_pTree[nodeindex]==0) { return NULL; } return &m_pTree[nodeindex]; } bool Tree::AddNode(int nodeindex,int direction,int *pNode) { if(nodeindex<0 || nodeindex>=m_iSize || m_pTree[nodeindex]==0) { return false; } switch(direction) { case 0: if( nodeindex*2+1>=m_iSize || m_pTree[nodeindex*2+1]!=0) { return false; } m_pTree[nodeindex*2+1]=*pNode; break; case 1: if( nodeindex*2+2>=m_iSize || m_pTree[nodeindex*2+2]!=0) { return false; } m_pTree[nodeindex*2+2]=*pNode; break; } return true; } bool Tree::DeleteNode(int nodeindex,int * pNode) { if(nodeindex<0 || nodeindex>=m_iSize || m_pTree[nodeindex]==0) {查看全部
-
子類繼承了父類,并且重寫了父類的方法,則調用父類的方法查看全部
-
用數組來表示結點 左結點:父節點下標*2+1; 右結點:父節點下標*2+2;查看全部
-
結點深度和結點所在的層是統一的,在第幾次,結點的深度就是幾 樹的深度,是指當前這棵樹當中,結點所具有的最大深度 多棵樹放在一起就構成森林 二叉樹:所有結點的度,夠小于等于2 前序遍歷、中序遍歷、后序遍歷是相對于根節點說的 前序遍歷:先訪問根,再訪問左右結點(根第一位訪問) 中序遍歷:先訪問左結點,再訪問根,然后右結點(根第二位訪問) 后序遍歷:先訪問左結點,再訪問右結點,最后訪問根節點(根第三位訪問)查看全部
-
樹是結點的有限集合 樹頂端的結點,叫做根節點。雙親是一個結點,不是兩個結點 度,就是當前這個結點它的直接的孩子 葉子,終端結點就是葉子,即為沒有孩子的結點 根,相對于葉子來說的,就是非終端結點 有序樹和無序樹,它們是相對的概念,如果E和F不能夠換順序,就是有序樹,如果可以換,又不影響邏輯的話,就是無序樹 祖先:當前指定結點一直向上的到總的根結點所路過的所有結點查看全部
-
想建立什么樹,必須是已經有個大概思想,甚至整體內容的。根據某些條件添加查看全部
-
沒有跟節點,就尷尬了,因為不能往上插啊。。查看全部
-
祖先,子孫查看全部
-
有序樹和無序樹查看全部
-
葉子,跟查看全部
舉報
0/150
提交
取消