binary-tree

Convert recursive binary tree traversal to iterative

馋奶兔 提交于 2019-12-05 20:52:11
I was asked to write the iterative version, but I wrote the recursive version i.e. void inorderTraverse(BinaryTree root) { if(root==NULL) printf("%d",root->id); else { inorderTraverse(root->left); printf("%d",root->id); inorderTraverse(root->right); } } I'm not looking for the code, I want to understand how this can be done. Had it been just the last recursive call, I would have done void inorderTraverse(BinaryTree root) { while(root!=NULL) { printf("%d",root->id); root=root->right; } } But how do I convert to an iterative program when there are two recursive calls? Here are the type

generating a binary tree from given data in python

眉间皱痕 提交于 2019-12-05 19:20:40
i wanted to know how to read values from a list into a binary tree. i have a triangle like this: 0 1 2 3 4 5 6 7 8 9 i have written a class node like this class node: def __init__(self,data,left=None,right=None): self.data=data self.left=left self.right=right basically what i want to do is something like this node(0,node(1),node(2)) i want to make a recursive function that can handle much bigger triangles. Can somehow tell me what i am supposed to do? edit: quite clearly binary tree is not the way to approach this problem. what i basically want to find out are all the different combination's

Is the root node an internal node?

∥☆過路亽.° 提交于 2019-12-05 17:26:32
问题 So I've looked around the web and a couple of questions here in stackoverflow here are the definition: Generally, an internal node is any node that is not a leaf (a node with no children) Non-leaf/Non-terminal/Internal node – has at least one child or descendant node with degree not equal to 0 As far as i understand it, it is a node which is not a leaf. I was about to conclude that the root is also an internal node but there seems to be some ambiguity on its definition as seen here: What is

freeing memory of a binary tree C

随声附和 提交于 2019-12-05 17:20:33
I would like to free memory from my allocated binary tree what traversal is the best for doing so? typedef struct Node{ struct Node * right; struct Node * left; void * data; }Node; typedef int (*cmp) (void*,void *); Node* init(void * element){ Node * newNode=(Node*)malloc(sizeof(Node)); newNode->data=element; newNode->left=NULL; newNode->right=NULL; return newNode; } void insert(void * element, Node** root,cmp compareTo){ if(*root==NULL){ *root=init(element); return; } if(compareTo(element,(*root)->data)==1) insert(element,&((*root)->left),compareTo); else insert(element,&((*root)->right)

Convert Preorder listing of a binary tree to postorder and vice versa

﹥>﹥吖頭↗ 提交于 2019-12-05 15:55:12
How can I find the preorder listing of a tree if only the postorder listing is given and vice versa. Also, in the tree, every non-leaf node has two children (i.e. Each node has either two or zero children.) EDIT: Another given assumption is that the label of each node is unique and has a field that will identify it as an internal node or a leaf. I think that ought to get rid of the ambiguity of a single preorder or postorder being able to uniquely identify a tree. Without assuming that nodes in the tree have a field that idenrify themselves as internal or leaf, you can't find a unique answer

Recursion that branches running out of memory

强颜欢笑 提交于 2019-12-05 14:44:06
I have a programming assignment that goes like this: You are given three numbers a, b, and c. (1 ≤ а, b, c ≤ 10^18) Each time you have two choises, either add b to a (a+=b), or add a to b (b+=a). Write a program that will print out YES or NO depending on whether you can get to c by adding a and b to each other. I've tried solving this problem using recursion that branches to two branches every time where one branch stores a+b, b and the other branch stores a, b+a. In every recursive call, the function checks the values of a and b, and if they are equal to c the search stops and the function

File indexing (using Binary trees?) in Python

大城市里の小女人 提交于 2019-12-05 14:12:34
Background I have many (thousands!) of data files with a standard field based format (think tab-delimited, same fields in every line, in every file). I'm debating various ways of making this data available / searchable. (Some options include RDBMS, NoSQL stuff, using the grep/awk and friends, etc.). Proposal In particular, one idea that appeals to me is "indexing" the files in some way. Since these files are read-only (and static), I was imagining some persistent files containing binary trees (one for each indexed field, just like in other data stores). I'm open to ideas about how to this, or

how to find the height of a node in binary tree recursively

痞子三分冷 提交于 2019-12-05 13:07:06
path = 0 # the lenght of the path while self.right != None or self.left != None: while self.right != None: self = self.right path = path +1 while self.left != None: self = self.left path = path +1 return path this is my sample code for find the Height, is defined as the length of the longest path by number of nodes from self to a leaf. The height of a leaf node is 1. it doesn't work. What you're doing isn't recursive, it's iterative. Recursive would be something like: def height(node): if node is None: return 0 else: return max(height(node.left), height(node.right)) + 1 You were given the

What would be the time complexity of counting the number of all structurally different binary trees?

我的梦境 提交于 2019-12-05 13:02:04
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key values 1...numKeys, how many structurally unique binary search trees are possible that store those keys? Strategy: consider that each value could be the root. Recursively find the size of the left and right subtrees. */ public static int countTrees(int numKeys) { if (numKeys <=1) { return(1); } else { // there will be one value at the root, with whatever remains // on the left and right each forming their own subtrees. // Iterate through all the values that

How to draw binary tree view using WPF?

核能气质少年 提交于 2019-12-05 12:42:17
I want to draw it like this Image : I can draw a binary tree on console. I want to draw it using WPF. Here is my code which I write for console. class Program { static void Main(string[] args) { List<BinaryTreeData> myBinaryData = new List<BinaryTreeData>(); myBinaryData.Add(new BinaryTreeData{ownID=1}); myBinaryData.Add(new BinaryTreeData { parentID=1, ownID = 2 }); myBinaryData.Add(new BinaryTreeData { parentID=1,ownID = 3 }); foreach (var item in myBinaryData) { Console.WriteLine("{0}------{1}", item.parentID, item.ownID); } } } class BinaryTreeData : INotifyPropertyChanged { private int