Define a node using pointer - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Define a node using pointer

Description:

In tutorial sessions, we will help you to build up your own BST class step by step. ... We may process them in different batches using files. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 39
Provided by: scie241
Category:

less

Transcript and Presenter's Notes

Title: Define a node using pointer


1
Define 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.

2
Define 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)

3
Define a node using pointer
  • What to learn today
  • concept of binary search tree
  • introduction to file system

4
Binary 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.

5
Binary Trees
root
right
left
internal node 2
internal node 1
right
left
left
right
Leaf 4
Leaf 3
Leaf 1
Leaf 2
6
Binary 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.

7
Subtree rooted at internal node 1
internal node 1
left
right
Leaf 1
Leaf 2
8
Subtree rooted at internal node 2
internal node 2
right
left
Leaf 4
Leaf 3
9
Subtree rooted at leaf 1
Leaf 1
10
Subtree rooted at leaf 2
Leaf 2
11
Binary 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

12
A 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
13
The 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
14
The 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
15
The 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
16
The 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
17
The 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.

18
The inorder of the following subtree
root 1
5
left
right
8
2
Leaf 1
Leaf 2
19
The root
root
9
20
The inorder of the following subtree
Root 2
15
right
left
12
Leaf 4
18
Leaf 3
21
By 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
22
Followed by the root of the subtree
root 1
15
23
Followed 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
24
Its inorder is 12-15-18
root 1
15
left
right
18
12
Leaf 1
Leaf 2
25
Similarly, the inorder of this subtree is 2-5-8
root 1
5
left
right
8
2
Leaf 1
Leaf 2
26
Its 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
27
Theorem 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.
28
Binary 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

30
Introduction to file systems
  • Recommended reference book
  • data management of file structures by Mary E.S.
    Loomis, published by prentice hall.

31
Introduction to file systems
  • File classifications
  • file organizations
  • file operations

32
Why 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.

33
Files as a collection of logically related record
  • They are usually of a single format.
  • They are stored together for some common purpose

34
File 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.

35
File 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.

36
File 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.

37
File operations
  • Creation
  • Update, including
  • record insertion
  • record modification
  • record deletion
  • retrieval, including
  • inquiry
  • report generation
  • maintainance, including
  • restructuring
  • reorganization

38
Tutorials for Lecture 8
  • BST
  • implement TreeNode
  • prototype the class
  • implement constructor
  • implement empty()
Write a Comment
User Comments (0)
About PowerShow.com