Title: Define a node using pointer
1Define a node using pointer
Lecture 8 Binary Search Trees
- NOTICE Tutorial session for Wednesday 1030 am
will be moved to CS Lab 1411. - We will start with binary search trees (BST) now.
- In tutorial sessions, we will help you to build
up your own BST class step by step.
2Define a node using pointer
- What learnt last lecture
- more examples of Node and pointer to Node
- sorted list (insert)
- review of file output (examples given in tutorial
of last week) - file input (examples to be given in tutorial of
this week)
3Define a node using pointer
- What to learn today
- concept of binary search tree
- introduction to file system
4Binary Trees
- A special node called root
- Each node has at most two children, one called
left and another right. It is called parent of
its children. - A node without child is called a leaf.
- A node neither root nor leaf is called an
internal node.
5Binary Trees
root
right
left
internal node 2
internal node 1
right
left
left
right
Leaf 4
Leaf 3
Leaf 1
Leaf 2
6Binary Trees
- Each node induces a subtree rooted at it
- a leaf induces a (trivial) subtree consisting of
itself - an internal node induces a subtree consisting of
itself, its children and its grand children and
so on - the root induces the whole tree.
- A recursive definition
- a leaf induces a (trivial) subtree consisting of
itself - an internal node induces a subtree consisting of
itself and the subtrees induced by its children.
7Subtree rooted at internal node 1
internal node 1
left
right
Leaf 1
Leaf 2
8Subtree rooted at internal node 2
internal node 2
right
left
Leaf 4
Leaf 3
9Subtree rooted at leaf 1
Leaf 1
10Subtree rooted at leaf 2
Leaf 2
11Binary Search Trees
- This tree has a special property
- the value of a node is gt the value of every node
in the subtree rooted at its left child - the value of a node is lt the value of every node
in the subtree rooted at its right child
12A BST
root
9
right
left
internal node 2
internal node 1
15
5
right
left
left
right
12
Leaf 4
18
8
Leaf 3
2
Leaf 1
Leaf 2
13The value 9 of the root is large than that 2, 5,
8 of every node in the subtree rooted at its
left child
left
root
9
internal node 1
5
left
right
8
2
Leaf 1
Leaf 2
14The value 9 of the root is large than that 12,
15, 18 of every node in the subtree rooted at
its right child
root
9
right
internal node 2
15
right
left
12
Leaf 4
18
Leaf 3
15The value 5 of internal node 1 is large than
that 2 every node in the subtree rooted at its
left child
internal node 1
5
left
2
Leaf 1
16The value 5 of internal node 1 is smaller than
that 8 of every node in the subtree rooted at
its right child
internal node 1
5
right
8
Leaf 2
17The INORDER of Binary Search Trees
- Recursive definition
- base case nil if the tree is empty
- recursive case
- the inorder of subtree rooted at left child,
followed by - the root, followed by
- the inorder of subtree rooted at right child.
18The inorder of the following subtree
root 1
5
left
right
8
2
Leaf 1
Leaf 2
19The root
root
9
20The inorder of the following subtree
Root 2
15
right
left
12
Leaf 4
18
Leaf 3
21By definition, the inorder of the previous tree
is the inorder of subtree rooted at its left
child, which is 12 since the left child is a leaf
with no child.
12
Leaf 1
22Followed by the root of the subtree
root 1
15
23Followed by the inorder of the subtree rooted at
its right child which is 18, since the right
child is a leaf with no child.
18
Leaf 2
24Its inorder is 12-15-18
root 1
15
left
right
18
12
Leaf 1
Leaf 2
25Similarly, the inorder of this subtree is 2-5-8
root 1
5
left
right
8
2
Leaf 1
Leaf 2
26Its inorder is 2-5-8-9-12-15-18
root
9
right
left
internal node 2
internal node 1
15
5
right
left
left
right
12
Leaf 4
18
8
Leaf 3
2
Leaf 1
Leaf 2
27Theorem The inorder of any BST is sorted in
increasing order. Proof by induction on the
number of nodes in the tree. Base case zero
node, true, trivially. Now consider a tree of ngt0
nodes. Inductive assumption the inordr of any
BST with less than n nodes is sorted in
increasing order. For our tree of n node, denote
the left child of root by root1 and the root of
its right child by root2. All the nodes in the
subtree rooted at root1 is smaller than or equal
to root. The inorder of this subtree is sorted in
increasing order. All the nodes in the subtree
rooted at root2 is larger than or equal to root.
The inorder of this subtree is sorted in
increasing order. The inorder of the BST is the
inorder of the subtree rooted at root1, followed
by root, followed by the inorder of the subtree
rooted at root2. Therefore the outcome is a
sorted order of the original BST.
28Binary Search Trees
- Similar to linked list, the major operations are
- insert add a new item
- search check whether an item is in the list
- delete delete an item in the list.
- Its implementation is based on tree nodes similar
to the node for queue and stack but with two
pointers to two children left and right.
29- template ltclass Tgt class BST
- public
- BST () //initialize the empty list
- BST () //destructor
- TreeNodeltTgt search(T a_item) //return the
pointer to the item if //it is in the BST, NULL
otherwise. - void insert (T new_item) //add an item to BST
- T delete(T old_item) //remove old_item from BST
- int empty() const // return TRUE if BST is
empty - friend ofstreamoperatorltlt(ofstream out, BSTltTgt
a_tree) - //overloading operator ltlt usually in inorder
- private TreeNodeltTgt root
-
30Introduction to file systems
- Recommended reference book
- data management of file structures by Mary E.S.
Loomis, published by prentice hall.
31Introduction to file systems
- File classifications
- file organizations
- file operations
32Why do we need files for data management?
- Data stored in data structures as we did before
reside in the main memory. They are gone after
the execution of the program is completed. - Data stored in files are in the hard disk. They
are there for us to use later. - Main memory may not have enough space for a large
set of data. We may process them in different
batches using files. - Even when main memory is large, some programs may
only access a small portion of data, making it
more efficient to store other data in files.
33Files as a collection of logically related record
- They are usually of a single format.
- They are stored together for some common purpose
34File classifications
- Master file relatively permanent status data.
- Transaction file changes to be made on mater
file. - Report file data that formatted for presentation
to a user. - Work file temporary file (e.g., data used by a
program). - Program file programs to process data.
- Text file data input formatted using text
editor.
35File access mode
- Input read only files
- ifstream objectname1
- output write only files
- ofstream objectname2
- input/output both read from and written to are
allowed during the execution of a program. The
declaration is as follows - fstream objectname3
- NOTE more details will be discussed later.
36File organizations
- Techniques that are used to represent and store
records on a file. - Sequential
- relative
- indexed sequential
- multi-key
- major differences in organization techniques
- physical ordering of records in storage
- the set of operations necessary to find
particular records.
37File operations
- Creation
- Update, including
- record insertion
- record modification
- record deletion
- retrieval, including
- inquiry
- report generation
- maintainance, including
- restructuring
- reorganization
38Tutorials for Lecture 8
- BST
- implement TreeNode
- prototype the class
- implement constructor
- implement empty()