中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

java怎么實現平衡二叉樹

小億
84
2023-11-22 09:15:57
欄目: 編程語言

要實現平衡二叉樹,可以使用紅黑樹或AVL樹這樣的自平衡二叉搜索樹。
以下是使用AVL樹實現平衡二叉樹的示例代碼:

// AVL樹節點類
class Node {

????int?val;

????int?height;

????Node?left;

????Node?right;

????Node(int?val)?{

????????this.val?=?val;

????????this.height?=?1;

????} } //?平衡二叉樹類 class?AVLTree?{

????private?Node?root;

????//?獲取節點的高度

????private?int?getHeight(Node?node)?{

????????if?(node?==?null)?{

????????????return?0;

????????}

????????return?node.height;

????}

????//?更新節點的高度

????private?void?updateHeight(Node?node)?{

????????node.height?=?Math.max(getHeight(node.left),?getHeight(node.right))?+?1;

????}

????//?獲取節點的平衡因子

????private?int?getBalanceFactor(Node?node)?{

????????if?(node?==?null)?{

????????????return?0;

????????}

????????return?getHeight(node.left)?-?getHeight(node.right);

????}

????//?右旋操作

????private?Node?rightRotate(Node?node)?{

????????Node?newRoot?=?node.left;

????????node.left?=?newRoot.right;

????????newRoot.right?=?node;

????????updateHeight(node);

????????updateHeight(newRoot);

????????return?newRoot;

????}

????//?左旋操作

????private?Node?leftRotate(Node?node)?{

????????Node?newRoot?=?node.right;

????????node.right?=?newRoot.left;

????????newRoot.left?=?node;

????????updateHeight(node);

????????updateHeight(newRoot);

????????return?newRoot;

????}

????//?平衡節點

????private?Node?balance(Node?node)?{

????????if?(node?==?null)?{

????????????return?null;

????????}

????????updateHeight(node);

????????int?balanceFactor?=?getBalanceFactor(node);

????????if?(balanceFactor?>?1)?{

????????????if?(getBalanceFactor(node.left)?>=?0)?{

????????????????return?rightRotate(node);

????????????}?else?{

????????????????node.left?=?leftRotate(node.left);

????????????????return?rightRotate(node);

????????????}

????????}?else?if?(balanceFactor?<?-1)?{

????????????if?(getBalanceFactor(node.right)?<=?0)?{

????????????????return?leftRotate(node);

????????????}?else?{

????????????????node.right?=?rightRotate(node.right);

????????????????return?leftRotate(node);

????????????}

????????}

????????return?node;

????}

????//?插入節點

????public?void?insert(int?val)?{

????????root?=?insert(root,?val);

????}

????private?Node?insert(Node?node,?int?val)?{

????????if?(node?==?null)?{

????????????return?new?Node(val);

????????}

????????if?(val?<?node.val)?{

????????????node.left?=?insert(node.left,?val);

????????}?else?if?(val?>?node.val)?{

????????????node.right?=?insert(node.right,?val);

????????}?else?{

????????????//?如果樹中已經存在相同值的節點,則不進行插入

????????????return?node;

????????}

????????return?balance(node);

????}

????//?中序遍歷

????public?void?inorderTraversal()?{

????????inorderTraversal(root);

????}

????private?void?inorderTraversal(Node?node)?{

????????if?(node?==?null)?{

????????????return;

????????}

????????inorderTraversal(node.left);

????????System.out.print(node.val?+?"?");

????????inorderTraversal(node.right);

????} } //?測試代碼 public?class?Main?{

????public?static?void?main(String[]?args)?{

????????AVLTree?tree?=?new?AVLTree();

????????tree.insert(3);

????????tree.insert(2);

????????tree.insert(1);

????????

????????tree.inorderTraversal();??//?輸出:1?2?3

????} }

以上是使用AVL樹實現平衡二叉樹的示例代碼,其中包含了插入節點、平衡節點和中序遍歷等操作。你可以根據需要進行修改和擴展。

0
临夏市| 曲水县| 瓮安县| 万荣县| 盐源县| 武鸣县| 阿拉善盟| 喀喇沁旗| 突泉县| 大港区| 柳江县| 文安县| 凭祥市| 静海县| 抚州市| 阜城县| 牡丹江市| 浦县| 水城县| 台湾省| 郑州市| 山阴县| 潍坊市| 綦江县| 彭州市| 阳春市| 涡阳县| 金寨县| 乌海市| 砚山县| 盐亭县| 长丰县| 大厂| 汝南县| 闽清县| 苏尼特左旗| 台北县| 秭归县| 巴林右旗| 平顺县| 平舆县|