Do we have stack involvement in a threaded tree?

  • Thread starter zak100
  • Start date
  • Tags
    Tree
In summary, the article suggests that a threaded tree data structure can be used to speed up binary tree traversals by eliminating the need for a stack. This is done by incorporating pointers in each node that can toggle between pointing to the child nodes or the successor and predecessor nodes. The article also mentions the use of right threaded trees which have no left nodes and can be seen as equivalent to a linked list.
  • #1
zak100
462
11

Homework Statement



In the book it gives following background for using threaded trees:
"The concern is that some additional time has to be spent to maintain the stack & some more space has to be set aside for the stack itself."
& then it says
"It is more efficient to incorporate the stack as part of the tree. This is done by incorporating threads in a given node"

From first sentence "It is more efficient to incorporate the stack as part of the tree" it looks as if stack is utilized in the threaded tree but i can't understand what it means " incorporate stack as part of tree" but in the next sentence it says "This is done by incorporating threads in a given node". By threads we mean pointers which point to the inorder successor & inoder predecessor. They are not stack. So this is my problem that i can't understand whether stack is used in a threaded tree or not

Homework Equations


No equations, only theory.

The Attempt at a Solution


I think stack is present but at the same time it has threads/pointers also which help in efficient traversal

Some body please guide me.

Zulfi.
 
Physics news on Phys.org
  • #2
The article is suggesting that the 2 pointers already associated with each node, can negate the need for a stack in that we can change the direction we expect them to point.

Presume that each node would have 2 children designated as node.left and node.right. Typically, we would use a stack to retain reference to the parent node and which child node is next to be referenced, and the stack must be large enough to travel from the Root node to the bottom node of the branch with the most generations, as well as sibling nodes that have been visited in neighboring sub-branches.

In this scenario though, we can include an additional data field to indicate whether the pointers should thread to either of the child nodes, or thread to successor (child) and predecessor(parent) nodes. In short, each node incorporates it's own psuedo-stack, using itself as the reference point.
 
  • #3
Hi,
Thanks for your response but i feel that you are trying to avoid my question.

I can't understand whether we have stack or not in a threaded tree from your reply.
i got following from a site:
"
In many applications, binary tree traversals are carried out repeatedly, and the overhead of stack operations - the implicit ones carried out by the runtime system (when a recursively coded traversal is being executed), or the explicit pushes/pops of a non-recursive (but stack-driven) traversal procedure - is costly in terms of program execution time. (The traversal isO(n) in terms of computational effort, but the stack operations introduce overhead (a larger run-time constant) than might be desirable.) It is sometimes useful to modify the data structure (of pointers and nodes) which represents the binary tree so as to speed up - say - the inorder traversal process: make it "stack-free".
http://www.cs.rutgers.edu/~kaplan/503/handouts/threadedtrees.html

So can i say that we don't have stack involvement in a threaded tree?
Some body please provide me unambiguous answer to my question.

Zulfi.
 
  • #4
zak100 said:
Hi,
Thanks for your response but i feel that you are trying to avoid my question.

I can't understand whether we have stack or not in a threaded tree from your reply.
i got following from a site:
"
In many applications, binary tree traversals are carried out repeatedly, and the overhead of stack operations - the implicit ones carried out by the runtime system (when a recursively coded traversal is being executed), or the explicit pushes/pops of a non-recursive (but stack-driven) traversal procedure - is costly in terms of program execution time. (The traversal isO(n) in terms of computational effort, but the stack operations introduce overhead (a larger run-time constant) than might be desirable.) It is sometimes useful to modify the data structure (of pointers and nodes) which represents the binary tree so as to speed up - say - the inorder traversal process: make it "stack-free".
http://www.cs.rutgers.edu/~kaplan/503/handouts/threadedtrees.html

So can i say that we don't have stack involvement in a threaded tree?
Some body please provide me unambiguous answer to my question.

Zulfi.

Short answer, No there is not a stack 'object' in addition to the tree 'object' in the example above.

In a Binary tree, each node has 2 children and includes a pointer to each one. A stack would normally be implemented to track the path of which nodes were visited prior to the current one, so traversal lands on the correct parent node.

In the scenario above, the pointers can be toggled to point to the next child to be visited and the parent, rather than the left and right children. Where the 2nd pointer already points to the parent, it is not necessary to push that pointer to a stack to preserve the path.
 
  • #5
Wee-Lamm said:
In the scenario above, the pointers can be toggled to point to the next child to be visited and the parent, rather than the left and right children.
What I get when I read that article is that pointers are not toggled unless a new node is inserted to the tree. The threaded tree implementation is accomplished by adding two booleans to each node, and by using a dummy node as the head node for the tree.

Getting back to the OP's question, the functions used to traverse the threaded tree are iterative and not recursive, so only a fixed amount of stack is used by each function regardless of tree depth, and in the case of a processor where parameters, return address, return value, are in registers, then no stack would be needed.

Rebalancing a tree would probably require recursion.

The article mentions right threaded trees with no left nodes, isn't this the equivalent of a linked list?
 
Last edited:

Related to Do we have stack involvement in a threaded tree?

What is a threaded tree?

A threaded tree is a type of binary tree in which each node contains a reference to its successor and predecessor, allowing for efficient traversal without the need for recursion or a stack.

What is the purpose of a threaded tree?

The purpose of a threaded tree is to improve the efficiency of tree traversal by eliminating the need for a stack. This is particularly useful in cases where recursion or a large stack can cause memory issues.

How does a threaded tree differ from a regular binary tree?

In a regular binary tree, the child nodes do not contain references to their siblings. In a threaded tree, these references are present, allowing for efficient traversal without the need for a stack.

Can a threaded tree have both a left and right thread?

Yes, a threaded tree can have both a left and right thread. In this case, the child nodes will have references to both their left and right siblings, allowing for efficient traversal in both directions.

What are the advantages of using a threaded tree?

The main advantage of using a threaded tree is the improved efficiency of tree traversal. This can be particularly beneficial for large or complex trees where recursion or a stack may cause memory issues. Threaded trees also require less space compared to regular binary trees, as they do not need to store references to child nodes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
  • Feedback and Announcements
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Replies
6
Views
667
  • High Energy, Nuclear, Particle Physics
Replies
2
Views
1K
  • High Energy, Nuclear, Particle Physics
Replies
1
Views
2K
Back
Top