樹的相關操作

2021-07-05 13:43:50 字數 833 閱讀 6799

樹的直徑   對於樹上的邊帶有權重的話  而且每個節點最多只能經過一次的話   我們定義樹的直徑是倆個點最大距離為樹的直徑  然後其他的點的最大距離為到倆個直徑端點的最大的那個

#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;

typedef long long ll;

typedef pairpp;

#define inf 0x3f3f3f3f

#define eps 1e-10

#define maxl 30010

#define mem(i,j) memset(i,j,sizeof(i))

const int mod=1e9+7;

struct edge

edge(){}

};vectorg[maxl];

int d[maxl],vis[maxl];

int res,ans;

int n,m;//節點和邊的數目

void bfs(int p){

queueque;

memset(d,0,sizeof(d));

memset(vis,0,sizeof(vis));

while(que.size()) que.pop();

res=p;ans=0;

que.push(p);

vis[p]=1;

while(que.size()){

int q=que.front();que.pop();

for(int i=0;i

樹的相關操作

include stdio.h include malloc.h define maxlength 128 struct node struct node createnode int value struct stack struct queue void addchild struct node...

樹相關的操作

講乙個二叉樹變為排序二叉樹 struct treenode void insertbstree treenode root,int data else p p right if parent null root tempnode else void pretranverse treenode roo...

b樹及其相關操作

b樹的插入 1.類似於二叉排序樹,按照關鍵字大小插入到最低層的某個非葉節點。2.如果插入後結點關鍵字輸滿足b樹階的要求 關鍵字數 b樹階 1 終止 3.如果結點上的關鍵字樹大於等於b樹的階,如對階 3的b樹,最多關鍵字數為2,此時進行了插入結點後該結點關鍵字數為3,則設三個關鍵字從大到小分別叫a,b...