欧美一级a看免费观看,一本在线视频在线观看,国产偷自拍对白在线视频,精品欧美一区二区在线观看

    1. <dfn id="ktbui"><var id="ktbui"></var></dfn>
    2. 您當(dāng)前位置: 唯學(xué)網(wǎng) » 更多IT培訓(xùn) » 更多IT培訓(xùn)教育新聞 »

      Java實(shí)現(xiàn)二叉樹遍歷算法

      Java實(shí)現(xiàn)二叉樹遍歷算法

      唯學(xué)網(wǎng) • 教育培訓(xùn)

      2016-9-20 14:24

      it

      唯學(xué)網(wǎng) • 中國教育電子商務(wù)平臺(tái)

      加入收藏

      為了方便廣大考生更好的復(fù)習(xí),幫考網(wǎng)綜合整理提供了Java實(shí)現(xiàn)二叉樹遍歷算法,以供各位考生考試復(fù)習(xí)參考,希望對(duì)考生復(fù)習(xí)有所幫助。

      Java實(shí)現(xiàn)二叉樹遍歷算法

      在JAVA中實(shí)現(xiàn)二叉樹,程序如下

      //********************************************************************

      //filename: BinaryTreeTest.java

      //purpose: test a binarytree with java

      //date: 2002/12/18

      //author: flyfan

      //ver: 0.1

      //********************************************************************

      public class BinaryTreeTest

      {

      public static void main(String args[])

      {

      BinaryTreeTest b=new BinaryTreeTest();

      int data[]={12,11,34,45,67,89,56,43,22,98};

      BinaryTree root =new BinaryTree(data[0]);

      System.out.print(“二叉樹的中的數(shù)據(jù):  ”);

      for(int i=1;i《data.length;i++)

      {

      root.insertTree(root,data[i]);

      System.out.print(data[i-1]+“;”);

      }

      System.out.println(data[data.length-1]);

      int key=Integer.parseInt(args[0]);

      if(b.searchkey(root,key))

      {

      System.out.println(“找到了:”+key);

      }

      else

      {

      System.out.println(“沒有找到:”+key);

      }

      }

      public boolean searchkey(BinaryTree root, int key)

      {

      boolean bl=false;

      if(root==null)

      {

      bl=false;

      return bl;

      }

      else if(root.data==key)

      {

      bl=true;

      return bl;

      }

      else if(key》=root.data)

      {

      return searchkey(root.rightpoiter,key);

      }

      return searchkey(root.leftpoiter,key);

      }

      }

      class BinaryTree

      {

      int data;

      BinaryTree leftpoiter;

      BinaryTree rightpoiter;

      BinaryTree(int data)

      {

      this.data=data;

      leftpoiter=null;

      rightpoiter=null;

      }

      public void insertTree(BinaryTree root, int data)

      {

      if(data》=root.data)

      {

      if(root.rightpoiter==null)

      {

      root.rightpoiter=new BinaryTree(data);

      }

      else

      {

      insertTree(root.rightpoiter,data);

      }

      }

      else

      {

      if(root.leftpoiter==null)

      {

      root.leftpoiter=new BinaryTree(data);

      }

      else

      {

      insertTree(root.leftpoiter,data);

      }

      }

      }

      }

      //end

      講解:上述各序小,但層次分明,結(jié)構(gòu)嚴(yán)謹(jǐn),如果有數(shù)據(jù)庫結(jié)構(gòu)知識(shí)與C語文能力的JAVA初學(xué)者一看就明白,二個(gè)方法如同C語文中的函數(shù),一個(gè)尋找關(guān)鍵字--searchkey 另一個(gè)是插入一個(gè)結(jié)點(diǎn):insertTree 而class BinaryTree 如同一個(gè)C語言中的共同體。

      另外這是一個(gè)完全的先序遍歷二叉樹的語法。先根結(jié)點(diǎn),再左結(jié)點(diǎn),如無再右結(jié)點(diǎn),如些加歸至搜索完畢。

      運(yùn)行命令行:java BinaryTreeTest intNumber(一個(gè)整數(shù))

      以上內(nèi)容是關(guān)于Java實(shí)現(xiàn)二叉樹遍歷算法的介紹,要想了解更多相關(guān)信息、教育培訓(xùn)內(nèi)容,請(qǐng)隨時(shí)關(guān)注唯學(xué)網(wǎng),小編會(huì)第一時(shí)間為大家更新、跟進(jìn)最新信息。

      0% (0)
      0% (10)
      已有條評(píng)論