Jump to content

Parent pointer tree: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
about trees I guess
tag as one source
 
(13 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{Short description|Tree data structure}}
{{one source |date=May 2024}}
[[File:spaghettistack.svg|thumb|120px|Spaghetti stack with an "active" stack frame highlighted]]
[[File:spaghettistack.svg|thumb|120px|Spaghetti stack with an "active" stack frame highlighted]]
In [[computer science]], an '''in-tree''' or '''parent pointer tree''' is an {{mvar|N}}-ary [[tree data structure]] in which each node has a [[pointer (computer programming)|pointer]] to its [[parent node]], but no pointers to [[child node]]s. When used to implement a set of [[Stack (abstract data type)|stacks]], the structure is called a '''spaghetti stack''', '''cactus stack''' or '''saguaro stack''' (after the [[saguaro]], a kind of cactus).<ref>{{Cite book | doi = 10.1145/62678.62692| chapter = Implementation strategies for continuations| title = Proceedings of the 1988 ACM conference on LISP and functional programming - LFP '88| pages = 124-131| year = 1988| last1 = Clinger | first1 = W. | last2 = Hartheimer | first2 = A. | last3 = Ost | first3 = E. | isbn = 089791273X}}</ref> Parent pointer trees are also used as [[disjoint-set data structure]]s.
In [[computer science]], an '''in-tree''' or '''parent pointer tree''' is an {{mvar|N}}-ary [[tree data structure]] in which each node has a [[pointer (computer programming)|pointer]] to its [[parent node]], but no pointers to [[child node]]s. When used to implement a set of [[Stack (abstract data type)|stacks]], the structure is called a '''spaghetti stack''', '''cactus stack''' or '''saguaro stack''' (after the [[saguaro]], a kind of cactus).<ref>{{Cite book | doi = 10.1145/62678.62692| chapter = Implementation strategies for continuations| title = Proceedings of the 1988 ACM conference on LISP and functional programming - LFP '88| pages = 124-131| year = 1988| last1 = Clinger | first1 = W. | last2 = Hartheimer | first2 = A. | last3 = Ost | first3 = E. | isbn = 089791273X}}</ref> Parent pointer trees are also used as [[disjoint-set data structure]]s.
Line 5: Line 7:


==Use in compilers==
==Use in compilers==
A [[compiler]] for a language such as [[C (programming language)|C]] creates a spaghetti stack as it opens and closes [[symbol table]]s representing block [[Scope (computer science)|scope]]s. When a new block scope is opened, a symbol table is pushed onto a stack. When the closing curly brace is encountered, the scope is closed and the symbol table is popped. But that symbol table is remembered, rather than destroyed. And of course it remembers its higher level "parent" symbol table and so on. Thus when the compiler is later performing translations over the [[abstract syntax tree]], for any given expression, it can fetch the symbol table representing that expression's environment and can resolve references to identifiers. If the expression refers to a variable X, it is first sought after in the leaf symbol table representing the inner-most lexical scope, then in the parent and so on.
A [[compiler]] for a language such as [[C (programming language)|C]] creates a spaghetti stack as it opens and closes [[symbol table]]s representing block [[Scope (computer science)|scope]]s. When a new block scope is opened, a symbol table is pushed onto a stack. When the closing curly brace is encountered, the scope is closed and the symbol table is popped. But that symbol table is remembered, rather than destroyed, and it remembers its higher level "parent" symbol table and so on. Thus when the compiler is later performing translations over the [[abstract syntax tree]], for any given expression, it can fetch the symbol table representing that expression's environment and can resolve references to identifiers. If the expression refers to a variable X, it is first sought after in the leaf symbol table representing the inner-most lexical scope, then in the parent and so on.


==Use as call stacks==
==Use as call stacks==
The term ''spaghetti stack'' is closely associated with implementations of [[programming language]]s that support [[continuation]]s. Spaghetti stacks are used to implement the actual [[run-time stack]] containing variable bindings and other environmental features. When continuations must be supported, a function's local variables cannot be destroyed when that function returns: a saved continuation may later re-enter into that function, and will expect not only the variables there to be intact, but it will also expect the entire stack to be present so the function is able to return again. To resolve this problem, [[stack frame]]s can be [[dynamic memory allocation|dynamically allocated]] in a spaghetti stack structure, and simply left behind to be [[garbage collection (computer science)|garbage collected]] when no continuations refer to them any longer. This type of structure also solves both the upward and downward [[funarg problem]]s, so first-class lexical [[closure (computer science)|closure]]s are readily implemented in that substrate also.
The term ''spaghetti stack'' is closely associated with implementations of [[programming language]]s that support [[continuation]]s. Spaghetti stacks are used to implement the actual [[run-time stack]] containing variable bindings and other environmental features. When continuations must be supported, a function's local variables cannot be destroyed when that function returns: a saved continuation may later re-enter into that function, and will expect not only the variables there to be intact, but it will also expect the entire stack to be present so the function is able to return again. To resolve this problem, [[stack frame]]s can be [[dynamic memory allocation|dynamically allocated]] in a spaghetti stack structure, and simply left behind to be [[garbage collection (computer science)|garbage collected]] when no continuations refer to them any longer. This type of structure also solves both the upward and downward [[funarg problem]]s, as a result first-class lexical [[closure (computer science)|closure]]s are readily implemented in that substrate.


Examples of languages that use spaghetti stacks are:
Examples of languages that use spaghetti stacks are:
* Languages having first-class continuations such as [[Scheme (programming language)|Scheme]] and [[Standard ML of New Jersey]]
* Languages having first-class continuations such as [[Scheme (programming language)|Scheme]] and [[Standard ML of New Jersey]]
* Languages where the execution stack can be inspected and modified at runtime such as [[Smalltalk]]
* Languages where the execution stack can be inspected and modified at runtime such as
** [[Smalltalk]]
* [[Felix (programming language)|Felix]]
** [[Felix (programming language)|Felix]]
* [[Cilk]]
** [[Cilk]]

[[Mainframe computers]] using the [[Burroughs Large Systems]] architecture and running the [[Burroughs MCP|MCP]] operating system can spawn multiple tasks within the same program. Since these were originally [[ALGOL 60|ALGOL]]-based systems they have to support [[nested functions]] and the result is that task creation results in a fork in the stack, which [[Burroughs Corporation|Burroughs]] informally described as a "saguaro stack.".


==See also==
==See also==
* [[Burroughs large systems]]
* [[Persistent data structure]]
* [[Persistent data structure]]


Line 25: Line 29:
[[Category:Trees (data structures)]]
[[Category:Trees (data structures)]]
[[Category:Continuations]]
[[Category:Continuations]]
[[Category:Metaphors referring to spaghetti]]

Latest revision as of 19:17, 13 May 2024

Spaghetti stack with an "active" stack frame highlighted

In computer science, an in-tree or parent pointer tree is an N-ary tree data structure in which each node has a pointer to its parent node, but no pointers to child nodes. When used to implement a set of stacks, the structure is called a spaghetti stack, cactus stack or saguaro stack (after the saguaro, a kind of cactus).[1] Parent pointer trees are also used as disjoint-set data structures.

The structure can be regarded as a set of singly linked lists that share part of their structure, in particular, their tails. From any node, one can traverse to ancestors of the node, but not to any other node.

Use in compilers

[edit]

A compiler for a language such as C creates a spaghetti stack as it opens and closes symbol tables representing block scopes. When a new block scope is opened, a symbol table is pushed onto a stack. When the closing curly brace is encountered, the scope is closed and the symbol table is popped. But that symbol table is remembered, rather than destroyed, and it remembers its higher level "parent" symbol table and so on. Thus when the compiler is later performing translations over the abstract syntax tree, for any given expression, it can fetch the symbol table representing that expression's environment and can resolve references to identifiers. If the expression refers to a variable X, it is first sought after in the leaf symbol table representing the inner-most lexical scope, then in the parent and so on.

Use as call stacks

[edit]

The term spaghetti stack is closely associated with implementations of programming languages that support continuations. Spaghetti stacks are used to implement the actual run-time stack containing variable bindings and other environmental features. When continuations must be supported, a function's local variables cannot be destroyed when that function returns: a saved continuation may later re-enter into that function, and will expect not only the variables there to be intact, but it will also expect the entire stack to be present so the function is able to return again. To resolve this problem, stack frames can be dynamically allocated in a spaghetti stack structure, and simply left behind to be garbage collected when no continuations refer to them any longer. This type of structure also solves both the upward and downward funarg problems, as a result first-class lexical closures are readily implemented in that substrate.

Examples of languages that use spaghetti stacks are:

Mainframe computers using the Burroughs Large Systems architecture and running the MCP operating system can spawn multiple tasks within the same program. Since these were originally ALGOL-based systems they have to support nested functions and the result is that task creation results in a fork in the stack, which Burroughs informally described as a "saguaro stack.".

See also

[edit]

References

[edit]
  1. ^ Clinger, W.; Hartheimer, A.; Ost, E. (1988). "Implementation strategies for continuations". Proceedings of the 1988 ACM conference on LISP and functional programming - LFP '88. pp. 124–131. doi:10.1145/62678.62692. ISBN 089791273X.