Java實現二叉樹的相關操作

2021-07-04 18:37:47 字數 1028 閱讀 4658

// 求二叉樹的深度

public

static

intbitreedepth(bitnode t)

return depthval;

}// 求data所對應結點的層數,如果物件不在樹中,結果返回-1;否則結果返回該物件在樹中所處的層次,規定根節點為第一層

public

static

intlevel(bitnode bitnode, int data)

// 求二叉樹葉子節點的總數

public

static

intleafnum(bitnode tree)

}// 求二叉樹父節點個數

public

static

intfathernodes(bitnode tree)

}// 求只有乙個孩子結點的父節點個數

public

static

intonechildfather(bitnode tree)

}// 求二叉樹只擁有左孩子的父節點總數

public

static

intleftchildfather(bitnode tree)

}// 求二叉樹只擁有左孩子的父節點總數

public

static

intrightchildfather(bitnode tree)

}// 計算有兩個節點的父節點的個數

public

static

intdoublechildfather(bitnode tree)

}// 將樹中的每個節點的孩子對換位置

public

static

void

exchange(bitnode tree)

// 遞迴求所有結點的和

public

static

intgetsumbyrecursion(bitnode tree) else

}

java 實現二叉樹操作

public class tree 建立二叉樹,返回根結點 param input return public static tree createtree int input else else else else return root 前序遍歷 param tree public static...

二叉樹 二叉樹的相關操作

遞迴實現 建立求樹高 求葉子數 求節點數 統計度為2的結點個數 後序輸出 先序輸出 中序輸出 交換左右子樹 include include include define true 1 define false 0 define ok 1 define error 0 define overflow ...

二叉樹的相關操作(一) java

二叉樹的相關操作,主要包括建立 判空 刪除子樹 刪除節點 各種遍歷演算法等 包括先根遍歷 中根遍歷 後根遍歷 層次遍歷等 本章重點說明四種遍歷演算法與刪除節點操作的實現。以如下二叉樹為例 1 先根遍歷 先訪問根節點,然後是左節點,然後是右節點,遞迴直至訪問結束 上述二叉樹先根遍歷順序為abdgcef...