traversal

Traversing a single linked list in order

情到浓时终转凉″ 提交于 2021-02-08 10:33:33
问题 I have been trying to think a way to traverse a single linked list. This is so far what I have done: #include <iostream> typedef struct node { int data; // will store information node *next; // the reference to the next node }; int printList(node *traverse) { if (traverse->next == NULL) { return -1; } traverse=traverse->next; printList(traverse); cout << traverse->data << endl; return 0; } int main() { node *head = NULL; for (int i = 0; i < 10; i++) { node *newEntry = new node; newEntry->data

Algorithm to traverse all edges in a graph

試著忘記壹切 提交于 2021-02-07 13:20:35
问题 As a personal easter project I'm trying to implement some model-based testing at work. I've got a graph implemented in python and I need to traverse all edges / do all transitions of the graph, at least once. Traversing an edge twice or more does not matter, but I need to start and end in the same node and get a sequence of edges/transitions back. Simpler algorithm > shortest sequence. I've looked around and found a lot of algorithms, but I couldn't find one / a combination that works for me.

Shallow copy of a hashset

社会主义新天地 提交于 2021-02-04 11:54:02
问题 Whats the best way of doing it? var set2 = new HashSet<reference_type>(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements 回答1: Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary . It's easy to create your own of course: public static HashSet<T> ToHashSet<T>(this

Shallow copy of a hashset

喜夏-厌秋 提交于 2021-02-04 11:53:26
问题 Whats the best way of doing it? var set2 = new HashSet<reference_type>(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements 回答1: Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary . It's easy to create your own of course: public static HashSet<T> ToHashSet<T>(this

Struggling to remove rows from my dataframe

巧了我就是萌 提交于 2021-01-29 11:05:02
问题 I've been Googling and reading this site, and I know folks say that, in general, if you're iterating over a Dataframe, you're likely doing it wrong. So, I figure I'm doing it wrong. The challenge I'm presented with is that I need to make decisions on what to keep versus what to discard based on logic. Best explained with an example. Say my Dataframe looks like: SO_NUMBER ITEM # SALES_QTY PO_NUMBER PO_QTY PO_PRICE ENOUGH_TO_FILL -----------------------------------------------------------------

How to traverse a Btree?

浪尽此生 提交于 2021-01-28 06:27:17
问题 I have a Btree and I'm trying to figure out how traverse it so that the keys are displayed ascending order. All I can figure out is that this can be done with a recursive function. What's the pseudo-code to do it? 回答1: Assuming you have a definition like: template <class T> class btree_node { btree_node **child; // an array of child nodes T **element; // the elements in this node unsigned int child_count; // the number of children // the number of elements is 1 less then child_count }; Then

Binary search tree filter values in a range

这一生的挚爱 提交于 2021-01-27 20:19:22
问题 I have a tree(RBT) consisting of N elements. Let's imagine I have this tree (N = 7): 4 2 6 1 3 5 7 How do I filter values in some range (e.g. print all values between 3 and 6) with better performance than O(N)? Is there any specific algorithm? I'm imagining it something like find position of value 3 [log(N)], somehow continue until you get to the 6 [O(M)]. 回答1: If you have Sedgevick's Algorithms, 4 ed., look at the end of chapter 3.2 on BST's. Also book companion has implementation in Java.

how many ways to visit all the points of a given matrix?

眉间皱痕 提交于 2021-01-21 05:42:46
问题 There is a m*n Matrix . From one point of the matrix, you can move to one of the eight adjacent points ( up, down, left, right, upper left, lower left, upper right, lower right ) If the point in one direction has been visited, you can continue to move to the next unvisited point in this direction. You cann't visit a point which has been visited, but you can pass through the visited adjacent point to visit other un-visited point. For example, the current point is (5,5): If (5,4) has been

how many ways to visit all the points of a given matrix?

微笑、不失礼 提交于 2021-01-21 05:42:06
问题 There is a m*n Matrix . From one point of the matrix, you can move to one of the eight adjacent points ( up, down, left, right, upper left, lower left, upper right, lower right ) If the point in one direction has been visited, you can continue to move to the next unvisited point in this direction. You cann't visit a point which has been visited, but you can pass through the visited adjacent point to visit other un-visited point. For example, the current point is (5,5): If (5,4) has been