binary-tree

Generic binary search tree in C

血红的双手。 提交于 2019-12-01 08:21:45
I have the implemented a binary search tree but I also want to make it generic. The code is the following: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode; and the functions: treeNode* FindMin(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->left) /* Go to the left sub tree to find the min element */ return FindMin(node->left); else return node; } treeNode * Insert(treeNode *node,int data) { if(node==NULL) { treeNode *temp; temp = (treeNode *)malloc(sizeof(treeNode)); temp -> data = data; temp -> left

Java Algorithm for finding the largest set of independent nodes in a binary tree

岁酱吖の 提交于 2019-12-01 07:55:17
问题 By independent nodes, I mean that the returned set can not contain nodes that are in immediate relations, parent and child cannot both be included. I tried to use Google, with no success. I don't think I have the right search words. A link, any help would be very much appreciated. Just started on this now. I need to return the actual set of independent nodes, not just the amount. 回答1: You can compute this recursive function with dynamic programming (memoization): MaxSet(node) = 1 if "node" is

Binary tree transformation using rotations

∥☆過路亽.° 提交于 2019-12-01 06:46:15
While i was studying for midterm about binary trees, i found a statement that any arbitrary n-node binary tree can be transformed into any other n-node binary tree with at most 2*n-2 rotations. Is there any proof for that? I found some kind of proof with asymptotic notations but it was not that clear. I mean could someone explain/show why it is true? And if it says that n-node binary tree, does it include the root? This answer is from CLRS 3rd Edition textbook question 13.2-4. Let LEFT = an entire left linked list binary tree RIGHT = an entire right linked list binary tree. You can easily

Multiple Array Merge Using Binary Heap

陌路散爱 提交于 2019-12-01 06:37:45
Given k sorted arrays of integers, each containing an unknown positive number of elements (not necessarily the same number of elements in each array), where the total number of elements in all k arrays is n, give an algorithm for merging the k arrays into a single sorted array, containing all n elements. The algorithm's worst-case time complexity should be O(n∙log k). Name the k-sorted lists 1, ..., k. Let A be the name of the combined sorted array. For each list, i, pop v off of i and push (i, v) into a min-heap. Now the heap will contain pairs of value and list id for the smallest entries in

Micro optimisations iterating through a tree in C#

旧时模样 提交于 2019-12-01 06:20:37
问题 I'm working on a massive number crunching project. I've been optimising everything since the start as I knew it would be important. Doing performance analysis my code spends almost 40% of it's life in one function - the binary tree iterator. public ScTreeNode GetNodeForState(int rootIndex, float[] inputs) { 0.2% ScTreeNode node = RootNodes[rootIndex].TreeNode; 24.6% while (node.BranchData != null) { 0.2% BranchNodeData b = node.BranchData; 0.5% node = b.Child2; 12.8% if (inputs[b

Generic binary search tree in C

白昼怎懂夜的黑 提交于 2019-12-01 06:02:17
问题 I have the implemented a binary search tree but I also want to make it generic. The code is the following: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode; and the functions: treeNode* FindMin(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->left) /* Go to the left sub tree to find the min element */ return FindMin(node->left); else return node; } treeNode * Insert(treeNode *node,int data) { if(node=

Multiple Array Merge Using Binary Heap

走远了吗. 提交于 2019-12-01 04:58:26
问题 Given k sorted arrays of integers, each containing an unknown positive number of elements (not necessarily the same number of elements in each array), where the total number of elements in all k arrays is n, give an algorithm for merging the k arrays into a single sorted array, containing all n elements. The algorithm's worst-case time complexity should be O(n∙log k). 回答1: Name the k-sorted lists 1, ..., k. Let A be the name of the combined sorted array. For each list, i, pop v off of i and

Count number of left nodes in BST

元气小坏坏 提交于 2019-12-01 03:51:48
Given a BST, I am required to find the number of left nodes of the tree. Example: ` +---+ | 3 | +---+ / \ +---+ +---+ | 5 | | 2 | +---+ +---+ / / \ +---+ +---+ +---+ | 1 | | 4 | | 6 | +---+ +---+ +---+ / +---+ | 7 | +---+` The answer should be 4, as (5, 1 , 4, 7) are all left nodes of the tree. What I am thinking of doing is: public int countLeftNodes() { return countLeftNodes(overallRoot, 0); } private int countLeftNodes(IntTreeNode overallRoot, int count) { if (overallRoot != null) { count += countLeftNodes(overallRoot.left, count++); count = countLeftNodes(overallRoot.right, count); }

Are empty Binary Search Trees valid?

一个人想着一个人 提交于 2019-12-01 03:04:12
问题 I have two questions regarding binary search trees, both about empty trees. Is an empty tree (null) valid? Is a root node without children valid? 回答1: Yes, and yes. 回答2: Exactly what an empty tree is of course depends on your implementation, as does the meaning of the word "valid", but in general I would say "yes" to both questions. The empty tree represents the case where the set of entities you are intersted in is empty, and the single node the case where the set contains one entity. 回答3: A

generate all structurally distinct full binary trees with n leaves

大兔子大兔子 提交于 2019-12-01 01:08:12
This is a homework, I have difficulties in thinking of it. Please give me some ideas on recursions and DP solutions. Thanks a lot generate and print all structurally distinct full binary trees with n leaves in dotted parentheses form, "full" means all internal (non-leaf) nodes have exactly two children. For example, there are 5 distinct full binary trees with 4 leaves each. U can use recursion, on i-th step u consider i-th level of tree and u chose which nodes will be present on this level according to constraints: - there is parent on previous level - no single children present (by your