SlideShare a Scribd company logo
1 of 84
Trees1
Lecture 5
Tree Data StructureTree Data Structure
Basic Tree Concepts
Trees2
A tree consists of finite set of elements,
called nodes, and a finite set of directed
lines called branches, that connect the
nodes.
The number of branches associated
with a node is the degree of the node.
Trees3
Trees4
Tree
A simple unordered tree; in this diagram, the node
labelled 7 has two children, labelled 2 and 6, and one
parent, labelled 2. The root node, at the top, has no
parent.
A tree is a widely-used data structure that emulates a
hierarchical tree structure with a set of linked nodes.
Basic Tree Concepts
Trees6
When the branch is directed toward the
node, it is in degree branch.
When the branch is directed away from
the node, it is an out degree branch.
The sum of the in degree and out
degree branches is the degree of the
node.
If the tree is not empty, the first node is
called the root.
Basic Tree Concepts
Trees7
The in degree of the root is, by definition,
zero.
With the exception of the root, all of the nodes
in a tree must have an in degree of exactly
one; that is, they may have only one
predecessor.
All nodes in the tree can have zero, one, or
more branches leaving them; that is, they may
have out degree of zero, one, or more.
Trees8
Basic Tree Concepts
Trees9
A leaf is any node with an out degree of
zero, that is, a node with no successors.
A node that is not a root or a leaf is
known as an internal node.
A node is a parent if it has successor
nodes; that is, if it has out degree greater
than zero.
A node with a predecessor is called a
child.
Basic Tree Concepts
Trees10
Two or more nodes with the same parents
are called siblings.
An ancestor is any node in the path from
the root to the node.
A descendant is any node in the path
below the parent node; that is, all nodes
in the paths from a given node to a leaf are
descendants of that node.
Basic Tree Concepts
Trees11
A path is a sequence of nodes in which
each node is adjacent to the next node.
The level of a node is its distance from
the root. The root is at level 0, its children
are at level 1, etc. …
Basic Tree Concepts
Trees12
The height of the tree is the level of the
leaf in the longest path from the root plus
1. By definition the height of any empty
tree is -1.
A sub tree is any connected structure
below the root.
The first node in the sub tree is known as
the root of the sub tree.
Trees13
Trees14
Recursive definition of a tree
Trees15
A tree is a set of nodes that either:
is empty or
has a designated node, called the
root, from which hierarchically descend
zero or more sub trees, which are also
trees.
Tree Representation
Trees16
General Tree – organization chart
format
Indented list – bill-of-materials system
in which a parts list represents the
assembly structure of an item
Trees17
Trees18
Parenthetical Listing
Trees19
Parenthetical Listing – the algebraic
expression, where each open
parenthesis indicates the start of a new
level and each closing parenthesis
completes the current level and moves
up one level in the tree.
Parenthetical Listing
Trees20
A (B (C D) E F (G H I) )
Trees21
Binary Trees
A binary tree can have no more than twoA binary tree can have no more than two
descendentsdescendents
• Properties
• Binary Tree Traversals
• Expression Trees
• Huffman Code
Binary Trees
Trees22
A binary tree is a tree in which no node
can have more than two sub trees; the
maximum out degree for a node is two.
In other words, a node can have zero,
one, or two sub trees.
These sub trees are designated as the
left sub tree and the right sub tree.
Trees23
Trees24
A null
tree is
a tree
with no
nodes
Some Properties of Binary Trees
The height of binary trees can be mathematically
predicted
Given that we need to store N nodes in a binary
tree, the maximum height is
maxH N=
Trees25
A tree with a maximum height is rare. It
occurs when all of the nodes in the entire
tree have only one successor.
Some Properties of Binary Trees
The minimum height of a binary tree is determined
as follows:
[ ]min 2log 1H N= +
Trees26
For instance, if there are three nodes to be
stored in the binary tree (N=3) then Hmin=2.
Some Properties of Binary Trees
Given a height of the binary tree, H, the
minimum number of nodes in the tree is
given as follows:
minN H=
Trees27
Some Properties of Binary Trees
The formula for the maximum number of nodes is
derived from the fact that each node can have
only two descendents.
 Given a height of the binary tree, H, the
maximum number of nodes in the tree is given
as follows:
max 2 1H
N = −
Trees28
Some Properties of Binary Trees
The children of any node in a tree can be accessed by
following only one branch path, the one that leads to the
desired node.
The nodes at level 1, which are children of the root, can be
accessed by following only one branch; the nodes of level
2 of a tree can be accessed by following only two branches
from the root, etc.
The balance factor of a binary tree is the difference in
height between its left and right sub trees:
L RB H H= −Trees29
Trees30
B=0 B=0 B=1 B=-1
B=0 B=1
B=-2 B=2
Balance of
the tree
Some Properties of Binary Trees
In the balanced binary tree (the height of its sub trees
differs by no more than one) its balance factor is -1, 0, or
1), and its sub trees are also balanced.
Trees31
Complete and nearly complete binary
trees
Trees32
A complete tree has the maximum
number of entries for its height. The
maximum number is reached when the last
level is full.
A tree is considered nearly complete if it
has the minimum height for its nodes and
all nodes in the last level are found on the
left
Trees33
Implementing Binary Trees
Just like other ADTs,
we can implement a binary tree using
pointers or arrays.
A pointer based implementation
Implementing Binary Trees
Root
RootData Value
Left Right
Data Value
Left Right
Data Value
Left Right
etc.
Traversal through BSTs
Remember that a binary tree is either empty or
it is in the form of a Root with two sub trees.
If the Root is empty, then the traversal
algorithm should take no action (i.e., this is
an empty tree -- a "degenerate" case).
If the Root is not empty, then we need to
print the information in the root node and start
traversing the left and right sub trees.
When a sub tree is empty, then we stop
traversing it.
Traversal through BSTs
The recursive traversal algorithm is:
Traverse (Root)
If the Tree is not empty then
Visit the node at the Root
Traverse(Left sub tree)
Traverse(Right sub tree)
When traversing any binary tree, the algorithm should
have 3 choices of when to process the root:
before it traverses both sub trees,
after it traverses the left sub tree,
or after it traverses both sub trees.
Each of these traversal methods has a name: preorder, in
order, post order
Binary Tree Traversal
Trees38
A binary tree traversal requires that each
node of the tree be processed once and
only once in a predetermined sequence.
In the depth-first traversal processing
process along a path from the root through
one child to the most distant descendant of
that first child before processing a second
child.
Trees39
Trees40
Trees41
Trees42
Trees43
….Pre order Traversal
It would traverse the following tree as:
60,20,10,5,15,40,30,70,65,85
60
20
70
10
5 15 30
40 65 85
Trees45
Trees46
…In order traversal
It would traverse the same tree as:
5,10,15,20,30,40,60,65,70,85;
Notice that this type of traversal produces the numbers in
order.
Search trees can be set up so that all of the nodes in the
left sub tree are less than the nodes in the right sub tree.
60
20
70
10
5 15 30
40 65 85
Trees48
Trees49
…Post order traversal
 It would traverse the same tree as:
5, 15, 10,30,40,20,65,85,70,60
Tree Representations
There are many different ways to represent trees.
Common representations represent the nodes as records
allocated on the heap with pointers to their children, their
parents, or both, or as items in an array, with
relationships between them determined by their positions
in the array (e.g., binary heap).
Trees and Graphs
The tree data structure can be generalized to represent
directed graphs by removing the constraints that a node
may have at most one parent, and that no cycles are
allowed.
Edges are still abstractly considered as pairs of nodes,
however, the terms parent and child are usually replaced
by different terminology (for example, source and target).
Relationship with Trees in Graph Theory
• In graph theory, a tree is a connected acyclic graph; unless
stated otherwise, trees and graphs are undirected.
• There is no one-to-one correspondence between such
trees and trees as data structure.
• We can take an arbitrary undirected tree, arbitrarily pick
one of its vertices as the root, make all its edges directed
by making them point away from the root node - producing
an arborescence and assign an order to all the nodes.
The result corresponds to a tree data structure.
Picking a different root or different ordering produces a
different one.
• Enumerating all the items
• Enumerating a section of a tree
• Searching for an item
• Adding a new item at a certain position on the tree
• Deleting an item
• Removing a whole section of a tree is called pruning
• Adding a whole section to a tree is called grafting
• Finding the root for any node
Common Operations
Common Uses
Manipulate hierarchical data
Make information easy to search
Manipulate sorted lists of data
As a workflow for composting digital images for
visual effects
Router algorithms
Search Trees
A binary search tree can be created so that the
elements in it satisfy an ordering property.
This allows elements to be searched for quickly.
All of the elements in the left sub-tree are less than the
element at the root which is less than all of the
elements in the right sub-tree and this property applies
recursively to all the sub-trees.
The great advantage of this is that when searching for
an element, a comparison with the root will either find
the element or indicate which one sub-tree to search.
The ordering is an invariant property of the search
tree. All routines that operate on the tree can make
use of it provided that they also keep it holding true.
Additional…Binary Search Trees
Key property
Value at node
Smaller values in left sub-tree
Larger values in right sub-tree
Example
X > Y
X < Z
Y
X
Z
Binary Search Trees
Examples
Binary search
trees
Not a binary
search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Example Binary Searches
Find ( root, 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
root
Example Binary Searches
Find (root, 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
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
binary tree
Balanced
binary tree
Complete
binary tree
Binary Trees Properties
Degenerate
Height = O(n) for n
nodes
Similar to linked list
Balanced
Height = O( log(n) ) for
n nodes
Useful for searches
Degenerate
binary tree
Balanced
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 sub tree
Larger values in right sub tree
Example Insertion
Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
Example Deletion (Leaf)
Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
Example Deletion (Internal Node)
Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10
with largest
value in left
subtree
Replacing 5
with largest
value in left
subtree
Deleting leaf
Example Deletion (Internal Node)
Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10
with smallest
value in right
subtree
Deleting leaf Resulting tree
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)
Balanced Search Trees
B-trees
Generalization of 2/3 trees
Each node has an array of
pointers to children
Widely used in databases
AVL Tree
AVL (Adelson-Velskii and Landis) tree.
Also called Height Balanced Binary Search Trees
An AVL tree is identical to a BST except
 Height of the left and right sub trees can differ
by at most 1.
 Height of an empty tree is defined to be (–1).
 Every sub tree is an AVL tree.
AVL Tree
An AVL Tree
5
82
4
3
1 7
height
0
1
2
3
AVL Tree
Not an AVL tree
6
81
4
3
1
5
height
0
1
2
3
Example
An example of an AVL tree where the
heights are shown next to the nodes
88
44
17 78
32 50
48 62
2
4
1
1
2
3
1
1
Balanced Binary Tree
The height of a binary tree is the maximum level of
its leaves (also called the depth).
The balance of a node in a binary tree is defined as
the height of its left sub tree minus height of its right
sub tree.
Each node has an indicated balance of 1, 0, or –1.
B-Tree
A B-Tree of order m is an m-way tree, such that:
All leaves are on the same level
All internal nodes except the root are constrained to have
at most non empty children and at least m/2 non empty
children
The root has at most m non empty children
B-Tree of order 2, also known as 2-3-4-tree:
1717 2121
77
11
11
11
88
22
00
22
66
33
11
22 44 55 66 88 99
11
22
11
66
2222 2323 2525 2727 2929 3030 3232 3535
B- Tree
A B-tree is a tree data structure that keeps data sorted
and allows searches, sequential access, insertions, and
deletions in logarithmic time.
The B-tree is a generalization of a binary search tree in
that a node can have more than two children. B-tree is
optimized for systems that read and write large blocks of
data.
It is commonly used in databases and file systems.
...B- Tree
A B-tree is a specialized multi-way tree designed
especially for use on disk. In a B-tree each node may
contain a large number of keys.
The number of sub trees of each node, then, may also be
large.
A B-tree is designed to branch out in this large number of
directions and to contain a lot of keys in each node so
that the height of the tree is relatively small.
This means that only a small number of nodes must be
read from disk to retrieve an item.
The goal is to get fast access to the data, and with disk
drives this means reading a very small number of
records.
B - Tree
For example, the following is a multiway search tree of
order 4. Note that the first row in each node shows the
keys, while the second row shows the pointers to the
child nodes.
There is a record of data associated with each key, so
that the first row in each node might be an array of
records where each record contains a key and its
associated data.
Another approach would be to have the first row of each
node contain an array of records where each record
contains a key and a record number for the associated
data record, which is found in another file.
 This is often used when the data records are large.
.....
....
Records are stored in locations called leaves.
This name derives from the fact that records always exist
at end points; there is nothing beyond them.
The maximum number of children per node is the order of
the tree.
 The number of required disk accesses is the depth.
The image at left shows a binary tree for locating a
particular record in a set of eight leaves.
The image at right shows a B-tree of order three for
locating a particular record in a set of eight leaves (the
ninth leaf is unoccupied, and is called a null).
The binary tree at left has a depth of four; the B-tree at
right has a depth of three.
Clearly, the B-tree allows a desired record to be located
faster, assuming all other system parameters are identical.
A sophisticated program is required to execute the
operations in a B-tree. But this program is stored in RAM,
so it runs fast.

More Related Content

What's hot

trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeShivam Singh
 
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeDifference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeAjay Chimmani
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
Binary search trees
Binary search treesBinary search trees
Binary search treesDwight Sabio
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11sumitbardhan
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Treeleminhvuong
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations varagilavanya
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)Waheed Khalid
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10sumitbardhan
 

What's hot (20)

trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeDifference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary tree
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Binary tree
Binary treeBinary tree
Binary tree
 
binary tree
binary treebinary tree
binary tree
 
Tree
TreeTree
Tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 

Similar to Lecture 5 trees

Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptxRahulAI
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxRajitha Reddy Alugati
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics TreeMasud Parvaze
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsBHARATH KUMAR
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationnakulvarshney371
 
Describe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docxDescribe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docxandyb37
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptxAbirami A
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structuresASairamSairam1
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxfizzaahmed9
 

Similar to Lecture 5 trees (20)

Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Describe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docxDescribe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docx
 
Tree
TreeTree
Tree
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Trees
TreesTrees
Trees
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
Trees
TreesTrees
Trees
 
Ch12 Tree
Ch12 TreeCh12 Tree
Ch12 Tree
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 

More from Victor Palmar

Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Data structures and Alogarithims
Data structures and AlogarithimsData structures and Alogarithims
Data structures and AlogarithimsVictor Palmar
 

More from Victor Palmar (6)

Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Lecture 2c stacks
Lecture 2c stacksLecture 2c stacks
Lecture 2c stacks
 
Lecture 2b lists
Lecture 2b listsLecture 2b lists
Lecture 2b lists
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Data structures and Alogarithims
Data structures and AlogarithimsData structures and Alogarithims
Data structures and Alogarithims
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 

Lecture 5 trees

  • 1. Trees1 Lecture 5 Tree Data StructureTree Data Structure
  • 2. Basic Tree Concepts Trees2 A tree consists of finite set of elements, called nodes, and a finite set of directed lines called branches, that connect the nodes. The number of branches associated with a node is the degree of the node.
  • 5. Tree A simple unordered tree; in this diagram, the node labelled 7 has two children, labelled 2 and 6, and one parent, labelled 2. The root node, at the top, has no parent. A tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes.
  • 6. Basic Tree Concepts Trees6 When the branch is directed toward the node, it is in degree branch. When the branch is directed away from the node, it is an out degree branch. The sum of the in degree and out degree branches is the degree of the node. If the tree is not empty, the first node is called the root.
  • 7. Basic Tree Concepts Trees7 The in degree of the root is, by definition, zero. With the exception of the root, all of the nodes in a tree must have an in degree of exactly one; that is, they may have only one predecessor. All nodes in the tree can have zero, one, or more branches leaving them; that is, they may have out degree of zero, one, or more.
  • 9. Basic Tree Concepts Trees9 A leaf is any node with an out degree of zero, that is, a node with no successors. A node that is not a root or a leaf is known as an internal node. A node is a parent if it has successor nodes; that is, if it has out degree greater than zero. A node with a predecessor is called a child.
  • 10. Basic Tree Concepts Trees10 Two or more nodes with the same parents are called siblings. An ancestor is any node in the path from the root to the node. A descendant is any node in the path below the parent node; that is, all nodes in the paths from a given node to a leaf are descendants of that node.
  • 11. Basic Tree Concepts Trees11 A path is a sequence of nodes in which each node is adjacent to the next node. The level of a node is its distance from the root. The root is at level 0, its children are at level 1, etc. …
  • 12. Basic Tree Concepts Trees12 The height of the tree is the level of the leaf in the longest path from the root plus 1. By definition the height of any empty tree is -1. A sub tree is any connected structure below the root. The first node in the sub tree is known as the root of the sub tree.
  • 15. Recursive definition of a tree Trees15 A tree is a set of nodes that either: is empty or has a designated node, called the root, from which hierarchically descend zero or more sub trees, which are also trees.
  • 16. Tree Representation Trees16 General Tree – organization chart format Indented list – bill-of-materials system in which a parts list represents the assembly structure of an item
  • 19. Parenthetical Listing Trees19 Parenthetical Listing – the algebraic expression, where each open parenthesis indicates the start of a new level and each closing parenthesis completes the current level and moves up one level in the tree.
  • 20. Parenthetical Listing Trees20 A (B (C D) E F (G H I) )
  • 21. Trees21 Binary Trees A binary tree can have no more than twoA binary tree can have no more than two descendentsdescendents • Properties • Binary Tree Traversals • Expression Trees • Huffman Code
  • 22. Binary Trees Trees22 A binary tree is a tree in which no node can have more than two sub trees; the maximum out degree for a node is two. In other words, a node can have zero, one, or two sub trees. These sub trees are designated as the left sub tree and the right sub tree.
  • 24. Trees24 A null tree is a tree with no nodes
  • 25. Some Properties of Binary Trees The height of binary trees can be mathematically predicted Given that we need to store N nodes in a binary tree, the maximum height is maxH N= Trees25 A tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.
  • 26. Some Properties of Binary Trees The minimum height of a binary tree is determined as follows: [ ]min 2log 1H N= + Trees26 For instance, if there are three nodes to be stored in the binary tree (N=3) then Hmin=2.
  • 27. Some Properties of Binary Trees Given a height of the binary tree, H, the minimum number of nodes in the tree is given as follows: minN H= Trees27
  • 28. Some Properties of Binary Trees The formula for the maximum number of nodes is derived from the fact that each node can have only two descendents.  Given a height of the binary tree, H, the maximum number of nodes in the tree is given as follows: max 2 1H N = − Trees28
  • 29. Some Properties of Binary Trees The children of any node in a tree can be accessed by following only one branch path, the one that leads to the desired node. The nodes at level 1, which are children of the root, can be accessed by following only one branch; the nodes of level 2 of a tree can be accessed by following only two branches from the root, etc. The balance factor of a binary tree is the difference in height between its left and right sub trees: L RB H H= −Trees29
  • 30. Trees30 B=0 B=0 B=1 B=-1 B=0 B=1 B=-2 B=2 Balance of the tree
  • 31. Some Properties of Binary Trees In the balanced binary tree (the height of its sub trees differs by no more than one) its balance factor is -1, 0, or 1), and its sub trees are also balanced. Trees31
  • 32. Complete and nearly complete binary trees Trees32 A complete tree has the maximum number of entries for its height. The maximum number is reached when the last level is full. A tree is considered nearly complete if it has the minimum height for its nodes and all nodes in the last level are found on the left
  • 34. Implementing Binary Trees Just like other ADTs, we can implement a binary tree using pointers or arrays. A pointer based implementation
  • 35. Implementing Binary Trees Root RootData Value Left Right Data Value Left Right Data Value Left Right etc.
  • 36. Traversal through BSTs Remember that a binary tree is either empty or it is in the form of a Root with two sub trees. If the Root is empty, then the traversal algorithm should take no action (i.e., this is an empty tree -- a "degenerate" case). If the Root is not empty, then we need to print the information in the root node and start traversing the left and right sub trees. When a sub tree is empty, then we stop traversing it.
  • 37. Traversal through BSTs The recursive traversal algorithm is: Traverse (Root) If the Tree is not empty then Visit the node at the Root Traverse(Left sub tree) Traverse(Right sub tree) When traversing any binary tree, the algorithm should have 3 choices of when to process the root: before it traverses both sub trees, after it traverses the left sub tree, or after it traverses both sub trees. Each of these traversal methods has a name: preorder, in order, post order
  • 38. Binary Tree Traversal Trees38 A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. In the depth-first traversal processing process along a path from the root through one child to the most distant descendant of that first child before processing a second child.
  • 44. ….Pre order Traversal It would traverse the following tree as: 60,20,10,5,15,40,30,70,65,85 60 20 70 10 5 15 30 40 65 85
  • 47. …In order traversal It would traverse the same tree as: 5,10,15,20,30,40,60,65,70,85; Notice that this type of traversal produces the numbers in order. Search trees can be set up so that all of the nodes in the left sub tree are less than the nodes in the right sub tree. 60 20 70 10 5 15 30 40 65 85
  • 50. …Post order traversal  It would traverse the same tree as: 5, 15, 10,30,40,20,65,85,70,60
  • 51. Tree Representations There are many different ways to represent trees. Common representations represent the nodes as records allocated on the heap with pointers to their children, their parents, or both, or as items in an array, with relationships between them determined by their positions in the array (e.g., binary heap).
  • 52. Trees and Graphs The tree data structure can be generalized to represent directed graphs by removing the constraints that a node may have at most one parent, and that no cycles are allowed. Edges are still abstractly considered as pairs of nodes, however, the terms parent and child are usually replaced by different terminology (for example, source and target).
  • 53. Relationship with Trees in Graph Theory • In graph theory, a tree is a connected acyclic graph; unless stated otherwise, trees and graphs are undirected. • There is no one-to-one correspondence between such trees and trees as data structure. • We can take an arbitrary undirected tree, arbitrarily pick one of its vertices as the root, make all its edges directed by making them point away from the root node - producing an arborescence and assign an order to all the nodes. The result corresponds to a tree data structure. Picking a different root or different ordering produces a different one.
  • 54. • Enumerating all the items • Enumerating a section of a tree • Searching for an item • Adding a new item at a certain position on the tree • Deleting an item • Removing a whole section of a tree is called pruning • Adding a whole section to a tree is called grafting • Finding the root for any node Common Operations
  • 55. Common Uses Manipulate hierarchical data Make information easy to search Manipulate sorted lists of data As a workflow for composting digital images for visual effects Router algorithms
  • 56. Search Trees A binary search tree can be created so that the elements in it satisfy an ordering property. This allows elements to be searched for quickly. All of the elements in the left sub-tree are less than the element at the root which is less than all of the elements in the right sub-tree and this property applies recursively to all the sub-trees. The great advantage of this is that when searching for an element, a comparison with the root will either find the element or indicate which one sub-tree to search. The ordering is an invariant property of the search tree. All routines that operate on the tree can make use of it provided that they also keep it holding true.
  • 57. Additional…Binary Search Trees Key property Value at node Smaller values in left sub-tree Larger values in right sub-tree Example X > Y X < Z Y X Z
  • 58. Binary Search Trees Examples Binary search trees Not a binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45
  • 59. Example Binary Searches Find ( root, 2 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found root
  • 60. Example Binary Searches Find (root, 25 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found
  • 61. 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 binary tree Balanced binary tree Complete binary tree
  • 62. Binary Trees Properties Degenerate Height = O(n) for n nodes Similar to linked list Balanced Height = O( log(n) ) for n nodes Useful for searches Degenerate binary tree Balanced binary tree
  • 63. 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
  • 64. Binary Search Tree Construction How to build & maintain binary trees? Insertion Deletion Maintain key property (invariant) Smaller values in left sub tree Larger values in right sub tree
  • 65. Example Insertion Insert ( 20 ) 5 10 30 2 25 45 10 < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 20
  • 66. Example Deletion (Leaf) Delete ( 25 ) 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, delete 5 10 30 2 45
  • 67. Example Deletion (Internal Node) Delete ( 10 ) 5 10 30 2 25 45 5 5 30 2 25 45 2 5 30 2 25 45 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf
  • 68. Example Deletion (Internal Node) Delete ( 10 ) 5 10 30 2 25 45 5 25 30 2 25 45 5 25 30 2 45 Replacing 10 with smallest value in right subtree Deleting leaf Resulting tree
  • 69. 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)
  • 70. Balanced Search Trees B-trees Generalization of 2/3 trees Each node has an array of pointers to children Widely used in databases
  • 71. AVL Tree AVL (Adelson-Velskii and Landis) tree. Also called Height Balanced Binary Search Trees An AVL tree is identical to a BST except  Height of the left and right sub trees can differ by at most 1.  Height of an empty tree is defined to be (–1).  Every sub tree is an AVL tree.
  • 72. AVL Tree An AVL Tree 5 82 4 3 1 7 height 0 1 2 3
  • 73. AVL Tree Not an AVL tree 6 81 4 3 1 5 height 0 1 2 3
  • 75. An example of an AVL tree where the heights are shown next to the nodes 88 44 17 78 32 50 48 62 2 4 1 1 2 3 1 1
  • 76. Balanced Binary Tree The height of a binary tree is the maximum level of its leaves (also called the depth). The balance of a node in a binary tree is defined as the height of its left sub tree minus height of its right sub tree. Each node has an indicated balance of 1, 0, or –1.
  • 77. B-Tree A B-Tree of order m is an m-way tree, such that: All leaves are on the same level All internal nodes except the root are constrained to have at most non empty children and at least m/2 non empty children The root has at most m non empty children
  • 78. B-Tree of order 2, also known as 2-3-4-tree: 1717 2121 77 11 11 11 88 22 00 22 66 33 11 22 44 55 66 88 99 11 22 11 66 2222 2323 2525 2727 2929 3030 3232 3535
  • 79. B- Tree A B-tree is a tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree is a generalization of a binary search tree in that a node can have more than two children. B-tree is optimized for systems that read and write large blocks of data. It is commonly used in databases and file systems.
  • 80. ...B- Tree A B-tree is a specialized multi-way tree designed especially for use on disk. In a B-tree each node may contain a large number of keys. The number of sub trees of each node, then, may also be large. A B-tree is designed to branch out in this large number of directions and to contain a lot of keys in each node so that the height of the tree is relatively small. This means that only a small number of nodes must be read from disk to retrieve an item. The goal is to get fast access to the data, and with disk drives this means reading a very small number of records.
  • 81. B - Tree For example, the following is a multiway search tree of order 4. Note that the first row in each node shows the keys, while the second row shows the pointers to the child nodes. There is a record of data associated with each key, so that the first row in each node might be an array of records where each record contains a key and its associated data. Another approach would be to have the first row of each node contain an array of records where each record contains a key and a record number for the associated data record, which is found in another file.  This is often used when the data records are large.
  • 82. .....
  • 83. .... Records are stored in locations called leaves. This name derives from the fact that records always exist at end points; there is nothing beyond them. The maximum number of children per node is the order of the tree.  The number of required disk accesses is the depth. The image at left shows a binary tree for locating a particular record in a set of eight leaves.
  • 84. The image at right shows a B-tree of order three for locating a particular record in a set of eight leaves (the ninth leaf is unoccupied, and is called a null). The binary tree at left has a depth of four; the B-tree at right has a depth of three. Clearly, the B-tree allows a desired record to be located faster, assuming all other system parameters are identical. A sophisticated program is required to execute the operations in a B-tree. But this program is stored in RAM, so it runs fast.

Editor's Notes

  1. Start of 20 (Nov 29)