利用java如何實(shí)現(xiàn)一個(gè)二叉查找樹(shù)功能

這篇文章給大家介紹利用java 如何實(shí)現(xiàn)一個(gè)二叉查找樹(shù)功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

創(chuàng)新互聯(lián)是專(zhuān)業(yè)的浉河網(wǎng)站建設(shè)公司,浉河接單;提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行浉河網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

java 二叉查找樹(shù)實(shí)例代碼

1.左邊<中間<右邊

2.前序遍歷 左中右

3.中序遍歷 中左右

4.后序遍歷 左右中

public class BinaryTree {

  // 二叉樹(shù)的根節(jié)點(diǎn)
  public TreeNode rootNode ;
  // 記錄搜索深度
  public int count;

  /**
   * 利用傳入一個(gè)數(shù)組來(lái)建立二叉樹(shù)
   */
  public BinaryTree(int[] data) {
    for (int i = 0; i < data. length; i++) {
      addNodeToTree(data[i]);
    }
  }

  /**
   * 將指定的值加入到二叉樹(shù)中適當(dāng)?shù)墓?jié)點(diǎn)
   */
  private void addNodeToTree(int value) {
    TreeNode currentNode = rootNode;
    // 建立樹(shù)根
    if (rootNode == null) {
      rootNode = new TreeNode(value);
      return;
    }

    // 建立二叉樹(shù)
    while (true) {
      // 新增的value比節(jié)點(diǎn)的value小,則在左子樹(shù)
      if (value < currentNode.value ) {
        if (currentNode.leftNode == null) {
          currentNode.leftNode = new TreeNode(value);
          return;
        } else {
          currentNode = currentNode.leftNode;
        }
      } else { // 新增的value比節(jié)點(diǎn)的value大,在右子樹(shù)
        if (currentNode.rightNode == null) {
          currentNode. rightNode = new TreeNode(value);
          return;
        } else {
          currentNode = currentNode. rightNode;
        }
      }
    }
  }

  /**
   * 中序遍歷(左子樹(shù) -樹(shù)根- 右子樹(shù))
   */
  public void inOrder(TreeNode node) {
    if (node != null) {
      inOrder(node. leftNode);
      System. out.print("[" + node.value + "]");
      inOrder(node. rightNode);
    }
  }

  /**
   * 前序遍歷(樹(shù)根 -左子樹(shù)- 右子樹(shù))
   */
  public void preOrder(TreeNode node) {
    if (node != null) {
      System. out.print("[" + node.value + "]");
      preOrder(node. leftNode);
      preOrder(node. rightNode);
    }
  }

  /**
   * 后序遍歷(左子樹(shù) -右子樹(shù)- 樹(shù)根)
   */
  public void postOrder(TreeNode node) {
    if (node != null) {
      postOrder(node. leftNode);
      postOrder(node. rightNode);
      System. out.print("[" + node.value + "]");
    }
  }

  /**
   * 從二叉樹(shù)中查找指定value
   */
  public boolean findTree(TreeNode node, int value) {
    if (node == null) {
      System. out.println("共搜索" + count + "次");
      return false;
    } else if (node.value == value) {
      System. out.println("共搜索" + count + "次");
      return true;
    } else if (value < node.value) {
      count++;
      return findTree(node.leftNode , value);
    } else {
      count++;
      return findTree(node.rightNode , value);
    }
  }

  /**
   * 利用中序遍歷進(jìn)行排序
   */
  public void sort() {
    this.inOrder(rootNode );
  }

  class TreeNode {
    int value ;
    TreeNode leftNode;
    TreeNode rightNode;

    public TreeNode(int value) {
      this.value = value;
      this.leftNode = null;
      this.rightNode = null;
    }
  }

  public static void main(String[] args) {
    int[] content = { 50, 35, 27, 45, 40, 48, 78, 56, 90 };

    BinaryTree tree = new BinaryTree(content);
    System. out.println("前序遍歷:" );
    tree.preOrder(tree. rootNode);
    System. out.println("\n中序遍歷:" );
    tree.inOrder(tree. rootNode);
    System. out.println("\n后序遍歷:" );
    tree.postOrder(tree. rootNode);

    System. out.println("\n\n開(kāi)始搜索:" );
    boolean isFind = tree.findTree(tree.rootNode, 48);
    System. out.println("是否搜索到" + 48 + ":" + isFind);

    System. out.println("\n進(jìn)行排序:" );
    tree.sort();
  }
}

前序遍歷:

[50][35][27][45][40][48][78][56][90]

中序遍歷:

[27][35][40][45][48][50][56][78][90]

后序遍歷:

[27][40][48][45][35][56][90][78][50]

開(kāi)始搜索:

共搜索3次

是否搜索到48:true

進(jìn)行排序:

[27][35][40][45][48][50][56][78][90]

關(guān)于利用java 如何實(shí)現(xiàn)一個(gè)二叉查找樹(shù)功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享文章:利用java如何實(shí)現(xiàn)一個(gè)二叉查找樹(shù)功能
標(biāo)題路徑:http://muchs.cn/article8/gdssip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、電子商務(wù)、、移動(dòng)網(wǎng)站建設(shè)面包屑導(dǎo)航、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站