From eff1a986adfcb8c669c94a9cad3a9034dfcdc33c Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 3 Oct 2012 15:09:19 -0400 Subject: [PATCH] adding huffman node and a context by which to increment the id --- .../java/com/hashbangbash/trie/HuffNode.java | 67 +++++++++++++++++++ .../com/hashbangbash/trie/NodeContext.java | 25 +++++++ 2 files changed, 92 insertions(+) create mode 100644 src/main/java/com/hashbangbash/trie/HuffNode.java create mode 100644 src/main/java/com/hashbangbash/trie/NodeContext.java diff --git a/src/main/java/com/hashbangbash/trie/HuffNode.java b/src/main/java/com/hashbangbash/trie/HuffNode.java new file mode 100644 index 0000000..80b6272 --- /dev/null +++ b/src/main/java/com/hashbangbash/trie/HuffNode.java @@ -0,0 +1,67 @@ +package com.hashbangbash.trie; + +public class HuffNode { + private long id = 0; + private Object value = null; + private int weight = 0; + private HuffNode left = null; + private HuffNode right = null; + private NodeContext ctx = null; + + public HuffNode(Object value, int weight, HuffNode left, HuffNode right) { + this(new NodeContext(), value, weight, left, right); + } + + public HuffNode(NodeContext ctx, Object value, int weight, HuffNode left, HuffNode right) { + this.ctx = ctx; + this.value = value; + this.weight = weight; + this.left = left; + this.right = right; + this.id = this.ctx.nextId(); + } + + public HuffNode(Object value, int weight) { + this(new NodeContext(), value, weight); + } + + public HuffNode(NodeContext ctx, Object value, int weight) { + this.ctx = ctx; + this.value = value; + this.weight = weight; + this.id = this.ctx.nextId(); + } + + public NodeContext getContext() { + return this.ctx; + } + + public long getId() { + return this.id; + } + + public Object getObject() { + return this.value; + } + + public int getWeight() { + return this.weight; + } + + public HuffNode getLeft() { + return this.left; + } + + public HuffNode getRight() { + return this.right; + } + + public String toString() { + return "ID: " + id + + ", Value " + value + + ", Weight: " + weight + + ", Left: " + left + + ", Right: " + right; + } +} + diff --git a/src/main/java/com/hashbangbash/trie/NodeContext.java b/src/main/java/com/hashbangbash/trie/NodeContext.java new file mode 100644 index 0000000..3ed5c00 --- /dev/null +++ b/src/main/java/com/hashbangbash/trie/NodeContext.java @@ -0,0 +1,25 @@ +package com.hashbangbash.trie; + +/* + * for nodes like the HuffNode, that need + * an external incrementor to the node id + */ +public class NodeContext { + private long huffNodeId = 0; + + public NodeContext() { + } + + public NodeContext(long startId) { + this.huffNodeId = startId; + } + + public long getId() { + return this.huffNodeId; + } + + public long nextId() { + return this.huffNodeId++; + } +} +