Convert linked list to tree in Java -



Convert linked list to tree in Java -

i have create conversion singly, doubly , circular linked list binary tree.

i have made singly linked list , having problem understanding how convert tree.

my singly linked list node following:

public class node { node next; object data; public node(object _data) { next = null; info = _data; } public node(object _data, node _next) { next = _next; info = _data; } public object getdata() { homecoming data; } public void setdata(object _data) { info = _data; } public node getnext() { homecoming next; } public void setnext(node _next) { next = _next; } }

and singly linked list class:

import java.util.collection; import java.util.collections; public class linkedlist { private node head; private int listcount; public linkedlist() { head = new node(null); listcount = 0; } public void add(object data) { node temp = new node(data); node current = head; head.setdata(temp.getdata()); while(current.getnext() != null) { current = current.getnext(); } current.setnext(temp); listcount++; } public void add(object data, int index) { node temp = new node(data); node current = head; head.setdata(temp.getdata()); (int = 1; < index && current.getnext() != null; i++) { current = current.getnext(); } temp.setnext(current.getnext()); current.setnext(temp); listcount++; } public object get(int index) { if(index <= 0) { homecoming null; } node current = head.getnext(); for(int = 1; < index; i++) { if(current.getnext() == null) { homecoming null; } current = current.getnext(); } homecoming current.getdata(); } public int size() { homecoming listcount; } public boolean remove(int index) { if(index < 1 || index > size()) { homecoming false; } node current = head; for(int = 1; < index; i++) { if(current.getnext() == null) { homecoming false; } current = current.getnext(); } current.setnext(current.getnext().getnext()); listcount--; homecoming true; } public boolean contains(object data) { node temp = new node(data); node current = head; for(int = 0; < size(); i++) { if(temp.getdata().equals(current.getdata())) { homecoming true; } else { current = current.getnext(); } } homecoming false; } public node inserafter(object data, node n) { node temp = new node(data); node current = n.getnext(); temp.setnext(current); n.setnext(temp); homecoming temp; } public void rotateleft() { node temp = head; if (head != null) { //otherwise empty list if (head.getnext() != null) { //otherwise single item list head = head.getnext(); } } node tail; if (head.getnext() != null) { tail = head.getnext(); } else { tail = head; } while(tail.getnext() != null) { if (tail.getnext() != null) { tail = tail.getnext(); } } tail.setnext(temp); temp.setnext(null); } public void rotateright() { node temp = null; node current = head; while(current.getnext() != null) { temp = current; current = current.getnext(); } current.setnext(head); head = current; temp.setnext(null); } public void reverse() { node reversedpart = null; node current = head; while(current != null) { node next = current.next; current.next = reversedpart; reversedpart = current; current = next; } head = reversedpart; } public node copylist(node source) { node copyhead = null; node copytail = null; node temp = new node(source); node current = head.getnext(); for(int = 0; < size(); i++) { node newnode = new node(temp.getdata()); if(copyhead == null) { copyhead = newnode; copytail = copyhead; } else { copytail.setnext(newnode); copytail = copytail.getnext(); } } homecoming copyhead; } public object setdataindexof(object data, int index) { node node = nodeat(index); if(node == null) { homecoming null; } else { object old = node.getdata(); node.setdata(data); homecoming old; } } public object dataat(int index) { node current = head.getnext(); if(index < 1 || index > size()) { homecoming null; } for(int = 0; < index; ++) { if(i != index - 1) { current = current.getnext(); } } homecoming current.getdata(); } public node nodeat(int index) { node current = head.getnext(); if(index < 1 || index > size()) { homecoming null; } for(int = 0; < index; i++) { if(i != index - 1) { current = current.getnext(); } } homecoming current; } public int indexof(object data) { node temp = new node(data); node current = head.getnext(); for(int = 0; < size(); i++) { if(current.getdata().equals(temp.getdata())) { homecoming i; } current = current.getnext(); } homecoming -1; } public object min() { integer min = (integer)head.getdata(); node current = head; while(current.getnext() != null) { if((integer)current.getdata() < min) { min = (integer)current.getdata(); } current = current.getnext(); } homecoming min; } public object max() { integer max = (integer)head.getdata(); node current = head; while(current.getnext() != null) { if((integer)current.getdata() > max) { max = (integer)current.getdata(); } current = current.getnext(); } homecoming max; } public void removesecondappear(object data) { node temp = new node(data); node current = head; node previous = null; boolean found = false; while(current != null) { if(current.getdata().equals(temp.getdata()) && current.getdata() != null) { if(found == true) { previous.setnext(current.getnext()); break; } else if(found == false) { found = true; } } else{ found = false; } previous = current; current = current.getnext(); } } public string tostring() { node current = head.getnext(); string output = ""; while(current != null) { output += "[" + current.getdata().tostring() + "]"; current = current.getnext(); } homecoming output; } }

having singly linked list created, have create new tree based on list. lost , have no thought how start. never done similar before. help appreciate.

firstly you'll need implement binary search tree. had google , there plenty of illustration java implementations can larn from. binary search tree should have insert method takes 1 parameter, ideally unbounded type given list implementation go object now:

public void insert(object element);

once bst built, iterate through list , add together elements tree follows:

linkedlist list = new linkedlist(); list.add("foo"); list.add("bar"); list.add("baz"); list.add("etc"); binarysearchtree bst = new binarysearchtree(); for(object o:list) { bst.add(o); }

java tree linked-list binary-tree

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -