binary-tree

access element of struct passed into a void* pointer

邮差的信 提交于 2019-12-18 07:02:36
问题 I'm working with a binary search tree data structure to sort a series of structs with the type definitions: typedef struct { char c; int index; } data_t; typedef struct node node_t; typedef node { void *data; node_t *left; node_t *right; } The node_t typedef is from a library provided to me for this purpose, presumably with a void* pointer to ensure polymorphism. node will be passed into the function: static void *recursive_search_tree(node_t *root, void *key, int cmp(void*,void*)) Within the

access element of struct passed into a void* pointer

烈酒焚心 提交于 2019-12-18 07:01:05
问题 I'm working with a binary search tree data structure to sort a series of structs with the type definitions: typedef struct { char c; int index; } data_t; typedef struct node node_t; typedef node { void *data; node_t *left; node_t *right; } The node_t typedef is from a library provided to me for this purpose, presumably with a void* pointer to ensure polymorphism. node will be passed into the function: static void *recursive_search_tree(node_t *root, void *key, int cmp(void*,void*)) Within the

Level Order Traversal of a Binary Tree

≡放荡痞女 提交于 2019-12-18 04:47:09
问题 void traverse(Node* root) { queue<Node*> q; Node* temp_node= root; while(temp_node) { cout<<temp_node->value<<endl; if(temp_node->left) q.push(temp_node->left); if(temp_node->right) q.push(temp_node->right); if(!q.empty()) { temp_node = q.front(); q.pop(); } else temp_node = NULL; } } The above posted code is my level order traversal code. This code works fine for me but One thing I dont like is I am explicitly initializing temp_node = NULL or I use break. But it does not seem to be a good

Is SortedDictionary a red-black tree?

*爱你&永不变心* 提交于 2019-12-18 04:01:07
问题 I saw several quotes about this on the Internet but no official documentation? Can anyone tell me where I can get information about this? 回答1: This isn’t supposed to be documented since it’s an implementation detail . For instance, there is more than one implementation of SortedDictionary : there’s Microsoft’s and there’s the Mono implementation. And the Mono implementation does, in fact, use a red-black tree in its current version (2.10.9). So does the current .NET version (you can find that

How would you print out the data in a binary tree, level by level, starting at the top?

女生的网名这么多〃 提交于 2019-12-17 23:04:01
问题 This is an interview question I think of a solution. It uses queue. public Void BFS() { Queue q = new Queue(); q.Enqueue(root); Console.WriteLine(root.Value); while (q.count > 0) { Node n = q.DeQueue(); if (n.left !=null) { Console.Writeln(n.left); q.EnQueue(n.left); } if (n.right !=null) { Console.Writeln(n.right); q.EnQueue(n.right); } } } Can anything think of better solution than this, which doesn't use Queue? 回答1: Level by level traversal is known as Breadth-first traversal. Using a

In-order iterator for binary tree [closed]

柔情痞子 提交于 2019-12-17 22:27:20
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . How can I write a Java iterator (i.e. needs the next and hasNext methods) that takes the root of a binary tree and iterates through the nodes of the binary tree in in-order fashion? 回答1: The first element of a

Number of binary search trees over n distinct elements

只愿长相守 提交于 2019-12-17 22:20:53
问题 How many binary search trees can be constructed from n distinct elements? And how can we find a mathematically proved formula for it? Example: If we have 3 distinct elements, say 1, 2, 3, there are 5 binary search trees. 回答1: Given n elements, the number of binary search trees that can be made from those elements is given by the nth Catalan number (denoted C n ). This is equal to Intuitively, the Catalan numbers represent the number of ways that you can create a structure out of n elements

B-tree faster than AVL or RedBlack-Tree? [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 21:38:10
问题 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 6 years ago . I know that performance never is black and white, often one implementation is faster in case X and slower in case Y, etc. but in general - are B-trees faster then AVL or RedBlack-Trees? They are considerably more complex to implement then AVL trees (and maybe even RedBlack-trees?), but are they faster (does

When to use Preorder, Postorder, and Inorder Binary Search Tree Traversal strategies

泄露秘密 提交于 2019-12-17 21:37:51
问题 I realized recently that while having used BST's plenty in my life, I've never even contemplated using anything but Inorder traversal (while I am aware of and know how easy it is to adapt a program to use pre/post-order traversal). Upon realizing this, I pulled out some of my old data-structures textbooks and looked for reasoning behind the usefulness of pre-order and post-order traversals - they didn't say much though. What are some examples of when to use preorder/postorder practically?

Print a Tree Vertically

落爺英雄遲暮 提交于 2019-12-17 19:48:58
问题 To understand what's same vertical line, we need to define horizontal distances first. If two nodes have the same Horizontal Distance (HD), then they are on same vertical line. The idea of HD is simple. HD for root is 0, a right edge (edge connecting to right subtree) is considered as +1 horizontal distance and a left edge is considered as -1 horizontal distance. For example, in the below tree, HD for Node 4 is at -2, HD for Node 2 is -1, HD for 5 and 6 is 0 and HD for node 7 is +2. Examples: