Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)...

33
Data Structures Data Structures Azhar Maqsood School of Electrical Engineering and School of Electrical Engineering and Computer Sciences (SEECS-NUST) Computer Sciences (SEECS-NUST) TREES TREES

Transcript of Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)...

Page 1: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Data StructuresData Structures

Azhar MaqsoodSchool of Electrical Engineering and Computer School of Electrical Engineering and Computer

Sciences (SEECS-NUST)Sciences (SEECS-NUST)

TREESTREES

Page 2: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 3: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Tree TerminologyTree Terminology

• Trees are a hierarchical data structure of nodes

• Nodes are linked by edges

4

2 5

1 3

edge

nodes

Page 4: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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)

Page 5: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

tree is hierarchical classification

• A tree is hierarchical classification.• Data items appear at various levels within the

organization• E.g., directory structure:

Page 6: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

tree is hierarchical classification

Page 7: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Decision TreesDecision Trees

Page 8: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Decision tree based upon expert systemDecision tree based upon expert system

Page 9: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 10: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Tree Terminology (Siblings)Tree Terminology (Siblings)

• Two nodes that are children of the same parent.

Page 11: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Tree Terminology (Internal node)Tree Terminology (Internal node)

• A node is internal if it has one or more children

Page 12: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Tree Terminology (Leaf /External node)Tree Terminology (Leaf /External node)

• A node is a leaf if it has no children

Page 13: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 14: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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.

Page 15: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 16: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 17: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 18: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Terminology …Terminology …

Height of the tree?Depth of node B?

Page 19: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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}

Page 20: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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.

Page 21: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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.

Page 22: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 23: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Binary TreeBinary Tree

Page 24: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 25: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 26: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

• A binary tree is either empty or has the following form:

• Where Tleft and Tright are binary trees.

root

TLTR

Page 27: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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.

Page 28: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Binary TreesBinary Trees

Page 29: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) 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.

Page 30: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

Page 31: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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.

Page 32: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

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

}

Page 33: Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) TREES.

Binary Tree StructureBinary Tree Structure