binary-tree

Binary Tree Traversal Sum Of Each Depth

南笙酒味 提交于 2019-12-24 04:28:08
问题 I am looking for an algorithm or modification of an efficient way to get the sum of run through of a tree depth, for example: Z / \ / \ / \ / \ X Y / \ / \ / \ / \ A B C D The number of final leaves is four so us have four final sums and they would be this respectively. [Z+X+A] [Z+X+B] [Z+Y+C] [Z+Y+D] If someone could guide me in the right direction into getting the sums of all possible depths, that would be great. This will be done in python with fairly large trees. 回答1: You can recurse over

Binary tree for strings c

南笙酒味 提交于 2019-12-24 03:41:12
问题 I'm trying to implement a binary tree capable of holding strings in c. After having the code to work for ints, I tried altering it slightly to handle char arrays. Now I seem to have totally broke the code and don't know how. Any help appreciated. #include <stdio.h> #include <stdlib.h> //struct for node struct node { void *value; struct node *p_left; struct node *p_right; }; //use typedef to make calling the compare function easier typedef int (*Compare)(const void *, const void *); //inserts

Vector based binary tree traversal

萝らか妹 提交于 2019-12-24 03:40:28
问题 I have a vector based binary tree and need to apply a function to each value in the tree using various methods of traversal. The preorder traversal was very easy to implement with a recursive function but I have been having trouble doing the same with the inorder and postorder traversals. If anyone could help out that would be great! Some extra information that I should have included: I am using a vector of nodes, each node containing a boolean variable stating whether or not that node is

What is a use case for an unordered Binary Tree?

◇◆丶佛笑我妖孽 提交于 2019-12-24 03:06:11
问题 Does anyone use unordered Binary Trees? I don't see any advantage to using an unordered Binary Tree over an array. 回答1: To represent a simple expression language with operators of at most two arguments. + / \ 3 * / \ 4 5 回答2: Generating Unordered Binary Trees is necessary in both the graph theory and in different applications. For example, a list of all trees with a given number of internal nodes can be used in computer science to test or analyze an algorithm for its correctness or

Convert Binary Tree -> BST (maintaining original tree shape)

流过昼夜 提交于 2019-12-24 02:18:05
问题 I have a binary tree of some shape . I want to Convert it to BST search tree of same shape . Is it possible? I tried methods like - Do In-order traversal of Binary Tree & put contents into an array. Then map this into a BST keeping in mind the condition (left val <= root <= right val). This works for some cases but faile for others. P.S.: I had a look at this - Binary Trees question. Checking for similar shape. But It's easy to compare 2 BST's for similarity in shape. 回答1: The short answer is

Can you draw a binary tree given its pre-order binary sequence/ordering?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 12:32:49
问题 Binary trees (and hence ordered forests) can be represented as binary strings. The binary string is obtained by traversing a binary tree in preorder, recording a 1 for every node and a 0 for every empty subtree (null link). This means that if I'm given a binary tree, I can do a preorder traversal and produce a binary sequence representation. Is the opposite also possible? If I'm given this binary sequence 11011000101101010001 , can I draw the binary tree? 回答1: Yes you can. Label the internal

In Binary Tree, find how many grandfathers have only two or three grandchildrens

允我心安 提交于 2019-12-23 09:25:53
问题 8 / \ 4 12 / \ / \ 3 6 2 1 / \ / \ / / \ 7 10 13 15 5 9 11 / 14 I need to find the grandfathers of a tree, in this exemple I have only one grandfather, number 12 (I need that he has only two or three grandchildren) . This is what I tried so far: int T(struct node * tree){ int t = 0; if (tree == NULL) return 0; if (tree->left && tree->right) { //In this case i check if we NOT have all the four grandchildrens. if (!((tree->left->left) && (tree->left->right) && (tree->right->left) && (tree-

Algorithm for a XOR tree traverse [closed]

余生长醉 提交于 2019-12-23 06:21:28
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I have a binary tree and its internal nodes consist of 'AND' or 'XOR'. Leaf nodes have also and data members. I need to print all possible paths by using stack(non-recursive). I have searched for traversing tree with stack, but its algorithm doesn't hold in my case since postorder,preorder or inorder cannot be

In order recursion in binary trees

余生长醉 提交于 2019-12-23 06:12:42
问题 Can someone please explain to me how recursion works in in Order traversal. here's my inOrder() method. public void inOrder(BinaryNode p){ if(p.left!=null){ inOrder(p.left); } visit(p); if(p.right!=null){ inOrder(p.right); } } public void visit(BinaryNode p){ System.out.println(p.element); } BinaryTree t=new BinaryTree(); t.insert(5); t.insert(t.root,4); t.insert(t.root,6); t.insert(t.root,60); t.insert(t.root,25); t.insert(t.root,10); t.inOrder(t.root); The method inOrder() prints the

In order recursion in binary trees

做~自己de王妃 提交于 2019-12-23 06:12:12
问题 Can someone please explain to me how recursion works in in Order traversal. here's my inOrder() method. public void inOrder(BinaryNode p){ if(p.left!=null){ inOrder(p.left); } visit(p); if(p.right!=null){ inOrder(p.right); } } public void visit(BinaryNode p){ System.out.println(p.element); } BinaryTree t=new BinaryTree(); t.insert(5); t.insert(t.root,4); t.insert(t.root,6); t.insert(t.root,60); t.insert(t.root,25); t.insert(t.root,10); t.inOrder(t.root); The method inOrder() prints the