* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Lecture 15 Trees
Survey
Document related concepts
Transcript
Data Structures
TREES
Azhar Maqsood
School of Electrical Engineering and Computer Sciences
(SEECS-NUST)
What is a tree?
• In computer science, a tree is an abstract
model of a hierarchical structure
• A tree consists of nodes with a parentchild relation
• Previous data structures placed data in
linear order.
• Some data organizations require
categorizing data into groups, subgroups
Tree Terminology
• Trees are a hierarchical data structure of
nodes
• Nodes are linked by edges
4
nodes
2
5
edge
1
3
Applications
• Organization charts
• File systems
• Programming environments
• A class hierarchy in programming languages that
support single inheritance (e.g. Java)
• Document Object Model (for HTML and XML
• Artificial Intelligence (Decision making etc)
tree is hierarchical classification
• A tree is hierarchical classification.
• Data items appear at various levels within the
organization
• E.g., directory structure:
tree is hierarchical classification
Decision Trees
Decision tree based upon expert system
Tree Terminology (Parent / Child)
• Root: node without parent
• A parent node references one or
more nodes (children nodes)
that are “lower” in the tree
hierarchy
• If node u is the parent of node v,
then v is the child of u
• Except for the root (no parent),
every node has exactly one
parent (by definition)
• A tree has only one root node
u
v
root
Tree Terminology (Siblings)
• Two nodes that are children of the same
parent.
Tree Terminology (Internal node)
• A node is internal if it has one or more
children
Tree Terminology (Leaf /External node)
• A node is a leaf if it has no children
Tree Terminology (Ancestor / Descendent)
• An ancestor of a node is either the node’s
parent or any ancestor of the node’s parent
(this is recursive)
• The root is an ancestor of each other node
• Descendant of a node: child, grandchild,
grand-grandchild
u
u
v
v
Tree Terminology (Subtree)
• A tree may be divided into subtrees.
• A subtree is a tree that has the child of a node as its
root.
• Hence, a subtree is composed of a node and all of
that node’s descendants.
• The first node in a subtree is known as the root of
the subtree and is used to name the subtree.
• Subtrees themselves can be further divided into
other subtrees.
Tree Terminology (Subtree)
• The subtree of T rooted at node v is the tree
consisting of all the descendents of v in T
(including v)
• tree consisting of a node and its descendants
Root of subtree
v
v
Tree Terminology (Depth of a node)
• The depth of a node v in T is the number of
ancestors of v, excluding v itself.
• More formally:
• If v is the root, the depth of v is 0
v
depth of v = 1
v
depth of v = 3
Tree Terminology( Height /depth of a tree)
• The depth of a tree is the maximum depth of
any of its leaves
• maximum levels of a tree
tree depth = 3
tree depth = 2
tree depth = 0
Terminology …
Height of the tree?
Depth of node B?
Terminology
Parents: A, B, F
Children: B, E, F, C, D, G, H, I
Siblings: {B, E, F}, {C, D}, {G, H, I}
Leaves: C, D, E, G, H, I
Internal Nodes: A, B, F
Terminology
• Two nodes are adjacent if a branch connects
them.
• A path is a sequence of nodes in which each
node is adjacent to the next one.
• Every node in the tree can be reached by
following a unique path starting from the
root.
• The length of this path is the number of
edges on the path.
• There is a path of length 0 from every node
to itself.
Terminology
• The path from the root, A, to the leaf, I, is denoted
as AFI and has a length of 2.
• ABD is the path from the root, A, to the leaf, D,
and also has a length of 2.
Types of Trees
• General tree – a node
can have any number
of children
• Binary tree – a node
can have at most two
children
Binary Tree
Binary Trees
• The simplest form of tree is a Binary Tree
• A Binary Tree consists of
• (a) A node (called the root node) and
• (b) Left and right subtrees
• Both the subtrees are themselves binary trees
• Note: this is a recursive definition
• (A node can’t have more than 2 children)
Binary tree
General tree
Binary Trees
• Finite set of nodes that is either empty, or
consists of a root and two disjoint binary
trees called the left subtree and right
subtree.
• Node contains information and two pointers to
other nodes
• Each node has at most two children
• A binary tree is either empty or has the
following form:
root
TL
TR
• Where Tleft and Tright are binary trees.
Binary Trees
• Full binary tree: All leaves on the same level
and every node has either zero or two
children.
• Complete binary tree: Leaves are filled from
left to right on one level before moving to
next level.
Binary Trees
Binary Trees
• Skewed binary tree: Contains only left or
right children.
• Similar: Two trees with same structure and
different data.
• Copy or identical: Same structure and
same data.
Tree Height and Full Binary Tree
• If
h = height of a binary tree,
max # of leaves = 2h
max # of nodes = 2h + 1 - 1
• A binary tree with height h
and 2h + 1 - 1 nodes (or 2h
leaves) is called a full binary
tree
Binary tree
Visiting and Traversing a Node
• Many applications require that all of the
nodes of a tree be “visited”.
• Visiting a node may mean printing contents,
retrieving information, making a calculation,
etc.
• Traverse: To visit all the nodes in a tree in a
systematic fashion.
• A traversal can pass through a node without
visiting it at that moment.
Binary Tree Structure
• The representation of a binary tree structure is
relatively straightforward.
• We need a variable to store the data at the
node and 2 pointers to the left and right
subtrees.
struct Node {
int data
Node *left
Node *right
}
Binary Tree Structure