chapter_tree/summary/ #122
Replies: 15 comments 10 replies
-
什么时候可以继续讲一下红黑树、哈夫曼树、B树、B+树、B-树等~ |
Beta Was this translation helpful? Give feedback.
-
希望提高红黑树的优先级 |
Beta Was this translation helpful? Give feedback.
-
哈夫曼树进行编码 //根节点
public HuffmanNode root;
//编码表
public HashMap<Byte, String> codingForm;
/**
* 根据key-value创建huffman编码树
* 和之前的一样不过加入的data
*
* @param counter 节点信息
*/
public void createCodingTree(HashMap<Byte, Integer> counter)
{
// 管理节点
ArrayList<HuffmanNode> huffmanNodes = new ArrayList<>();
// 创建节点
for (Byte key : counter.keySet())
{
huffmanNodes.add(new HuffmanNode(key, counter.get(key)));
}
// 最后会剩下一个根节点
while (huffmanNodes.size() > 1)
{
// 1.按照权值对节点排序
huffmanNodes.sort(new Comparator<HuffmanNode>()
{
@Override
public int compare(HuffmanNode o1, HuffmanNode o2)
{
return o1.weight - o2.weight;
}
});
// 2.构建二叉树
// 2.1.获取权值最小的两个节点
HuffmanNode first = huffmanNodes.get(0);
HuffmanNode second = huffmanNodes.get(1);
// 2.2.创建父节点
HuffmanNode parent = new HuffmanNode(first.weight + second.weight);
// 2.3.构建二叉树
parent.left = first;
parent.right = second;
// 3.删除两个节点并将父节点加入
huffmanNodes.remove(0);
huffmanNodes.remove(0);
huffmanNodes.add(parent);
}
this.root = huffmanNodes.get(0);
}
/**
* 建立编码表
* 编码为向左为0向右为1
*/
public void createCodingForm(String code, HuffmanNode node)
{
// 叶节点
if (node.data != null)
{
// 添加
codingForm.put(node.data.byteValue(), code);
return;
}
StringBuilder codeTemp = new StringBuilder(code);
// 记录左编码
codeTemp.append(0);
createCodingForm(codeTemp.toString(), node.left);
// 删除左编码添加右编码
codeTemp.delete(codeTemp.length() - 1, codeTemp.length());
codeTemp.append(1);
createCodingForm(codeTemp.toString(), node.right);
} |
Beta Was this translation helpful? Give feedback.
-
没有B树和B+树吗? |
Beta Was this translation helpful? Give feedback.
-
有没有三叉树和四叉树,多叉树? |
Beta Was this translation helpful? Give feedback.
-
K神,催更红黑树、B+树等 |
Beta Was this translation helpful? Give feedback.
-
OmyGOD,K神,还会有机会讲解红黑树吗 |
Beta Was this translation helpful? Give feedback.
-
在二叉搜索树代码里没有找到build_tree方法啊 |
Beta Was this translation helpful? Give feedback.
-
最后一个问题错了吧,最底层之前不就是倒数第二层,2^(h-1) |
Beta Was this translation helpful? Give feedback.
-
没有线索二叉树的部分呀 |
Beta Was this translation helpful? Give feedback.
-
讲讲红黑树呗 大神 |
Beta Was this translation helpful? Give feedback.
-
树的章节太少了吧,就二叉树和avl树,红黑树,B树,B+树,霍夫曼树都没有 |
Beta Was this translation helpful? Give feedback.
-
chapter_tree/summary/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_tree/summary/
Beta Was this translation helpful? Give feedback.
All reactions