日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java实现树

發(fā)布時(shí)間:2025/6/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现树 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java實(shí)現(xiàn)樹,采用鏈?zhǔn)酱鎯?chǔ),父節(jié)點(diǎn)記錄子節(jié)點(diǎn)的存儲(chǔ)位置。

首先定義一個(gè)用于存儲(chǔ)子節(jié)點(diǎn)位置的節(jié)點(diǎn)類

[java]?view plaincopy
  • package?my.tree.link;??
  • ??
  • public?class?SubNode?{??
  • ????private?int?location;??
  • ????private?SubNode?next;??
  • ??????
  • ????public?SubNode(){??
  • ????????this.location?=?0;??
  • ????????this.next?=?null;??
  • ????}??
  • ????public?SubNode(int?location){??
  • ????????this.location?=?location;??
  • ????????this.next?=?null;??
  • ????}??
  • ??????
  • ????public?SubNode(int?location,?SubNode?next){??
  • ????????this.location?=?location;??
  • ????????this.next?=?next;??
  • ????}??
  • ??????
  • ????public?void?setLocation(int?location){??
  • ????????this.location?=?location;??
  • ????}??
  • ??????
  • ????public?int?getLocation(){??
  • ????????return?this.location;??
  • ????}??
  • ??????
  • ????public?void?setNext(SubNode?next){??
  • ????????this.next?=?next;??
  • ????}??
  • ??????
  • ????public?SubNode?getNext(){??
  • ????????return?this.next;??
  • ????}??
  • }??

  • 然后定義一個(gè)用于存儲(chǔ)節(jié)點(diǎn)信息的節(jié)點(diǎn)類

    [java]?view plaincopy
  • package?my.tree.link;??
  • ??
  • public?class?Node<T>?{??
  • ????private?T?data;??
  • ????private?SubNode?son;??
  • ??????
  • ????public?Node(){??
  • ??????????
  • ????}??
  • ??????
  • ????public?Node(T?data){??
  • ????????this.data?=?data;??
  • ????????this.son?=?null;??
  • ????}??
  • ??????
  • ????public?Node(T?data,?SubNode?son){??
  • ????????this.data?=?data;??
  • ????????this.son?=?son;??
  • ????}??
  • ??????
  • ????public?void?setData(T?data){??
  • ????????this.data?=?data;??
  • ????}??
  • ??????
  • ????public?T?getData(){??
  • ????????return?this.data;??
  • ????}??
  • ??????
  • ????public?void?setSon(SubNode?son){??
  • ????????this.son?=?son;??
  • ????}??
  • ??????
  • ????public?SubNode?getSon(){??
  • ????????return?this.son;??
  • ????}??
  • ??????
  • ????@Override??
  • ????public?String?toString(){??
  • ????????return?"節(jié)點(diǎn):"?+?this.data;??
  • ????}??
  • }??



  • 編寫鏈?zhǔn)酱鎯?chǔ)的樹類,這里采用遞歸求解樹的深度(貌似有問題,在求樹的深度是,很迷糊)

    [java]?view plaincopy
  • package?my.tree.link;??
  • ??
  • import?java.util.LinkedList;??
  • import?java.util.List;??
  • ??
  • public?class?MyLinkTree<T>?{??
  • ????private?final?int?DEFAUL_SIZE?=?10;??
  • ????private?int?size;??
  • ????private?int?count;??
  • ??
  • ????private?Node<T>[]?nodes;??
  • ??
  • ????@SuppressWarnings("unchecked")??
  • ????public?MyLinkTree()?{??
  • ????????this.size?=?this.DEFAUL_SIZE;??
  • ????????this.nodes?=?new?Node[this.size];??
  • ????????this.count?=?0;??
  • ????}??
  • ??
  • ????@SuppressWarnings("unchecked")??
  • ????public?MyLinkTree(int?size)?{??
  • ????????this.size?=?size;??
  • ????????this.nodes?=?new?Node[this.size];??
  • ????????this.count?=?0;??
  • ????}??
  • ??
  • ????public?MyLinkTree(T?data)?{??
  • ????????this();??
  • ????????Node<T>?node?=?new?Node<T>(data);??
  • ????????this.nodes[0]?=?node;??
  • ????????this.count++;??
  • ????}??
  • ??
  • ????public?MyLinkTree(Node<T>?root)?{??
  • ????????this();??
  • ????????this.nodes[0]?=?root;??
  • ????????this.count++;??
  • ????}??
  • ??
  • ????public?void?add(Node<T>?node,?Node<T>?parent)?{??
  • ????????SubNode?son?=?new?SubNode();??
  • ????????for?(int?i?=?0;?i?<?this.size;?i++)?{??
  • ????????????if?(this.nodes[i]?==?null)?{??
  • ????????????????this.nodes[i]?=?node;??
  • ????????????????son.setLocation(i);??
  • ????????????????break;??
  • ????????????}??
  • ????????}??
  • ??
  • ????????//?往鏈表中添加子節(jié)點(diǎn)位置??
  • ????????SubNode?next?=?parent.getSon();??
  • ????????if?(next?!=?null)?{??
  • ????????????while?(next.getNext()?!=?null)?{??
  • ????????????????next?=?next.getNext();??
  • ????????????}??
  • ????????????next.setNext(son);??
  • ????????}?else?{??
  • ????????????parent.setSon(son);??
  • ????????}??
  • ??
  • ????????this.count++;??
  • ????}??
  • ??
  • ????public?int?size()?{??
  • ????????return?this.count;??
  • ????}??
  • ??
  • ????public?Node<T>?getRoot()?{??
  • ????????return?this.nodes[0];??
  • ????}??
  • ??
  • ????//?獲取指定節(jié)點(diǎn)的子節(jié)點(diǎn)??
  • ????public?List<Node<T>>?getSon(Node<T>?parent)?{??
  • ????????List<Node<T>>?list?=?new?LinkedList<Node<T>>();??
  • ????????SubNode?son?=?parent.getSon();??
  • ????????while?(son?!=?null)?{??
  • ????????????list.add(this.nodes[son.getLocation()]);??
  • ????????????son?=?son.getNext();??
  • ????????}??
  • ????????return?list;??
  • ????}??
  • ??
  • ????//?獲取樹的深度,通過遞歸的方式來解決??
  • ????public?int?getDepth(Node<T>?node)?{??
  • ????????SubNode?son?=?node.getSon();??
  • ????????if(son?==?null){??
  • ????????????return?1;??
  • ????????}else{??
  • ????????????int?max?=?0;??
  • ????????????while(son?!=?null){??
  • ????????????????int?temp?=?this.getDepth(this.nodes[son.getLocation()]);??
  • ????????????????max?=?temp?>?max???temp?:?max;??
  • ????????????????son?=?son.getNext();??
  • ????????????}??
  • ????????????//為什么要max+1???
  • ????????????return?max+1;??
  • ????????}??
  • ????}??
  • ??
  • ????public?int?deep()?{??
  • ????????int?max?=?0;??
  • ????????for?(int?i?=?0;?i?<?this.count;?i++)?{??
  • ????????????int?temp?=?this.getDepth(this.nodes[i]);??
  • ????????????max?=?max?>?temp???max?:?temp;??
  • ????????}??
  • ????????return?max;??
  • ????}??
  • }??



  • 最后編寫一個(gè)測(cè)試端,用來測(cè)試功能是否基本實(shí)現(xiàn)

    [java]?view plaincopy
  • package?my.tree.link;??
  • ??
  • public?class?MyLinkTreeClient?{??
  • ????public?static?void?main(String[]?args)?{??
  • ????????Node<string>?root?=?new?Node<string>("A");??
  • ????????Node<string>?b?=?new?Node<string>("B");??
  • ????????Node<string>?c?=?new?Node<string>("C");??
  • ????????Node<string>?d?=?new?Node<string>("D");??
  • ????????Node<string>?e?=?new?Node<string>("E");??
  • ????????Node<string>?f?=?new?Node<string>("F");??
  • ????????Node<string>?g?=?new?Node<string>("G");??
  • ????????Node<string>?h?=?new?Node<string>("H");??
  • ????????MyLinkTree<string>?tree?=?new?MyLinkTree<string>(root);??
  • ????????tree.add(b,?root);??
  • //??????tree.add(c,?root);??
  • //??????tree.add(d,?root);??
  • //??????tree.add(e,?b);??
  • //??????tree.add(f,?b);??
  • //??????tree.add(g,?f);??
  • //??????tree.add(h,?g);??
  • //??????System.out.println(tree.size());??
  • ????????System.out.println(tree.deep());??
  • //??????System.out.println(tree.getSon(b));??
  • ????}??
  • }


  • 實(shí)現(xiàn)一顆樹,采用數(shù)組的存儲(chǔ)方式,將樹中的節(jié)點(diǎn)用Node類表示,方便與操作。

    首先,整棵樹的數(shù)組結(jié)構(gòu)如下表所示,根節(jié)點(diǎn)的無父節(jié)點(diǎn),用“-1”表示。
    indexDataparent
    0A-1
    1B0
    2C0
    3D0
    4E1
    5F1
    6G5

    其次,定義一個(gè)節(jié)點(diǎn)Node類,用來存儲(chǔ)每個(gè)節(jié)點(diǎn)的內(nèi)容

    [java]?view plaincopy
  • package?my.tree;??
  • ??
  • public?class?Node<T>?{??
  • ????private?T?data;??
  • ????private?int?parent;??
  • ??????
  • ????public?Node(){??
  • ????}??
  • ??????
  • ????public?Node(T?data){??
  • ????????this.data?=?data;??
  • ????}??
  • ??????
  • ????public?Node(T?data,int?parent){??
  • ????????this.data?=?data;??
  • ????????this.parent?=?parent;??
  • ????}??
  • ??????
  • ????public?void?setData(T?data){??
  • ????????this.data?=?data;??
  • ????}??
  • ??????
  • ????public?T?getData(){??
  • ????????return?this.data;??
  • ????}??
  • ??????
  • ????public?void?setParent(int?parent){??
  • ????????this.parent?=?parent;??
  • ????}??
  • ??????
  • ????public?int?getParent(){??
  • ????????return?this.parent;??
  • ????}??
  • }??

  • 開始實(shí)現(xiàn)樹,提供基本的插入節(jié)點(diǎn)、獲取所有節(jié)點(diǎn)、獲取樹的深度操作(著重)這些將數(shù)組大小設(shè)置為2,主要是考慮數(shù)組能否自動(dòng)擴(kuò)容;

    [java]?view plaincopy
  • package?my.tree;??
  • ??
  • import?java.util.ArrayList;??
  • import?java.util.Arrays;??
  • import?java.util.List;??
  • ??
  • public?class?MyTree<T>?{??
  • ????private?final?int?DEFAULT_SIZE?=?2;??
  • ????private?int?size;??
  • ????private?int?count;??
  • ????private?Object[]?nodes;??
  • ??
  • ????public?MyTree()?{??
  • ????????this.size?=?this.DEFAULT_SIZE;??
  • ????????this.nodes?=?new?Object[this.size];??
  • ????????this.count?=?0;??
  • ????}??
  • ??
  • ????public?MyTree(Node<T>?root)?{??
  • ????????this();??
  • ????????this.count?=?1;??
  • ????????this.nodes[0]?=?root;??
  • ????}??
  • ??
  • ????public?MyTree(Node<T>?root,?int?size)?{??
  • ????????this.size?=?size;??
  • ????????this.nodes?=?new?Object[this.size];??
  • ????????this.count?=?1;??
  • ????????this.nodes[0]?=?root;??
  • ????}??
  • ??
  • ????//添加一個(gè)節(jié)點(diǎn)??
  • ????public?void?add(Node<T>?node)?{??
  • ????????for?(int?i?=?0;?i?<?this.size;?i++)?{??
  • ????????????if?(this.nodes[i]?==?null)?{??
  • ????????????????nodes[i]?=?node;??
  • ????????????????break;??
  • ????????????}??
  • ????????}??
  • ????????this.count++;??
  • ????}??
  • ??
  • ????public?void?check(){??
  • ????????if(this.count?>=?this.size){??
  • ????????????this.enlarge();??
  • ????????}??
  • ????}??
  • ????//添加一個(gè)節(jié)點(diǎn),并指明父節(jié)點(diǎn)??
  • ????public?void?add(Node<T>?node,?Node<T>?parent)?{??
  • ????????this.check();??
  • ????????node.setParent(this.position(parent));??
  • ????????this.add(node);??
  • ????}??
  • ??
  • ????//獲取節(jié)點(diǎn)在數(shù)組的存儲(chǔ)位置??
  • ????public?int?position(Node<T>?node)?{??
  • ????????for?(int?i?=?0;?i?<?this.size;?i++)?{??
  • ????????????if?(nodes[i]?==?node)?{??
  • ????????????????return?i;??
  • ????????????}??
  • ????????}??
  • ????????return?-1;??
  • ????}??
  • ??????
  • ????//獲取整棵樹有多少節(jié)點(diǎn)??
  • ????public?int?getSize(){??
  • ????????return?this.count;??
  • ????}??
  • ??????
  • ????//獲取根節(jié)點(diǎn)??
  • ????@SuppressWarnings("unchecked")??
  • ????public?Node<T>?getRoot(){??
  • ????????return?(Node<T>)?this.nodes[0];??
  • ????}??
  • ??????
  • ????//獲取所以節(jié)點(diǎn),以List形式返回??
  • ????@SuppressWarnings("unchecked")??
  • ????public?List<Node<T>>?getAllNodes(){??
  • ????????List<Node<T>>?list?=?new?ArrayList<Node<T>>();??
  • ????????for(int?i=0;i<this.size;i++){??
  • ????????????if(this.nodes[i]?!=?null){??
  • ????????????????list.add((Node<T>)nodes[i]);??
  • ????????????}??
  • ????????}??
  • ????????return?list;??
  • ????}??
  • ??????
  • ????//獲取樹的深度,只有根節(jié)點(diǎn)時(shí)為1??
  • ????@SuppressWarnings("unchecked")??
  • ????public?int?getDepth(){??
  • ??????????
  • ????????int?max?=?1;??
  • ????????if(this.nodes[0]?==?null){??
  • ????????????return?0;??
  • ????????}??
  • ??????????
  • ????????for(int?i=0;i<this.count;i++){??
  • ????????????int?deep?=?1;??
  • ????????????int?location?=?((Node<T>)(this.nodes[i])).getParent();??
  • ????????????while(location?!=?-1?&&?this.nodes[location]?!=?null){??
  • ????????????????location?=?((Node<T>)(this.nodes[location])).getParent();??
  • ????????????????deep++;??
  • ????????????}??
  • ????????????if(max?<?deep){??
  • ????????????????max?=?deep;??
  • ????????????}??
  • ????????}??
  • ????????return?max;??
  • ????}??
  • ??????
  • ????public?void?enlarge(){??
  • ????????this.size?=?this.size?+?this.DEFAULT_SIZE;??
  • ????????Object[]?newNodes?=?new?Object[this.size];??
  • ????????newNodes?=?Arrays.copyOf(nodes,?this.size);??
  • ????????Arrays.fill(nodes,?null);??
  • ????????this.nodes?=?newNodes;??
  • ????????System.out.println("enlarge");??
  • ????}??
  • }??

  • 最后,使用MyTreeClient來測(cè)試下,基本功能是否都已經(jīng)實(shí)現(xiàn);

    [java]?view plaincopy
  • package?my.tree;??
  • ??
  • public?class?MyTreeClient?{??
  • ????public?static?void?main(String[]?args){??
  • ????????Node<String>?root?=?new?Node<String>("A",-1);??
  • ????????MyTree<String>?tree?=?new?MyTree<String>(root);??
  • ????????Node<String>?b?=?new?Node<String>("B");??
  • ????????Node<String>?c?=?new?Node<String>("C");??
  • ????????Node<String>?d?=?new?Node<String>("D");??
  • ????????Node<String>?e?=?new?Node<String>("E");??
  • ????????Node<String>?f?=?new?Node<String>("F");??
  • ????????Node<String>?g?=?new?Node<String>("G");??
  • ????????tree.add(b,root);??
  • ????????tree.add(c,root);??
  • ????????tree.add(d,root);??
  • ??????????
  • ????????tree.add(e,b);??
  • ????????tree.add(f,b);??
  • ????????tree.add(g,f);??
  • ??????????
  • ??????????
  • ????????System.out.println(tree.getSize());??
  • ????????System.out.println(tree.getRoot().getData());??
  • ????????System.out.println(tree.getAllNodes());??
  • ????????System.out.println(tree.getDepth());??
  • ????????tree.add(new?Node<String>("H"),g);??
  • ????????System.out.println(tree.getDepth());??
  • ????????tree.enlarge();??
  • ????}??
  • } ?
  • 總結(jié)

    以上是生活随笔為你收集整理的java实现树的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。