data structures and algorithms trees · data structures and algorithms trees sidra malik...

35
Data Structures and Algorithms Trees Sidra Malik [email protected]

Upload: others

Post on 15-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Data Structures and Algorithms

TreesTrees

Sidra [email protected]

Page 2: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree?

• In computer science, a tree is an abstract

model of a hierarchical structure

• A tree is a finite set of one or more nodes such that:such that:

– There is a specially designated node called the root.

– The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree.

– We call T1, ..., Tn the subtrees of the root.

DS&A--Sidra Malik 2

Page 3: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree Terminology

• Trees are a hierarchical data structure of nodes

• Nodes are linked by edges

4

2 5

1 3

edge

nodes

3DS&A--Sidra Malik

Page 4: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Applications

• Organization charts

• File systems

• Programming environments

– A class hierarchy in programming languages that support – 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)

4DS&A--Sidra Malik

Page 5: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Hierarchical Classification

• A tree is hierarchical classification.

– Data items appear at various levels within the organization

– E.g., directory structure:

5DS&A--Sidra Malik

Page 6: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Decision Trees

6DS&A--Sidra Malik

Page 7: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Decision Tree based upon Expert System

7DS&A--Sidra Malik

Page 8: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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

u

v

root

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

v

8DS&A--Sidra Malik

Page 9: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree Terminology…

Siblings

– Two nodes that are children of the same

parent.

9DS&A--Sidra Malik

Page 10: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree Terminology…

Internal node

• A node is internal if it has one or more

children

10DS&A--Sidra Malik

Page 11: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree Terminology…

Leaf /External node

– A node is a leaf if it has no children

11DS&A--Sidra Malik

Page 12: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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-• Descendant of a node: child, grandchild, grand-

grandchildu

v

u

v12DS&A--Sidra Malik

Page 13: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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.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.

13DS&A--Sidra Malik

Page 14: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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

Root of subtree

14DS&A--Sidra Malik

Page 15: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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

15DS&A--Sidra Malik

Page 16: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree Terminology…

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

16DS&A--Sidra Malik

Page 17: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Tree Terminology…

Height of a Tree

• The Height of a tree is the maximum number

of nodes possible in a path starting from root

to leaf

• H= max levels + 1• H= max levels + 1

Tree height= 4

tree height= 3height= 1

17DS&A--Sidra Malik

Page 18: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Terminology …

Height of the tree?

Depth of node B?

18DS&A--Sidra Malik

Page 19: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Terminology

Parents: Leaves:Children: Internal Nodes:Siblings:

19DS&A--Sidra Malik

Page 20: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Terminology

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}

20DS&A--Sidra Malik

Page 21: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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 • 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.

21DS&A--Sidra Malik

Page 22: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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.

22DS&A--Sidra Malik

Page 23: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Types of Trees

• General tree – a node

can have any number of

children

• Binary tree – a node can

have at most two

children

23

Page 24: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Tree

Page 25: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Trees• The simplest form of tree is a Binary Tree

• A Binary Tree consists of

– T which is empty

– A node (called the root node) and

– Disjoint Left and right subtrees , T1 and T2

– Both the subtrees are themselves binary trees– 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

25DS&A--Sidra Malik

Page 26: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Trees…

Difference b/w Trees and Binary Trees

• A Tree can never be empty but a binary tree may be

empty.

• Binary Tree can’t have more than 2 children whereas in

case of a tree a node may have any number of childrencase of a tree a node may have any number of children

General treeBinary tree

26DS&A--Sidra Malik

Page 27: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Tree

• A binary tree is either empty or has the

following form:

root

• Where Tleft and Tright are binary trees.

TL

TR

27DS&A--Sidra Malik

Page 28: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Trees

• Full binary tree: is said to be full if it contains

maximum possible number of nodes in all

levels

• Complete binary tree: is said to be complete if

it contains maximum possible number of

nodes except possibly the last level

28DS&A--Sidra Malik

Page 29: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Trees

29DS&A--Sidra Malik

Page 30: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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 • Copy or identical: Same structure and same

data.

30DS&A--Sidra Malik

Page 31: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Properties of Binary Trees

• Max number of nodes on level l is 2l , l >= 0

• If h = height of a binary tree, max number of nodes possible are = 2h - 1

• Minimum number of nodes possible in

binary Tree are hbinary Tree are h

• For any non empty binary Tree, if n is number of nodes and e are edges then

n=e+1

• A binary tree with height h and 2h - 1 nodes is called a full binary tree

Binary tree

31DS&A--Sidra Malik

Page 32: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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 dataNode *leftNode *right

}

32DS&A--Sidra Malik

Page 33: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Tree Structure

33DS&A--Sidra Malik

Page 34: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

Binary Tree Operations

• Insertion

– Into empty or existing Tree

• Deletion

– From a non empty Tree

• Traversal• Traversal

– To visit all nodes in Binary Tree

• Merge

– To merge two binary trees into a larger one

34DS&A--Sidra Malik

Page 35: Data Structures and Algorithms Trees · Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk. Tree? • In computer science, a tree is an abstract model

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, 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.

35DS&A--Sidra Malik