trees. linked lists vs trees zwith large amounts of data, access time for linked lists is quite...

52
TREES

Upload: bridget-park

Post on 27-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

TREES

Page 2: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Linked Lists vs Trees

With large amounts of data, access time for linked lists is quite long.

Access time can be improved with trees - Linked List : O (n)- Binary Tree : O (square root of n)- Binary Search Tree : O (log n)

Page 3: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Definition

The Tree Data Structurestores objects (nodes) hierarchicallynodes have parent-child relationshipsoperations include accessing the parent or

children of a given nodeA tree T is a set of nodes such that

there is a distinguished node r (called the root) of T that has no parent

each node v of T except r has a parent node

Page 4: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Visualizing a Tree

N

AP

T

MI

O

R

G

RootChild

Page 5: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Sample Uses

A company’s organizational structureFamily treeThe Java class hierarchyO/S directory structureBook (parts, chapters, sections)Arithmetic expressionsWeb page links (?)

Page 6: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Tree Terminology

Rootthe only node that has no parent

Leaf (External node)node that has no children

Internal nodenode that has at least one child

Siblingsnodes that have a common parent

Page 7: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

More Tree Terminology

Ancestorrecursive definition: ancestors of a node v

are v itself and the ancestors of its parentproper ancestors: ancestors excluding itself

Descendantv is a descendant of u if u is an ancestor of

vSubtree of T rooted at v

set of all descendants of v

Page 8: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Even More Terminology

Ordered Treechildren of a node have a strict linear order

Binary Treean ordered tree where nodes have at most

two children (left and right)Depth and Height of a node

depth: distance from node to rootheight: from node to its farthest descendant

Page 9: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Tree ADT

Tree-specific operationsretrieval: root(), parent(v), children(v)test: isInternal(v), isExternal(v), isRoot(v)

Container-specific operationsTree ADT is just a special type of

containermethods: size(), isEmpty(), replace(v,e),

etc.

Page 10: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Tree Operations

root()

parent(v)

Return the root of T; error if tree is emptyInput: NoneOutput: Position (Node)

Return the parent of v;error if v = root()Input: PositionOutput: Position

Page 11: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

More Tree Operations

children(v)

isInternal(v)

Return children of vInput: PositionOutput: Enumeration of Positions

Test whether position v is internalInput: PositionOutput: boolean

Page 12: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

More Tree Operations

isExternal(v)

isRoot(v)

Test whether position v is externalInput: PositionOutput: boolean

Test whether position v is the rootInput: PositionOutput: boolean

Page 13: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Container-Specific Operations

size(): returns number of nodes/positions isEmpty(): test for empty treepositions(): returns all positionselements(): returns all objects/elementsswap(v,w): swaps elements of 2 positionsreplace(v,e): assigns element to position

Page 14: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Tree Implementation

Sequence/Array-based implementationelements (or references to them) are stored

in an arrayparent-child relationships derived from

indicesLinked implementation

elements stored in nodesnodes contain pointers to parent and

children

Page 15: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Algorithms on Trees

Depth computationHeight computation

node heighttree height

Traversalspreorderpostorder

Page 16: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Computing Depth

Algorithm depth(v)Input: Node vOutput: Integer

if isRoot(v)return 0

elsereturn 1 + depth(parent(v))

Page 17: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Computing Node Height

Algorithm height(v) Input: Node vOutput: Integer

if isExternal(v) // leaf nodes have 0 heightreturn 0

else

m max[height(w)] where w is a child of v

return 1 + m

Page 18: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Computing Tree Height

Method 1compute the depths of all the leavesget the maximum

Method 2compute height of root recursively height of a node is one plus the maximum

height of the subtrees rooted at its childrenheights of subtrees rooted at leaves: 0

Page 19: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Traversals

Traversalsystematic way of accessing or visiting

the nodes of a treePreorder

visit root first, then traverse its subtrees (recursive)

Postordervisit subtrees first, then root (recursive)

Page 20: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Preorder Traversal

Algorithm preorder(v)Input: Node vOutput: Depends on application

perform the “visit” action for node v// example: print element(v)for each child w of v do preorder(w)

Page 21: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Postorder Traversal

Algorithm postorder(v)Input: Node vOutput: Depends on application

for each child w of v do postorder(w)perform the “visit” action for node v// example: print element(v)

Page 22: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Traversals - Query

Given the following tree, show (by arrows) a preorder and a post-order traversal.

Page 23: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Traversals-Query

Paper

Chap 2.3

Chap 1

Chap 2.2Chap 1.2 Chap 2.1Chap 1.1

Chap 2Title Abstract References

Chap 3.2Chap 3.1

Chap 3

Page 24: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Traversals-Pre-Order

Paper

Chap 2.3

Chap 1

Chap 2.2Chap 1.2 Chap 2.1Chap 1.1

Chap 2Title Abstract References

Chap 3.2Chap 3.1

Chap 3

Page 25: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Traversals-Post-Order

Paper

Chap 2.3

Chap 1

Chap 2.2Chap 1.2 Chap 2.1Chap 1.1

Chap 2Title Abstract

Paper

References

Chap 3.2Chap 3.1

Chap 3

Page 26: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Trees

Definition - A binary tree is a tree in which no nodes can have more than two children

- average depth is O (square root of n)

- special tree, binary search tree has an average depth of O (log n)

Page 27: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree Properties

Given a binary tree withn nodes, i internal nodes, e external nodesand height h:h+1 <= e <= 2h

h <= i <= 2h -12h +1 <= n <= 2h+1 -1log(n+1) -1 <= h <= (n-1)/2

(Minimum height is O(log n))

Page 28: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree access methods

leftChild(v)

rightChild(v)

Return the left child of v; error if externalInput: PositionOutput: Position

Return the right child of v; error if externalInput: PositionOutput: Position

Page 29: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree insertion update methods

expandExternal(v)

removeAboveExternal(v)

Create two nodes under node v; error if internal

Remove parent of v including its two children; error if internal

Page 30: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Trees- Query

What tree is produced by a pre-order traversal of a binary tree ?

Page 31: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Trees - Query

66

4411

22 88

33

Page 32: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree Uses

Heaps / Priority queuesArithmetic expression treesDecision treesBinary-search trees

Database storage (B+ Trees)

Page 33: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree Traversals

Preorder and PostorderSame as general caseWhen processing children of a node, left

child first, right child nextInorder Traversal

applies to binary trees onlyrecursively process left subtree, then

root, then right subtree

Page 34: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Inorder Traversal for Binary Trees

Algorithm inorder(v)Input: Node vOutput: Depends on application

if isInternal(v) then inorder(leftChild(v))perform the “visit” action for node v// example: print element(v)if isInternal(v) then inorder(rightChild(v))

Page 35: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Trees - Inorder Traversal

--

33xx

// 88

1133

Page 36: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Trees - Expression

Expressions can be represented by a binary tree

The leaves of an expression tree are operands (i.e. constants, variables,etc)

Other nodes are the operators

Page 37: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Expression Trees

Example

The expression (a+b*c)+((d*e+f)*g)can be represented as follows :

Page 38: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Expression Trees

++

**aa

++ **

bb cc

ee

**

++

dd

gg

ff

Page 39: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Expression Trees

If you do a post-traversal of the tree, the result is as follows :

a b c * + d e f + g * +

Postfix notation of the expressionCompiler design

Page 40: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Expression Trees - Query

Make an expression tree of the following formula : (a*b) * (c+d) - e

Page 41: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree Array-Based Implementation

Use array S[1..max] to store elementsderive parent/child relationships from indices

Let i be an array index, S[i] be the nodei = 1 for the root nodeleft child of S[i] is S[2*i]right child of S[i] is S[2*i+1]parent of S[i] is S[i/2]

Slightly different formula if S[0..max-1]

Page 42: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

BT Array Implementation continued

Implementations of root(), parent(v), leftChild(v), rightChild() and test methodssimple arithmetic computations

May need an integer n to represent last valid node/first available node in the arraye.g., when implementing a heap

Disadvantages:maximum size has to be predeterminedspace-inefficient for “skewed” trees

Page 43: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Tree Linked Implementation

Need a Node class containingelementleft, right and parent of class Nodeusual set/get methods

Binary Tree class now containsroot variable of class Nodesize variable (int) to monitor tree size

Page 44: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

BT Linked Implementation continued

Access and test methodsall involve inspecting node data

expandExternal(v) create two nodes and set some pointers

removeAboveExternal(v)reset child pointer of grandparent to nulldispose garbage nodes (automatic in Java)

Page 45: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Search Trees

For every node x, in the tree, the values of all the keys in the left subtree are smaller than the key value in x and the values of all the keys in the right subtree are larger than the key value in x

Average depth (access time) is O (log(n))

Page 46: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Search Trees

66

4411

22 88

33

66

66

22 88

77

4411

33

Page 47: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Binary Search Tree - AVL

Definition- It is a binary search tree with a balance condition- for every node in the tree, the average height of the left and right subtrees can differ by at most one (1).

Page 48: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

AVL Binary Search Trees

66

4411

22 88

33

66

66

22 88

77

4411

33

7

Page 49: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

AVL Binary Search Trees

Binary Search Trees are transformed to AVL Binary Search Trees by a process called rotation

Page 50: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

Single Rotation

Consider the binary search tree X (k1<k2, X<k1,Z>k2)

ZZZ

k2

k1

X Y

k2X

k1

ZY

Page 51: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

AVL Binary Search Trees

Problem :Construct a AVL Binary Search Tree of the keys 1 - 7 assuming that the keys will be in sequential order.

Page 52: TREES. Linked Lists vs Trees zWith large amounts of data, access time for linked lists is quite long. zAccess time can be improved with trees - Linked

THE END