data structures lakshmish ramaswamy. tree hierarchical data structure several real-world systems...
DESCRIPTION
Tree – Informal Definition Each node has a single parent node except for root which has no parent root parent nodes children nodes leaf nodesTRANSCRIPT
Data Structures
Lakshmish Ramaswamy
Tree• Hierarchical data structure• Several real-world systems have
hierarchical concepts– Physical and biological systems– Organizations
• Many other applications– Sorting– Searching– Databases– Internet
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
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
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
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
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
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
Example