0% found this document useful (0 votes)
24 views60 pages

Lecture 3 Trees

trees

Uploaded by

degagaalemayehu5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views60 pages

Lecture 3 Trees

trees

Uploaded by

degagaalemayehu5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 60

Tree Data Structures

Definition
 A tree is a finite nonempty set of elements.
 It is an abstract model of a hierarchical structure.
 consists of nodes with a parent-child relation.
 Applications:
 Organization charts

 File systems
Computers”R”Us
 Programming environments

Sales Manufacturing R&D

US International Laptops Desktops

Europe Asia Canada


Trees Data Structures
 Tree
 Nodes
 Each node can have 0 or more children
 A node can have at most one parent
 Binary tree
 Tree with 0–2 children per node

Tree Binary Tree


Trees
 Terminology
 Root  no parent
 Leaf  no child
 Interior  non-leaf
 Height  distance from root to leaf
Root node

Interior nodes Height

Leaf nodes
Binary Search Trees

 Key property
 Value at node
 Smaller values in left subtree
 Larger values in right subtree
 Example
X
 X>Y
 X<Z

Y Z
Binary Search
 Binary search can perform operation find(k) on a dictionary
implemented by means of an array-based sequence, sorted by key
 similar to the high-low game
 at each step, the number of candidate items is halved
 terminates after O(log n) steps
 Example: find(7)

0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
lm h
Binary Search Trees 6
Binary Search Trees
 Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10

25
Binary Not a binary
search trees search tree
Binary Tree Implementation

class BinaryNode
{
// Friendly data; accessible by other package
routines
Object element; // The data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}
Iterative Search of Binary
Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key) // Found it
return n;
if (n->data > key) // In left subtree
n = n->left;
else // In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
Recursive Search of Binary
Tree
Node *Find( Node *n, int key) {
if (n == NULL) // Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key) // In left subtree
return Find( n->left, key );
else // In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
Example Binary Searches
 Find ( root, 2 )

root
10 5
10 > 2, left 5 > 2, left
5 30 5 > 2, left 2 45 2 = 2, found
2 = 2, found
2 25 45 30

10

25
Example Binary Searches
 Find (root, 25 )

10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found

25
Types of Binary Trees
 Degenerate – only one child
 Complete – always two children
 Balanced – “mostly” two children
 more formal definitions exist, above are intuitive ideas

Degenerate Balanced Complete


binary tree binary tree binary tree
Binary Trees Properties
 Degenerate
 Balanced
 Height = O(n) for n
 Height = O( log(n) )
nodes for n nodes
 Similar to linked list
 Useful for searches

Degenerate Balanced
binary tree binary tree
Binary Search Properties

 Time of search
 Proportional to height of tree
 Balanced binary tree
 O( log(n) ) time
 Degenerate tree
 O( n ) time
 Like searching linked list / unsorted array
Binary Search Tree
Construction
 How to build & maintain binary trees?
 Insertion
 Deletion
 Maintain key property (invariant)
 Smaller values in left subtree
 Larger values in right subtree
Binary Search Tree –
Insertion
 Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for
Y
 Observations
 O( log(n) ) operation for balanced tree
 Insertions may unbalance tree
Example Insertion

 Insert ( 20 )
10 10 < 20, right
30 > 20, left
5 30
25 > 20, left
2 25 45 Insert 20 on left

20
Binary Search Tree –
Deletion
 Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
 O( log(n) ) operation for balanced tree
 Deletions may unbalance tree
Example Deletion (Leaf)

 Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
Example Deletion (Internal
Node)
 Delete ( 10 )
10 5 5

5 30 5 30 2 30

2 25 45 2 25 45 2 25 45

Replacing 10 Replacing 5 Deleting leaf


with largest with largest
value in left value in left
subtree subtree
Example Deletion (Internal
Node)
 Delete ( 10 )
10 25 25

5 30 5 30 5 30

2 25 45 2 25 45 2 45

Replacing 10 Deleting leaf Resulting tree


with smallest
value in right
subtree
Performance
 Consider a dictionary
with n items
implemented by means
of a binary search tree
of height h
 the space used is O(n)
 methods find, insert and
remove take O(h) time
 The height h is O(n) in
the worst case and
O(log n) in the best
case

Binary Search Trees 23


Tree Traversal
 Traversal is a process to visit all the nodes of a tree and
may print their values too. Because, all nodes are
connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node
in a tree. There are three ways which we use to traverse a
tree −
 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
 Generally, we traverse a tree to search or locate a given
item or key in the tree or to print all the values it contains.
In-order Traversal

 In this traversal method, the left subtree is


visited first, then the root and later the right
sub-tree. We should always remember that
every node may represent a subtree itself.
 If a binary tree is traversed in-order, the
output will produce sorted key values in an
ascending order.
Continued
 We start from A, and
following in-order
traversal, we move to its
left subtree B. B is also
traversed in-order. The
process goes on until all
the nodes are visited. The
output of inorder traversal
of this tree will be −
 D→B→E→A→F→
C→G
Pre-Order
 In this traversal method,
the root node is visited
first, then the left subtree
and finally the right
subtree
 The process goes on until
all the nodes are visited.
The output of pre-order
traversal of this tree will
be −
 A→B→D→E→C→
F→G
Post-order Traversal
 We start from A, and
following Post-order
traversal, we first visit the
left subtree B. B is also
traversed post-order. The
process goes on until all
the nodes are visited. The
output of post-order
traversal of this tree will
be D → E → B → F → G
→ C → A−
Tree Traversal Summary
 A tree traversal is a specific order in which to trace
the nodes of a tree.
 There are 3 common tree traversals.
1. in-order: left, root, right
2. pre-order: root, left, right
3. post-order: left, right, root
 This order is applied recursively. So for in-order, we
must print each sub tree's left branch before we print
its root.
 Note "pre" and "post" refer to when we visit the root.

Binary Search Trees 29


Inorder Traversal
 In an inorder traversal a node is Algorithm inOrder(v)
visited after its left subtree and
before its right subtree if isInternal (v)
inOrder (leftChild
(v))
visit(v)
if isInternal (v)
6 inOrder
(rightChild (v))
2 8

1 4 7 9

3 5
Preorder Traversal
 A traversal visits the nodes of a tree Algorithm preOrder(v)
in a systematic manner
visit(v)
 In a preorder traversal, a node is
for each child w of
visited before its descendants
v
 Application: print a structured
preorder (w)
document

1
Become Rich

2 5 9
1. Motivations 2. Methods 3. Success Stories

3 4 6 7 8
1.1 1.2 Help
Enjoy 2.1 Get a 2.2 Start a 2.3 Acquired
Poor
Life CS PhD Web Site by Google
Friends
Postorder Traversal

 In a postorder traversal, a node is Algorithm


visited after its descendants postOrder(v)
 Application: compute space used for each child w of
by files in a directory and its v
subdirectories
postOrder
(w)
9 visit(v)
cs16/

8
3 7
todo.txt
homeworks/ programs/
1K

1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Continued

Binary Search Trees 33


Quiz

 Given inorder and pre order of tree draw the


binary tree
(a) Inorder : 10, 20, 30, 100, 150, 200 , 300

(b) Preorder: 100 , 20 , 10 , 30 , 200 , 150, 300


AVL Trees

6
v
3 8
z
4

AVL Trees 35
Self adjusting Trees
 Ordinary binary search trees have no balance
conditions
 what you get from insertion order is it
 Balanced trees like AVL trees enforce a balance
condition when nodes change
 tree is always balanced after an insert or delete
 Self-adjusting trees get reorganized over time as
nodes are accessed
 Tree adjusts after insert, delete, or find

36
AVL Tree Definition

 AVL trees are 4


44
balanced.
2 3
 An AVL Tree is a 17 78
binary search tree 1 2 1
32 50 88
such that for every 1 1
internal node v of T, 48 62
the heights of the
children of v can differ
by at most 1. An example of an AVL tree where the
heights are shown next to the nodes:

AVL Trees 37
Insertion in an AVL Tree
 Insertion is as in a binary search tree
 Always done by expanding an external node.
 Example: 44 44

c=z
17 78 17 78
a=y

32 50 88 32 50 88

48 62 48 62
b=x

54
w

before insertion after insertion

AVL Trees 38
Trinode Restructuring
 let (a,b,c) be an inorder listing of x, y, z
 perform the rotations needed to make b the topmost node of the
three
(other two cases
a=z case 2: double rotation
are symmetrical)
a=z (a right rotation about c,
c=y then a left rotation about a)
b=y T0
T0 b=x
c=x
T3
b=y b=x
T1
T1 T2
T2 T3 a=z c=x a=z c=y

case 1: single rotation


(a left rotation about a) T0 T1 T2 T3 T0 T1 T2 T3

AVL Trees 39
Insertion Example,
continued 44
5

2
z 64
17 78 7
3
2y 1
1
32 1 50 4 88
1
2 x
48
1
3 62
5
54 T3
unbalanced... T2
T0
4
T1 44 4
2 3 x
17
2 y 62
z6
1 2 2
32
1
1 50 3 5
78 7
1 1
...balanced 48 54 88

T2

T0 T1 T3
AVL Trees 40
Restructuring
(as Single Rotations)
 Single Rotations:

a=z single rotation b=y


b=y a=z c=x
c=x

T0 T3
T1 T3 T0 T1 T2
T2

c=z single rotation b=y


b=y a=x c=z
a=x

T3 T3
T0 T2 T2 T1 T0
T1
AVL Trees 41
Restructuring
(as Double Rotations)
 double rotations:

a=z double rotation b=x


c=y a=z c=y
b=x

T0 T2
T2 T3 T0 T1 T3
T1

c=z double rotation b=x


a=y a=y c=z
b=x

T0 T2
T3 T2 T3 T1 T0
T1
AVL Trees 42
Removal in an AVL Tree
 Removal begins as in a binary search tree, which means the node
removed will become an empty external node. Its parent, w, may
cause an imbalance.
 Example:
44 44

17 62 17 62

32 50 78 50 78

48 54 88 48 54 88

before deletion of 32 after deletion

AVL Trees 43
Rebalancing after a
Removal
 Let z be the first unbalanced node encountered while travelling
up the tree from w. Also, let y be the child of z with the larger
height, and let x be the child of y with the larger height.
 We perform restructure(x) to restore balance at z.
 As this restructuring may upset the balance of another node
higher in the tree, we must continue checking for balance until
the root of T is reached
62
a=z 44

44 78
w 17 62 b=y

17 50 88
50 78 c=x

48 54
48 54 88

AVL Trees 44
Running Times for
AVL

Trees
a single restructure is O(1)
 using a linked-structure binary tree
 find is O(log n)
 height of tree is O(log n), no restructures needed
 insert is O(log n)
 initial find is O(log n)
 Restructuring up the tree, maintaining heights is O(log n)
 remove is O(log n)
 initial find is O(log n)
 Restructuring up the tree, maintaining heights is O(log n)

AVL Trees 45
AVL Trees 46
AVL Trees 47
AVL Trees 48
AVL Trees 49
AVL Trees 50
AVL Trees 51
AVL Trees 52
AVL Trees 53
AVL Trees 54
Problems

Show the AVL tree that results after each of the


integer keys 9, 27, 50, 15, 2, 21, and 36 are
inserted, in that order, into an initially empty
AVL tree. Clearly show the tree that results
after each insertion, and make clear any
rotations
that must be performed

AVL Trees 55
Balanced Search Trees
 Kinds of balanced binary search trees
 height balanced vs. weight balanced
 “Tree rotations” used to maintain balance on insert/delete
 Non-binary search trees
 2/3 trees
 each internal node has 2 or 3 children
 all leaves at same depth (height balanced)
 B-trees
 Generalization of 2/3 trees
 Each internal node has between k/2 and k children
 Each node has an array of pointers to children
 Widely used in databases
Other (Non-Search) Trees
 Parse trees
 Convert from textual representation to tree
representation
 Textual program to tree
 Used extensively in compilers
 Tree representation of data
 E.g. HTML data can be represented as a tree
 called DOM (Document Object Model) tree
 XML
 Like HTML, but used to represent data
 Tree structured
Parse Trees
 Expressions, programs, etc can be
represented by tree structures
 E.g. Arithmetic Expression Tree
 A-(C/5 * 2) + (D*5 % 4)

+
- %

A * * 4

/ 2 D 5

C 5
XML
 Data Representation
 E.g.
<dependency>
<object>sample1.o</object>
<depends>sample1.cpp</depends>
<depends>sample1.h</depends>
<rule>g++ -c sample1.cpp</rule>
</dependency>
 Tree representation

dependency

object depends depends rule

sample1.o sample1.cpp sample1.h g++ -c …


Applications of tree
 Store hierarchical data, like folder structure, organization structure,
XML/HTML data.
 Binary Search Tree is a tree that allows fast search, insert, delete on a sorted
data. It also allows finding closest item
 Heap is a tree data structure which is implemented using arrays and used to
implement priority queues.
 B-Tree and B+ Tree : They are used to implement indexing in databases.
 Syntax Tree: Used in Compilers.
 K-D Tree: A space partitioning tree used to organize points in K dimensional
space.
 Trie : Used to implement dictionaries with prefix lookup.
 Suffix Tree : For quick pattern searching in a fixed text.
 Spanning Trees and shortest path trees are used in routers and bridges
respectively in computer networks
 As a workflow for compositing digital images for visual effects.

You might also like