IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 5: [Data Structures] – Structures, Singly-Linked Lists, Doubly-Linked Lists, Hash Tables, Tries, Queues, and Stacks › Why another node cursor created when there is already a node root
- This topic is empty.
-
AuthorPosts
-
November 13, 2023 at 5:31 am #1764
while (fscanf(infile, "%s", name) == 1) { node *cursor = root;
Query: Why another node cursor created when there is already a node root Source code: CS50 Trie
Reply by ChatGPT
In the code snippet you provided, it seems like a tree structure is being used, and a node named `root` is being declared and initialized outside the loop. Inside the loop, a new pointer to a node named `cursor` is being declared and initialized to the same node as `root`. This is a common practice when traversing or manipulating tree structures.
The reason for creating a separate `cursor` pointer is to traverse the tree without modifying the original pointer (`root`). The loop likely iterates over some input data (possibly reading from a file using `fscanf`), and the `cursor` pointer is used to navigate through the tree without affecting the original starting point.
For example, if the tree is being built or searched, the `cursor` pointer can move through the tree nodes based on the input data while the `root` pointer remains at the top of the tree. This allows you to reset `cursor` to `root` for each iteration of the loop, ensuring that the tree traversal starts from the root node each time.
Without more context or the complete code, it’s not possible to provide a more specific explanation, but the use of a separate pointer like `cursor` is a common technique in tree traversal and manipulation.
Query: Do you mean by preserving root node details, we can also access details about all its children nodes, even children of children nodes?
Reply by ChatGPT
Preserving root node details typically refers to maintaining information about the top-level or parent node in a hierarchical structure, such as a tree or graph. While preserving the details of the root node, it doesn’t necessarily imply direct access to all the details of its children or children of children nodes.
To access details about all the nodes in a hierarchical structure, you generally need to traverse the structure, starting from the root node and navigating through its children, grandchildren, and so on. Preserving root node details might help you initiate this traversal and keep track of the overall structure, but you would still need to traverse the hierarchy to access information about individual nodes.
In summary, preserving root node details is a starting point for understanding the structure, but direct access to details about all nodes, including children and grandchildren, usually involves a traversal of the hierarchy.
Just like in a pointer, access to first character would lead to access of all the string characters, similar concept applicable to nodes?
byu/DigitalSplendid inC_ProgrammingComment
byu/DigitalSplendid from discussion
inC_ProgrammingComment
byu/DigitalSplendid from discussion
inC_ProgrammingComment
byu/DigitalSplendid from discussion
inC_ProgrammingComment
byu/DigitalSplendid from discussion
inC_Programming -
AuthorPosts
- You must be logged in to reply to this topic.