Non Linear Data Structures

17
Nonlinear data structure Subject :- Data structure :: Presented by :: Patel Adarsh

Transcript of Non Linear Data Structures

Page 1: Non Linear Data Structures

Nonlinear data structure

Subject :- Data structure

:: Presented by ::Patel Adarsh

Page 2: Non Linear Data Structures

TreeThe boxes on the tree are called nodes• The nodes immediately below (to the left and right of) a given node are called its children• The node immediately above a given node is called its parent• The (unique) node without a parent is called the root• A node with no children is called a leaf• Two children of the same parent are said to be siblings

Page 3: Non Linear Data Structures

Depth and height

• depth or level: length of the path from root to the current node (depth of root = 0)

• height: length of the longest path from root to any leaf

– empty (null) tree's height: -1– single-element tree's height: 0– tree with a root with children: 1

Page 4: Non Linear Data Structures

• In a binary tree, each node can have no more than two child nodes

• Trees are typically are represented using references as dynamic links

• For binary trees, this requires storing only two links per node to the left and right child

Binary Tree

Page 5: Non Linear Data Structures

• Tree traversal schemes include • Preorder traversal ( root, left, right )• Inorder traversal ( left, root, right )• Postorder traversal ( left, right, root )

• Preorder generates prefix expression ( polish notation ) from an expression trees• Inorder generates a sorted ordering• Postorder generates a post fix expression, also useful

for node deletion

Tree Traversal

Page 6: Non Linear Data Structures

Pre-order traversal sequence :: F, B, A, D, C, E, G, I, H (root, left, right)

In-order traversal sequence :: A, B, C, D, E, F, G, H, I (left, root, right)

Post-order traversal sequence :: A, C, E, D, B, H, I, G, F (left, right, root)

Page 7: Non Linear Data Structures

Binary Search Tree• Binary search trees allow for fast insertion and removal of

elements • They are specially designed for fast searching • All nodes in a binary search tree fulfill the property that: • Descendants to the left have smaller data values than the

node data value • Descendants to the right have larger data values than the

node data value

Page 8: Non Linear Data Structures
Page 9: Non Linear Data Structures

Threaded Tree

• Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers

• We can use these pointers to help us in inorder traversals

• We have the pointers reference the next node in an inorder traversal called threads

• We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer

Page 10: Non Linear Data Structures

Threaded Tree

• Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers

• We can use these pointers to help us in inorder traversals

• We have the pointers reference the next node in an inorder traversal called threads

• We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer

Page 11: Non Linear Data Structures

Threaded Tree

8

753

11

13

1

6

9

Page 12: Non Linear Data Structures

Threaded Tree

8

753

11

13

1

6

9

Output13

Page 13: Non Linear Data Structures

Trees in which the number of subtrees of any node need not be required to be 0, 1, or 2.

The number of subtrees for any node may be variable.

Some nodes may have 1 or no subtrees, others may have 3, some 4, or any other combination.

General Tree

Page 14: Non Linear Data Structures

Conversion process

Use the root of the general tree as the root of the binary tree.

Determine the first child of the root. This is the leftmost node in the general tree at the next level. Insert this node. The child reference of the parent node refers to this node. Continue finding the first child of each parent node and insert it below the parent node with the child reference of the parent to this node.

Page 15: Non Linear Data Structures

Conversion process (continued)

When no more first children exist in the path just used, move back to the parent of the last node entered and repeat the above process.

Complete the tree for all nodes. In order to locate where the node fits you must search for the first child at that level and then follow the sibling references to a nil where the next sibling can be inserted. The children of any sibling node can be inserted by locating the parent and then inserting the first child. Then the above process is repeated.

Page 16: Non Linear Data Structures

Creating binary tree from a general tree

General Tree Binary Tree

Page 17: Non Linear Data Structures

.. Thank you ..