data structures lakshmish ramaswamy. tree hierarchical data structure several real-world systems...

9
Data Structures Lakshmish Ramaswamy

Upload: teresa-gilmore

Post on 17-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Tree – Informal Definition Each node has a single parent node except for root which has no parent root parent nodes children nodes leaf nodes

TRANSCRIPT

Page 1: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Data Structures

Lakshmish Ramaswamy

Page 2: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Tree• Hierarchical data structure• Several real-world systems have

hierarchical concepts– Physical and biological systems– Organizations

• Many other applications– Sorting– Searching– Databases– Internet

Page 3: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Tree – Informal Definition

• Each node has a single parent node except for root which has no parent

9

6

5

3

8

7

rootparent nodes

children nodes

leaf nodes

Page 4: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Examples

6

382

7

6

3

7 8

5

7

4

8

a tree a binarytree a Binary

search Treenot a tree

7

5

6

a treeand a ...

9

6

5

3

8

7

Page 5: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Basics of Graph Theory• Graph is a set of vertices V = {v1, v2, ,, vn}

and a set of edges connecting them E = {e1, e2, …, eM}– Each edge is a tuple of vertices el = (vi, vj)

• A graph is directed if edges are directed (pair of vertices is ordered)

• Connected graph – Graph where any pair of vertices have a path b/w them

• Acyclic Graph: A graph with no cycles• Tree is a connected acyclic graph

Page 6: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Rooted Tree

• A directed graph with following properties– One node is distinguished as root – Every node c, except root, has exactly one

incoming edge from another node p. p is c’s parent.

– There exists a unique path from root to any node

Page 7: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Tree Properties and Definitions• A tree with N nodes has N-1 edges• Siblings – Nodes with same parent• Ancestor & descendant

– If there is a path from u to v, v is the descendant of u & u is the ancestor of v

– If u ≠ v, v is proper descendant of u & u is proper ancestor of v

Page 8: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Definitions (Contd.)• Depth of a node – Length of path from root

to node– Depth of root is 0– Depth of any node is one more than its

parent’s depth• Height of a node – Length of path from

node to deepest descendant leaf• Size of a node – Number of descendants

of the node (including itself)• Height of a tree – Height of the root of the

tree

Page 9: Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems

Example