Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

algorithm - Nature of Tree Data Structure

According to blogs/books/google etc.

A tree is a data structure that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes

Which is completely fine, but the term hierarchical confuses me. Can tree be both Hierarchical and Linear?
Consider the following example:

                           Root
                  Left-Node
         Left-Node
Left-Node

If we want to traverse it to last element from root, then:

Root -> Left-Node -> Left-Node -> Left-Node

Doesn't that make a linear structure (like linkedlist)?
NOTE: My question doesn't mean to say that "Tree is not hierarchical" but "Tree is both Linear (In some cases) and Hierarchical"
Can you guys help me out with what I am understanding wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The functionality achieved by a tree data structure which is not hierarchical is not of great use. For example, the Binary search tree or Red black tree or AVL tree each of them are have that hierarchical nature.

Example: (Both Hierarchical and linear)

In binary search tree when keys are monotonically increasing or decreasing starting from the root it boils down to a linear structure which is unable to provide us with the necessary searching capability that we want to obtain from tree. That's why we go for AVL or RB tree.

Yes tree can be both hierarchical and linear. The BST when skewed is still hierarchical (it has parent child grandparent relationship among them ) but they are linearly accessible while searching. Hope this explains.


A data structure is said to be linear if the elements form a sequence or a linear list, for example Array, Linked list, queue etc. Here in case of BST when skewed it forms basically a linked list like linear structure. That's why my answer mentioned it as linear.

Clarifiction-1

Doesn't that make a linear structure (like linkedlist)?

Yes it is a linear structure.

Linear structures: the time required to search a linear list is proportional to the size of the data set. For example, if the size of the data set is n,(in case skewed tree) then the number of comparisons needed to find (or not find) an item may be multiple of n(here just one traversal).


Does that mean tree is linear?

A tree is a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear data structures. But particular case of skewed-ness it becomes linear. That deviation from it's non-linearity gives it no advantage compared to linear structures like list.


To be more precise: there is no way we are mentioning that to be hierarchical it must be non-linear. Some times linear data structure can be hierarchical too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...