• No results found

Linear and Non Linear Data Structure

N/A
N/A
Protected

Academic year: 2022

Share "Linear and Non Linear Data Structure"

Copied!
92
0
0

Loading.... (view fulltext now)

Full text

(1)

Tree

(2)

Linear and Non Linear Data Structure

• Arrays, Linked lists, Stacks, Queues are linear structure.

Why linear

• The elements are totally ordered.

• For any pair of elements, one precedes the other.

• We can have non-linear structure

Trees, Graphs are non linear data strucutre

• Elements are not totally ordered.

(3)

Tree Data structure

A tree is a finite set of one or more nodes such that:

o There is a specially designated node called the root.

o The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree.

o We call T1, ..., Tn the subtrees of the root.

(4)

Tree representation

A

J I

H

F E

D C

M

P Q

L K

(5)

Tree representation

A

J I

H

F D E

C

M

P Q

L K

Subtree with single node

Subtree with root D

Subtree with root E

Subtree with root F Root

(6)

Tree Terminology

A

G F

D E B C

H

Root Parent Child Siblings Leaves

Root is the node which has no parent.

Siblings are nodes which has common parent.

Leaves are node which has no child node.

Every node except Root node has parent.

Level 0

Level 1

Level 2

I Level 3

(7)

Tree Terminology

A

G F

D E B C

H

Root Parent Child Siblings Leaves

A is Root Node

A is Parent of B, C, D and E F and G are Child of D

F, and G are Siblings

B, C, F, G and I are leaves

Level 0

Level 1

Level 2

I Level 3

(8)

Tree Terminology

Height (Depth) of Tree is= max. level number +1.

The Degree of node is equal to its Childs. Or in other words, it is the number f subtrees of a node in a given tree.

External Node: a node which has no child.

Internal Node: a node which has at least one child.

Terminal Node (s): A node with degree zero is called a terminal node or a leaf.

Non-terminal node: A node (except the root) whose degree is not zero

(9)

Tree Terminology

A

G F

D E B C

Height of Tree is 4 H

The Degree of node D is 2

B, C, F, G and I are External

A , D, E, H are internal node of Tree

Level 0

Level 1

Level 2

I Level 3

(10)

Binary Trees

(11)

Binary Tree

Binary Tree:

Ordered tree with all nodes having at most two children

(12)

Some examples

A

H G

F E

D

C B

I

Binary tree

(13)

Structures which are not binary trees

A

G

F E

D

C B

I

(14)

Various binary trees

• Ordered binary Tree:

Is a tree in which the children of each node are ordered.

• Strictly Binary Tree:

If every non leaf node in a binary tree has non empty left and right sub trees then such tree is termed as a strictly binary tree.

• Complete Binary Tree:

A complete binary tree of height h is the strict binary tree all of whose leaves are at level h.

(15)

A

E G D

C B

H I

F

J

1

2 3

4 5 6 7

8 9 10

Ordered Binary Tree

(16)

Ex: of a strictly binary tree

A

E C

B

D

F G

(17)

A

J N E F D

C B

H I K O

G

L M

A COMPLETE BINARY TREE OF DEPTH 3

(18)

Complete Binary Tree

• Level i has 2i nodes

• in a tree of height h

Leaves are at level h No of leaves is 2h

(19)

Binary Search Tree

(20)

Binary Search Tree

• Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently.

Binary Search tree property

For every node X, all the keys in its left subtree are smaller than the key value in X, and all the keys in its right subtree are larger than the key value in X

(21)

Binary Search Trees

A binary search tree

Not a binary search tree

(22)

Design a Binary Search Tree

(23)
(24)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13 Root

(25)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

(26)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

(27)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

(28)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

(29)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

(30)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5

(31)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

(32)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8

(33)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8 2

(34)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8 2

7

(35)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8 2

7 9

(36)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8 2

7 9

11

(37)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8 2

7 9

11

6

(38)

13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

13

3

4

12

14

10

5 1

8 2

7 9

11

6

18

(39)

Operations on Binary Trees

• INSERT

• SEARCH

• DELETE

(40)

Binary Tree Traversals

(41)

Tree Traversal

• Many binary tree operations are done by performing a traversal of the binary tree.

• In a traversal, each element of the binary tree is

visited exactly once.

(42)

Three tree traversal methods

• Preorder traversal

• Inorder traversal

• Postorder traversal

(43)

Preorder Traversal

Steps:

• Visit the root

• Traverse the left subtree in preorder

• Traverse the right subtree in preorder

(44)

Traversing a Tree Preorder

A

C B

D E F G

I H

(45)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: A

(46)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: AB

(47)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABD

(48)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABDE

(49)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABDEH

(50)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABDEHC

(51)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABDEHCF

(52)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABDEHCFG

(53)

Traversing a Tree Preorder

A

C B

D E F G

I H

Result: ABDEHCFGI

(54)

Inorder Traversal (symmetric order)

Steps for a nonempty binary tree:

• Traverse the left subtree in inorder

• Visit the root

• Traverse the right subtree in inorder

(55)

Traversing a Tree Inorder

A

C B

D E F G

I H

(56)

Traversing a Tree Inorder

A

C B

D E F G

I H

(57)

Traversing a Tree Inorder

A

C B

D E F G

I H

(58)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: D

(59)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DB

(60)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DB

(61)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBH

(62)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHE

(63)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHEA

(64)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHEA

(65)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHEAF

(66)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHEAFC

(67)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHEAFCG

(68)

Traversing a Tree Inorder

A

C B

D E F G

I H

Result: DBHEAFCGI

(69)

Postorder Traversal

Steps:

• Traverse the left subtree in postorder

• Traverse the right subtree in postorder

• Visit the root

(70)

Traversing a Tree Postorder

A

C B

D E F G

I H

(71)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result:

(72)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result:

(73)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: D

(74)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: D

(75)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DH

(76)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHE

(77)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEB

(78)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEB

(79)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEBF

(80)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEBF

(81)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEBFI

(82)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEBFIG

(83)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEBFIGC

(84)

Traversing a Tree Postorder

A

C B

D E F G

I H

Result: DHEBFIGCA

(85)

Tree Traversal – Example 1

A

D F

C B

G

E

I H

Preorder:

Inorder:

Postorder:

ABDGCEHIF DGBAHEICF GDBHIEFCA

(86)

Tree Traversal – Example 2

A

F C

B

I J

E H

D

G

L K

Preorder: ABCEIFJDGHKL

Inorder: EICFJBGDKHLA

Postorder: IEJFCGKLHDBA

(87)

General Trees

(88)
(89)
(90)

Forests

• Definition: A forest is a set of n ≥ 0 disjoint trees.

• When we remove a root from a tree, we’ll get a forest. E.g., Removing the root of a binary tree will get a forest of two trees.

A

I H

E G

D

B C F

Figure 5.34 Three-tree forest

(91)

Transforming A Forest Into A Binary Tree

• T1, …, Tn

forest of trees

• B(T1, …, Tn)

binary tree

• Algorithm

Is empty if n = 0

Has root equal to root(T1)

Has left subtree equal to B(T11, …, T1m) Has right subtree equal to B(T2, …, Tn)

(92)

Transforming A Forest Into A Binary Tree

A

I H

E

G

D B

C F

A

I H

E G

D

B C

F

References

Related documents

(adaptive, extrapolative, regressive, and interactive; see Appendix 1 for variable definitions); then estimate using OLS A. Optimal lag specification or single regressor)

environment and research.   

tivity-based routing. The reason that PROPHET_S performs worse than connectivity-based routing is that it only compares d-connectivity values of two encountering nodes for

A binary tree can be implemented where each node has left and right pointer elds, an (optional) parent pointer, and a data eld.... Right: A heap but not a binary

Unit3: Tree: binary tree, Binary Tree representations: node and implicit array representation, internal and external nodes, binary tree traversals, threaded binary

See Nielsen and Hunter’s (this issue) discussion of “sequential and informational complementarity”. The issue of performance management and evaluation as competing management tools

Ecolabel: EU flower, Øko tex, Wools of New Zealand, Flame retardant, No heavy

Economic Networks Workshop, Stanford University, May 2011 IRISS Board of Trustees, Stanford University, May 2011 2011 MIT Europe Conference, Vienna, Austria, March 2011