/** * Creates binary tree from the linked list. * * @param linkedList Orderer ByteData as linked list */ public void createBinaryTreeFromLinkedList(ByteDataLinkedList linkedList) { linkedList.startIteration(); while (linkedList.checkObject() != null) { this.root = new ByteData((byte)0); ByteData left = linkedList.nextObject(); ByteData right = linkedList.nextObject(); this.root.setLeftChild(left); this.root.setRightChild(right); left.setParent(this.root); right.setParent(this.root); this.root.setCount(left.getCount() + right.getCount()); linkedList.add(this.root); } }
/** * Adds new object to the right place in the linked list according to the count value. * * @param newByteData New object */ public void add(ByteData newByteData) { ByteData current = this.first; while (true) { if (newByteData.getCount() < current.getCount() || current == this.last) { newByteData.setPrevious(current.getPrevious()); newByteData.setNext(current); newByteData.getPrevious().setNext(newByteData); newByteData.getNext().setPrevious(newByteData); break; } current = current.getNext(); } }
public void binaryTreeGenerationFromTheLinkedListWorks() { Assert.Equal(null, this.byteDataBinaryTree.getRoot()); this.byteDataBinaryTree.createBinaryTreeFromLinkedList(this.byteDataLinkedList); ByteData root = this.byteDataBinaryTree.getRoot(); Assert.True(root != null); Assert.Equal(10, root.getCount()); Assert.Equal(byteData4, root.getLeftChild()); Assert.Equal(root, root.getLeftChild().getParent()); Assert.Equal(6, root.getRightChild().getCount()); Assert.Equal(root, root.getRightChild().getParent()); Assert.Equal(byteData3, root.getRightChild().getLeftChild()); Assert.Equal(3, root.getRightChild().getRightChild().getCount()); Assert.Equal(byteData1, root.getRightChild().getRightChild().getLeftChild()); Assert.Equal(byteData2, root.getRightChild().getRightChild().getRightChild()); Assert.Equal(root, root.getRightChild().getRightChild().getRightChild().getParent().getParent().getParent()); }