binary-tree

How to fill a binary tree from a string, java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 21:34:16
问题 I'm suppose to fill a binary tree with integers based upon a string like this. [int](LT)(RT) LT is an expression of the same form for the left part of the tree. Same with RT. A valid string would be something like this: 4(2(1)(3))(6(5)(7) . How can I fill this tree? This is not a sorted tree of any kind. So it can just fill every "level" with nodes. Thank you for help. 回答1: You have to create a parser for that and fill some kind of data structure with the instructions from your parser. Then

Binary Search Tree insertion doesn't work

感情迁移 提交于 2019-12-24 21:17:55
问题 I am following a book ,Problem Solving & Program Design in C, to learn C. In this book, they gave all necessary parts to build a binary search tree.... But, My implementation didn't work. Here is insertion part; void add_to_t(tree_node_t *oldTreep, // input/output - binary search tree tree_element_t ele) // input - element to add { oldTreep = tree_insert(oldTreep, ele); } tree_node_t * tree_insert(tree_node_t *oldTreep, tree_element_t ele) { if(oldTreep == NULL){ oldTreep = TYPED_ALLOC(tree

c++ delete specific node with two children from binary search tree

随声附和 提交于 2019-12-24 21:00:16
问题 I'm currently working on a program to handle a BST in c++. I have all of my functions currently working, except removeNode, which deletes a node of given key value in the tree. I think the first two cases work, but the 3rd is giving me trouble. I know the logic of how to delete a node with two children, but the code is not working for me at the moment. Here is node struct node{ int key; node* left; node* right; }; And here is the code for the delete function, with the case of a node with more

How can I get number of leaf nodes in binary tree non-recursively?

此生再无相见时 提交于 2019-12-24 20:21:22
问题 I have a practice question that I'm stumped on - to get the number of leaf nodes in a binary tree without using recursion. I've had a bit of a look around for ideas, I've seen some such as passing the nodes to a stack, but I don't see how to do it when there's multiple branches. Can anyone provide a pointer? 回答1: NumberOfLeafNodes(root); int NumberOfLeafNodes(NODE *p) { NODE *nodestack[50]; int top=-1; int count=0; if(p==NULL) return 0; nodestack[++top]=p; while(top!=-1) { p=nodestack[top--];

Recursive Insert for Binary Tree

半世苍凉 提交于 2019-12-24 14:25:55
问题 I'm working on code for insertion into a binary search tree. It works for the first node I insert, making it the root, but after that it doesn't seem to insert any nodes. I'm sure it's a problem with setting left/right references, but I can't quite figure it out. Please help! //params: key of node to be inserted, parent node public void insert(int newKey, TreeNode parent){ //if the root of the tree is empty, insert at root if(this.getRoot() == null){ this.root = new TreeNode(newKey, null,

Is there a way to control line angle when producing graphs with pyDot

走远了吗. 提交于 2019-12-24 13:17:16
问题 I wrote a python class to display and animate binary search trees. Yet the graphs produced by the pyDot edge and node commands don't seem to allow me to control the angle or direction of the arrows connecting each element of my tree. There are lots of controls for shape of the nodes but there does not seem to be any control for angle of the lines. I was hoping to find some kind of minimum angle parameter to add to my edges as they are added to the graph? This is a sample of my code where I

Understanding pseudocode to construct tree from preorder traversal

回眸只為那壹抹淺笑 提交于 2019-12-24 11:04:43
问题 I need to do something similar to the task described in this question: Construct tree with pre-order traversal given There is a really helpful answer here, but I don't understand the pseudocode fully, so I was wondering if someone could help describe to me what is going on. k = 0 // Initialize input = ... get preorder traversal vector from user ... // Get input Reconstruct(T) // Reconstruct method with tree input if input[k] == N // If element of input is N T = new node with label N // Make a

Binary Tree Recursive Function

大憨熊 提交于 2019-12-24 10:31:51
问题 I need to print out a binary tree that looks like this: --------x------- ----x-------x--- --x---x---x---x- -x-x-x-x-x-x-x-x xxxxxxxxxxxxxxxx Using recursion to print the left side of the line and the right side of the line with the exception of the first line. So the function would call a display function with parameters of the left starting point and the right ending point. Then it calls itself twice, on for the left side and one for the right. #include <stdio.h> #define LENGTH 16 void

Shortest path between two nodes in an infinite, complete binary tree?

喜你入骨 提交于 2019-12-24 07:30:06
问题 Suppose we have an infinite, complete binary tree where the nodes are numbered 1, 2, 3, ... by their position in a layer-by-layer traversal of the tree. Given the indices of two nodes u and v in the tree, how can we efficiently find the shortest path between them? Thanks! 回答1: @Jonathan Landrum pointed out the solution in his comment. This answer fleshes out that solution. In any tree, there is exactly one path between any two nodes. Therefore, this problem boils down to determining the

Is it possible to get a p-value for nodes in a categorical tree analysis with R?

强颜欢笑 提交于 2019-12-24 05:23:07
问题 Is it possible to get a p-value for nodes in a categorical tree analysis with R? I am using rpart and can't locate a p-value for each node. Maybe this is only possible with a regression and not categories. structure(list(subj = c(702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L, 702L), visit = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), run = structure