binary-tree

Haskell: Get path from every leaf node to root in a Bin tree structure

筅森魡賤 提交于 2019-12-12 02:09:00
问题 I've got work to do, but i dunno how to do it. I have Bin tree 1 / \ 2 3 / \ / \ 4 5 6 7 And I need to find the way from root to Node with the coord [i, j]. For example: (2, 2) -> [1, 3, 6] fromRoot :: Int -> Int -> Tree a -> [a] I wrote some function for Index and BinTree, but how to make main function i dont know. data Tree a = Node (Tree a) a (Tree a) index :: Tree a -> Int -> Int -> a index (Node _ x _ ) 0 _ = x index (Node l x r) i j | ((border i)<j) = index r (i-1) (j-(border i)-1) |

Having trouble understanding tree traversal recursive functions

十年热恋 提交于 2019-12-12 01:23:34
问题 I am having some trouble understanding the recursive functions involved in preorder, inorder, and postorder tree traversal. I have some knowledge of recursion (but admittedly its not my strong suit). All of the seem to call themselves twice first making a call with the left child of the root and then with the right child. But how exactly is this possible? Wouldn't the call to the preOrder function with the left child return the flow of control back to the top, and the next call would never be

Prove that a binary tree with n leaves has a height of at least log n

孤人 提交于 2019-12-11 23:20:50
问题 I know that the number of leaves in a binary tree of height h can be at most 2^h , and I also know how to prove this using induction. Where do I go from here? I found this previously answered question, but this doesn't make any sense to me because I don't understand what the proof by contradiction in the theorem section has anything to do with a binary tree's height being at least log(n) . I was expecting him to talk about how log(n) relates to the number of leaves and height, but instead he

temporary pointer point to a using vector address

↘锁芯ラ 提交于 2019-12-11 19:17:56
问题 I have a question about parsing the string s into a binary tree. struct TreeNode { string val; // The data in this node. TreeNode *left; // Pointer to the left subtree. TreeNode *right; // Pointer to the right subtree. }; string s="((OR (AND pass (NOT reject)) (AND (NOT pass) reject)))"; I do some stroke and eliminate the "(" and ")" and keep all the separate part in vector aftersplit, aftersplit has (bottom) OR AND pass NOT reject AND NOT pass reject (back) vector<string> aftersplit; vector

Building a decision tree from two lists

五迷三道 提交于 2019-12-11 16:45:05
问题 I'm trying to build this decision tree through two lists that I have. input: records= ['dead','healthy','cold','influenza'] symptoms= ['cough','sneezing','fever'] (doesn't always have to be this exact lists can be different lengths etc..) the records list represent the leafs in the tree output: cough Yes / \ No sneezing sneezing Yes / \ No Yes / \ No fever fever fever fever Yes / \ No Yes/ \No Yes / \ No Yes/ \No dead cold influenza cold dead influenza cold healthy my code: def buildtree

C++ Binary Search Tree Insert via Recursion

拥有回忆 提交于 2019-12-11 15:58:35
问题 So my code is below. I'm not getting any errors and it places everything in the node just fine. But based on my debug statements Everytime anything is inserted it's finding the root. I'm not sure if that is right. But according to output file for the assignment, my answers are different when it comes to the height of the tree, the traversals, and I just flat am still having troubles with my leaf count function. Another story though. Based on the debug statements it looks like everything is

C++: operator<< overloading in the nested classes

六月ゝ 毕业季﹏ 提交于 2019-12-11 12:32:24
问题 This question has a detailed answer here: Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’ I am trying to overload a nested subclass, and spent an hour trying to overload operator<< . Researched a little here, but still couldn't resolve it. Any help? :) Whenever I try compiling it using g++ -std=c++11 -lm -ggdb -g -O0 -Wall p_7_1.cpp -o p_7_1 , it gives me an error: Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream<char, std::__1::char

Build binary expression tree from prefix notation?

痴心易碎 提交于 2019-12-11 12:23:59
问题 import java.util.ArrayList; import java.util.List; public class ExpressionTree { List<String> expArray = new ArrayList<String>(); ExpressionTreeNode root; ExpressionTreeNode curNode; ExpressionTreeNode left; ExpressionTreeNode right; String element; public ExpressionTree(String prefixExpression) { String[] temp = prefixExpression.split(" "); for (int i = 0; i < temp.length; i++) { expArray.add(temp[i]); } root = createExpressionTree(expArray); System.out.println(root); } private

What's tight time complexity of this algorithm for Binary Tree Zigzag Level Order Traversal?

自古美人都是妖i 提交于 2019-12-11 12:08:42
问题 Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (i.e.from left to right, then right to left for the next level andalternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: [ [3], [20,9], [15,7] ] Personally I think , time complexity = O(n * height), n is the number of nodes, height is the height of the given binary tree. getHeight() => O(n)

Decoding Huffman Tree

試著忘記壹切 提交于 2019-12-11 11:56:07
问题 I am implementing a function that takes in a tree and an encoded string. Example: decode(*Huffmantree, "10010101010") I want this function to return decoded string for the encoded string in the input relative to the Huffman tree input. The code I have so far: string decode(NodePtr root, string encoded_str) { string temp = ""; for (int i = 0 ; i < encoded_str.size() ; i++) { if (root->is_leaf() == true) { temp[i] = root->letter; //cout << root->letter; } if (root->left != NULL) { encoded_str.