0% found this document useful (0 votes)
6 views

Slot12 13 Trees Part2 BinaryTrees

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Slot12 13 Trees Part2 BinaryTrees

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Trees-Part 2

and Binary Search Trees

Trees, Part 2: BST Trees 1


Learning outcomes of this part
LO4.1 Define general tree, Binary Tree and Binary Search Tree (BST).
LO4.2 Given a BST , draw resulted tree after an insert/ delete
operations
LO4.3 Find the smallest and largest elements, number of nodes in a
tree and its’ height.
LO4.4 Write code to implement features of a binary search tree, such
as insertion, deletion, searching, traversals, counting nodes, height
calculation, rotation (part 3) ...
LO4.5 Derive the time complexities for the above operations on a BST.
LO4.6: Compare a BST over other data structures
LO4.7: Identify applications where a binary search tree will be useful.

Trees, Part 2: BST Trees 2


Contents

What is binary search tree (BST)?


What are BST’s properties?
How to describe a node?
The given demonstration
How to manager a tree?
must be implemented by
When are BSTs used?
yourself and your works
Demo.: BST of integers will be evaluated.
Algorithms on BST:
Add new node
Getting minimum, maximum values
Getting tree’s height
Searching
Deleting
Trees, Part 2: BST Trees 3
Binary Search Tree: Introduction
Binary Search tree is an orderly binary tree whose nodes are
designated at exactly positions based on pre-defined
comparisonal function.

Common used order:


Data in Left child node < data in father Node < data in Right child node
- LNR order in briefly.

Examples of binary search trees


Trees, Part 2: BST Trees 4
BST: Introduction …

Natural orders:
On numbers: numerical comparison
On characters, strings: Dictionary order  ASCII
comparison

Examples of binary search trees

Trees, Part 2: BST Trees 5


BST: Introduction …

Order of User-defined Objects:


Programmer must define a comparison method. This
method must return an integer.
In Java, the default comparison is declared in the interface
java.lang.Comparable, method:
int compareTo(Object obj)
In Java, we can also use the interface Comparator, method:
int Compare (Object obj1, Object obj2)

Trees, Part 2: BST Trees 6


BST’s Properties
5
Minimum  Maximum
value is put at 2 8 values is put
the leftmost at the
position of the rightmost
1 3 6 9
tree. position of the
tree.
4 7 10

Searching operation will not need to traverse all nodes but a path will be
chosen ( searching x= 7)
Complexity of these operations are improved.
A BST is recommended to use when managing a collection of items in which search
operations are frequently used Search operation must be improved  All elements
are stored in pre-defined order.

Trees, Part 2: BST Trees 7


BST: Node and Tree Structures
class BinTreeNode{
Object data;
BinTreeNode left;
BinTreeNode right;
BinTreeNode father; // additive data
} Implementation view: More memory
accepted, higher performance gained
in operations

class BST {
BinTreeNode root = null;
}

Trees, Part 2: BST Trees 8


Demo.: BST of Integers
Program structure:

Balanced BST of
Integers

BST of Integers

Trees, Part 2: BST Trees 9


Demo.: BST of Integers

Program
User
Interface:

Trees, Part 2: BST Trees 10


Demo.: BST of Integers

Trees, Part 2: BST Trees 11


Demo.: BST of Integers

12
Trees, Part 2: BST Trees
Algorithms on BSTs

Managing, Initializing
Add new node
Get minimum, maximum values
Calculate tree’s height
Traverse
Search
Delete

Trees, Part 2: BST Trees 13


Alg.: Manage, Initialize BST

Trees, Part 2: BST Trees 14


Alg.: Add data (x=36)

Trees, Part 2: BST Trees 15


Alg.: Add data x

The IntBstTree class

Add a
new node

Trees, Part 2: BST Trees 16


Search/ Get minimum/ Get maximum data
Alg.:

The IntBstTree class

Trees, Part 2: BST Trees 17


Alg.: Calculate Tree’s Height

We can
find the
The IntBstTree class
tree’s
height
using a
traversal
only

Trees, Part 2: BST Trees 18


Alg.: Get average value
Summing all keys. The IntBstTree class

Trees, Part 2: BST Trees 19


Alg.: Traversals
The IntBstTree class

Traversal Algorithms:

Case 1: Using a traversal to do an operation on all nodes – For


example, print out all nodes.

Case 2: Using a traversal to create a means to process nodes


one-by-one . In some language libraries, this technique is
usually applied to create an iterator object.

In this implementation, these techniques are introduced.

Trees, Part 2: BST Trees 20


Alg.: Printing nodes based on levels
The IntBstTree class

Print all nodes based on levels – Breadth-first traversal

Trees, Part 2: BST Trees 21


Alg.: Printing nodes in Aligned Format
5
The IntBstTree class
2 8 Print nodes in aligned format.
Using recursive NLR traversal.

1 3 6 9

Trees, Part 2: BST Trees 22


Alg.: Traversing nodes to Linked list
Using depth-first recursive traversals The IntBstTree class

Trees, Part 2: BST Trees 23


Alg.: Creating Iterators
The IntBstTree class

From each
linked-list
result of each
traversal,
create an
iterator, an
object for
processing
nodes, one-by-
one at a time.

Trees, Part 2: BST Trees 24


Alg.: Using Iterators
Alg.:
Print
nodes The IntBstTree class
using
Iterators

For each depth-


first traversal, an
appropriate
iterator is used to
print data in
tree’s nodes

Trees, Part 2: BST Trees 25


Alg.: Deleting
The IntBstTree class
Removing a leaf
delNode = root

father

delNode x

Trees, Part 2: BST Trees 26


Alg.: Deleting
Removing an one-child node The IntBstTree class

delNode = root

x x

grandFather
GF

delNode
x

GC

grandChild
Trees, Part 2: BST Trees 27
Alg.: Deleting
Removing a 2-child node by Merging The IntBstTree class

delNode
grandFather

leftGrandChild rightGrandChild
rightMost
The tree’s height
may increase
(this example)
rightMost

Trees, Part 2: BST Trees 28


Alg.: Deleting

The IntBstTree class

Removing
a 2-child
node by
Merging

Trees, Part 2: BST Trees 29


Alg. :Deleting
The IntBstTree class

Removing the data x from the tree – Public method.


Remove by Merging algorithm is used.

Trees, Part 2: BST Trees 30


Alg. :Removing a node by Merging

Exercise: (1) After the node 1 was deleted, what is father of the node 0?
Remove (2) After the node 4 was deleted, what is child of the node 3?
(3) After the node 5 was deleted what is father of the node 4?
nodes by (4) After the node 14 was deleted, what is father of the node 18?
merging (5) After the node 9 was deleted, what is the tree’s height?

Trees, Part 2: BST Trees 31


Alg. :Removing a node by Merging

Exercise: (1) After the node 9 was deleted, what is father of the node 2?
Remove (2) After the node 9 was deleted, what is child of the node 14?
(3) After the node 6 was deleted what is father of the node 5?
nodes by (4) After the node 11 was deleted, what is father of the node 10?
merging (5) After the node 9 was deleted, what is the tree’s height?

Trees, Part 2: BST Trees 32


Alg.: Deleting
The IntBstTree class

Removing a 2-child node by Copying


delNode Steps
(1) Determine the rightmost
node of the left sub tree of
the deleted node.
(2) Copy data from the rightmost
node to the deleted node.
rightMost (3) Remove the rightmost node.

Advantage
The tree’s height is preserved or
may be reduced because a path
reduces one in it’s length.

Trees, Part 2: BST Trees 33


Alg.: Deleting
Removing a 2-child node by Copying The IntBstTree class

Public method

Trees, Part 2: BST Trees 34


Delete a node by copying

Exercise:
Remove (1) After the node 1 was deleted, what is father of the node 0?
nodes by (2) After the node 4 was deleted, what is child of the node 3?
(3) After the node 5 was deleted what is father of the node 4?
copying (4) After the node 14 was deleted, what is father of the node 18?

Trees, Part 2: BST Trees 35


Delete a node by copying

Exercise: (1) After the node 9 was deleted, what is father of the node 2?
Remove (2) After the node 9 was deleted, what is child of the node 14?
(3) After the node 6 was deleted what is father of the node 5?
nodes by (4) After the node 11 was deleted, what is father of the node 10?
copying (5) After the node 11 was deleted, what is the tree’s height?

Trees, Part 2: BST Trees 36


BST: Test Program

Trees, Part 2: BST Trees 37


BST: Test Program

Trees, Part 2: BST Trees 38


BST: Test Program

Trees, Part 2: BST Trees 39


BST: Test Program

Trees, Part 2: BST Trees 40


BST: Test Program

Trees, Part 2: BST Trees 41


Evaluating BSTs: Advantages

Operations including Adding, searching, removing are


improved with complexity of O(tree’s height), less than
O(number of items).
Sort operation is omitted because data in the tree are
always sorted.
If the tree is a complete binary tree, the tree’s height will be
minimum. In this case, operations on the tree are optimal.

Trees, Part 2: BST Trees 42


Evaluating BSTs: Disadvantages

Tree’s height depends mainly on order of data added. This


situation of complete BST is very hard to occur in normal
problem.
In case of the BST is degraded, almost of data are at one
direction, operations will have complexity of O(number of
nodes)  Hm Hm

Trees, Part 2: BST Trees 43


Evaluating BSTs

Structure Add Search Remove

Array O(1), O(n) O(n)


O(n)
Linked list O(1), O(n) O(n) O(1)

BST O(height) O(height) O(height)

Trees, Part 2: BST Trees 44


Summary: All LOs are met.
 LO4.1 Define general tree, Binary Tree and BST.
 LO4.2 Given a BST , draw resulted tree after an insert/ delete
operations
 LO4.3 Find the smallest and largest elements, number of nodes
in a tree and its’ height.
LO4.4 Write code to implement features of a binary search
 tree, such as insertion, deletion, searching, traversals, nodes
and height calculation, rotation ...
LO4.5 Derive the time complexities for the above operations

on a BST.
 LO4.6: Compare a BST over other data structures
LO4.7: Identify applications where a binary search tree will be

useful.

Trees, Part 2: BST Trees 45


Ôn tập- Viết vào vở
 Hãy vẽ một cây BST tuỳ ý sau đó cho kết quả của các phép duyệt: NLR,
NRL, LNR, RNL, LRN, RLN.
 Thứ tự tự nhiên là gì? Giải thích lý do.
 Để một nút trong cây BST có thể chứa object, class của object này cần phải
được khai báo thế nào?
 Hãy vẽ một cây BST tuỳ ý sau đó giải thích việc thêm một nút mới vào cây
này.
 Hãy viết vào vở 16 thao tác xoá trên 4 bài thí dụ trong slide này và cho kết
quả.
 Các trị sau được đưa vào cây BST trống theo thư tự: 3 5 7 9 2 4 6 8 1. Sau
khi xoá nút 4, nút 3 là con của nút nào?
 Các trị sau được đưa vào cây BST trống theo thư tự. Hỏi số phép so sánh
phải tiến hành khi tìm trị 12. 3 5 7 9 2 4 6 8 1.
 Giả sử nút gốc của cây nhị phân có mức 1. Hỏi số nút tối đa ở mức 10.
 Các trị sau được đưa vào cây BST trống theo thư tự 3 5 7 9 2 4 6 8 1. Hãy
cho biết kết quả của phép duyệt theo chiều rộng.

Trees, Part 2: BST Trees 46


Next part: Balanced BST and
Heap Structure

Trees, Part 2: BST Trees 47

You might also like