Data StructuresData Structures
Azhar MaqsoodSchool of Electrical Engineering and Computer School of Electrical Engineering and Computer
Sciences (SEECS-NUST)Sciences (SEECS-NUST)
TREESTREES
What is a tree?What is a tree?
• In computer science, a tree is an abstract model of a hierarchical structure
• A tree consists of nodes with a parent-child relation
• Previous data structures placed data in linear order.
• Some data organizations require categorizing data into groups, subgroups
Tree TerminologyTree Terminology
• Trees are a hierarchical data structure of nodes
• Nodes are linked by edges
4
2 5
1 3
edge
nodes
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 TreesDecision Trees
Decision tree based upon expert systemDecision tree based upon expert system
Tree Terminology (Parent / Child)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 nodeA tree has only one root node
u
v
root
Tree Terminology (Siblings)Tree Terminology (Siblings)
• Two nodes that are children of the same parent.
Tree Terminology (Internal node)Tree Terminology (Internal node)
• A node is internal if it has one or more children
Tree Terminology (Leaf /External node)Tree Terminology (Leaf /External node)
• A node is a leaf if it has no children
Tree Terminology (Ancestor / Descendent)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-grandchildu
v
u
v
Tree Terminology (Subtree)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)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
v
v
Root of subtree
Tree Terminology (Depth of a node)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
depth of v = 1 depth of v = 3
v
v
Tree Terminology( Height /depth of a tree)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 …Terminology …
Height of the tree?Depth of node B?
TerminologyTerminology
Parents: A, B, F Leaves: C, D, E, G, H, IChildren: B, E, F, C, D, G, H, I Internal Nodes: A, B, FSiblings: {B, E, F}, {C, D}, {G, H, I}
TerminologyTerminology
• 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.
TerminologyTerminology
• 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 TreesTypes of Trees
• General tree – a node can have any number of children
• Binary tree – a node can have at most two children
Binary TreeBinary Tree
Binary TreesBinary 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)
General treeBinary tree
Binary TreesBinary 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:
• Where Tleft and Tright are binary trees.
root
TLTR
Binary TreesBinary 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 TreesBinary Trees
Binary TreesBinary 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 TreeTree 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 NodeVisiting 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 StructureBinary 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 dataNode *leftNode *right
}
Binary Tree StructureBinary Tree Structure
Top Related