Post on 27-Dec-2015
TREES
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)
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
Visualizing a Tree
N
AP
T
MI
O
R
G
RootChild
Sample Uses
A company’s organizational structureFamily treeThe Java class hierarchyO/S directory structureBook (parts, chapters, sections)Arithmetic expressionsWeb page links (?)
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
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
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
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.
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
More Tree Operations
children(v)
isInternal(v)
Return children of vInput: PositionOutput: Enumeration of Positions
Test whether position v is internalInput: PositionOutput: boolean
More Tree Operations
isExternal(v)
isRoot(v)
Test whether position v is externalInput: PositionOutput: boolean
Test whether position v is the rootInput: PositionOutput: boolean
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
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
Algorithms on Trees
Depth computationHeight computation
node heighttree height
Traversalspreorderpostorder
Computing Depth
Algorithm depth(v)Input: Node vOutput: Integer
if isRoot(v)return 0
elsereturn 1 + depth(parent(v))
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
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
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)
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)
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)
Traversals - Query
Given the following tree, show (by arrows) a preorder and a post-order traversal.
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
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
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
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)
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))
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
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
Binary Trees- Query
What tree is produced by a pre-order traversal of a binary tree ?
Binary Trees - Query
66
4411
22 88
33
Binary Tree Uses
Heaps / Priority queuesArithmetic expression treesDecision treesBinary-search trees
Database storage (B+ Trees)
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
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))
Binary Trees - Inorder Traversal
--
33xx
// 88
1133
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
Expression Trees
Example
The expression (a+b*c)+((d*e+f)*g)can be represented as follows :
Expression Trees
++
**aa
++ **
bb cc
ee
**
++
dd
gg
ff
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
Expression Trees - Query
Make an expression tree of the following formula : (a*b) * (c+d) - e
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]
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
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
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)
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))
Binary Search Trees
66
4411
22 88
33
66
66
22 88
77
4411
33
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).
AVL Binary Search Trees
66
4411
22 88
33
66
66
22 88
77
4411
33
7
AVL Binary Search Trees
Binary Search Trees are transformed to AVL Binary Search Trees by a process called rotation
Single Rotation
Consider the binary search tree X (k1<k2, X<k1,Z>k2)
ZZZ
k2
k1
X Y
k2X
k1
ZY
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.
THE END