binary-tree

Is it possible to make efficient pointer-based binary heap implementations?

和自甴很熟 提交于 2020-01-20 02:43:06
问题 Is it even possible to implement a binary heap using pointers rather than an array? I have searched around the internet (including SO) and no answer can be found. The main problem here is that, how do you keep track of the last pointer? When you insert X into the heap, you place X at the last pointer and then bubble it up. Now, where does the last pointer point to? And also, what happens when you want to remove the root? You exchange the root with the last element, and then bubble the new

How do I represent binary numbers in C++ (used for Huffman encoder)?

家住魔仙堡 提交于 2020-01-17 07:50:12
问题 I am writing my own Huffman encoder, and so far I have created the Huffman tree by using a minHeap to pop off the two lowest frequency nodes and make a node that links to them and then pushing the new node back one (lather, rinse, repeat until only one node). So now I have created the tree, but I need to use this tree to assign codes to each character. My problem is I don't know how to store the binary representation of a number in C++. I remember reading that unsigned char is the standard

Seg Fault in program when parsing and storing strings in binary tree

夙愿已清 提交于 2020-01-16 04:57:21
问题 Right now I'm not even getting a seg fault with my program, I'm pretty sure it just runs forever, because when I run the program no output is produced, not even the segmentation fault. Basically what I want my program to do is read in a file of 4 lines. Then we parse the strings using the user created tokenizer function and the already created strtok function. Then I want to make a binary tree out of the commands that are read in. So like I said earlier my program produces no output at the

Declare a Binary Tree which takes a Generic type of Node which contains a Generic type of key value?

China☆狼群 提交于 2020-01-15 19:14:17
问题 If I have a node class(es) that can accept a generic type for it's key value: class Node<K extends Comparable<K>> implements Comparable<Node<K> { ... } class KeyValueNode<K extends Comparable<K>, V> extends Node<K> { ... } Is it possible to declare a generic binary tree class that accepts a generic type of node, which can contain a generic type of key value? I thought it would look something like this.... class BinaryTree<N<K>> { N<K> root; BinaryTree<N<K>> left, right; ... } Apologies for

AVL Tree for Java

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 03:51:06
问题 I'm not sure if I'm doing this correctly as this is my very first time coding with nodes. But here's my code so far, if someone can look over it and help me out with understanding if I'm doing anything wrong. Also my insert/remove methods are giving me a hard time. The professor gave us the pseudocode for that but I can't seem to grasp how to interpret it into Java code as I've never done this type of code before. Mostly because there's a if statement that checks the heights to see if the

Level-order tree traversal

岁酱吖の 提交于 2020-01-15 03:11:36
问题 I am trying to write a method that will take an IntTree as a parameter and return a Queue with the values of the IntTree in level order. To clarify: an IntTree is a tree with an integer value as its root, and has an IntTree as its left and right subtrees. A note on some methods: value(t) - returns the integer value at the root of the tree left(t) - returns the left IntTree subtree // right(t) - returns the right sub-tree Here's the code I have so far. I'm rather new to programming, so I'm

Rudimentary Tree, and Pointers, in Rust

喜夏-厌秋 提交于 2020-01-14 18:50:31
问题 Coming from a scripting language background with some C, trying to 'learn' Rust leads me to question my competence. I'm trying to figure out how to change an owned pointer, and struggling to do it. Besides copying in from the extra libs, I can't figure out the recursion I need on a binary tree. Particularly, I don't know how to swap out the pointer branches. Whereas with a linked list I can cheat and use a temporary vector to return a new list, or prepend a new Cons(value, ~Cons) to the list

Rudimentary Tree, and Pointers, in Rust

女生的网名这么多〃 提交于 2020-01-14 18:49:27
问题 Coming from a scripting language background with some C, trying to 'learn' Rust leads me to question my competence. I'm trying to figure out how to change an owned pointer, and struggling to do it. Besides copying in from the extra libs, I can't figure out the recursion I need on a binary tree. Particularly, I don't know how to swap out the pointer branches. Whereas with a linked list I can cheat and use a temporary vector to return a new list, or prepend a new Cons(value, ~Cons) to the list

LCA of Binary Tree Issue

北城余情 提交于 2020-01-14 06:05:29
问题 I have written a code for finding least common ancestor of nodes in binary tree but it seems to return null instead of the LCA. My algorithm is as below. Recursively find the ancestor in left and right branch of tree A node where left as well as right tree has matching elements in the LCA. public class LCA { public static BinaryTreeNode findLCA( BinaryTreeNode root, BinaryTreeNode node1 , BinaryTreeNode node2) { if (root == null) { return null; } BinaryTreeNode left = root.getLeft();

LCA of Binary Tree Issue

感情迁移 提交于 2020-01-14 06:04:20
问题 I have written a code for finding least common ancestor of nodes in binary tree but it seems to return null instead of the LCA. My algorithm is as below. Recursively find the ancestor in left and right branch of tree A node where left as well as right tree has matching elements in the LCA. public class LCA { public static BinaryTreeNode findLCA( BinaryTreeNode root, BinaryTreeNode node1 , BinaryTreeNode node2) { if (root == null) { return null; } BinaryTreeNode left = root.getLeft();