data structures & algorithms trees. tree in computer science, a tree is an abstract model of a...

33
Data Structures & Data Structures & Algorithms Algorithms Trees Trees

Upload: coleen-oneal

Post on 03-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Data Structures & AlgorithmsData Structures & Algorithms

TreesTrees

TreeTree

• 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, sub-groups

Tree TerminologyTree Terminology

• Trees are a hierarchical data structure of nodes

• Nodes are linked by edges4

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 A tree has only one root nodenode

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 (Sub-tree)Tree Terminology (Sub-tree)

• A tree may be divided into sub-trees.• A sub-tree is a tree that has the child of a

node as its root.• Hence, a sub-tree is composed of a node

and all of that node’s descendants.• The first node in a sub-tree is known as the

root of the sub-tree and is used to name the sub-tree.

• Sub-trees themselves can be further divided into other sub-trees.

Tree Terminology (Sub-tree)Tree Terminology (Sub-tree)

• The sub-tree 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 sub-tree

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 TreesBinary 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 sub-trees• Both the sub-trees 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 sub-tree and right sub-tree• 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

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

struct Node {int dataNode *leftNode *right

}

Binary Tree StructureBinary Tree Structure