Jake - PowerPoint PPT Presentation

Loading...

PPT – Jake PowerPoint presentation | free to download - id: 1ef1b8-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Jake

Description:

Jake. Manager Chef Brad Carol. Waitress Waiter Cook Helper. Joyce Chris Max Len ... less than every key value in its right subtree, and greater than every key value ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 44
Provided by: sylvi164
Learn more at: http://www.cs.uky.edu
Category:
Tags: jake | less | than

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Jake


1
0
Jakes Pizza Shop
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
2
Tree Definition
  • One root node that has no parent
  • Each node can have 0-N children
  • There is a path from each node to the root
  • There is only one unique path from each node to
    the root

3
Binary Tree
  • A binary tree is a structure in which
  • Each node can have at most two children, and
    in which a unique path exists from the root to
    every other node.
  • The two children of a node are called the left
    child and the right child, if they exist.

4
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
5
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
6
A Tree Has Levels
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 0
7
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
8
Level Two
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 2
9
A Subtree
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEFT SUBTREE OF ROOT NODE
10
Another Subtree
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
RIGHT SUBTREE OF ROOT NODE
11
A Binary Tree

V
Q
L
T
A
E
K
S
12
How many leaf nodes?

V
Q
L
T
A
E
K
S
13
How many descendants of Q?

V
Q
L
T
A
E
K
S
14
How many ancestors of K?

15
Implementing a Binary Tree with Pointers and
Dynamic Data

V
Q
L
T
A
E
K
S
16
Each node contains two pointers
struct TreeNode ItemType info
// Data member TreeNode left
// Pointer to left child
TreeNode right // Pointer
to right child
NULL A 6000
. left . info . right
17
// BINARY SEARCH TREE SPECIFICATION class
TreeType public TreeType ( )
// constructor TreeType ( )
// destructor bool IsEmpty ( ) const
bool IsFull ( ) const int
NumberOfNodes ( ) const void InsertItem (
ItemType item ) void DeleteItem
(ItemType item ) void RetrieveItem
( ItemType item, bool found ) void
PrintTree (ofstream outFile) const . .
. private TreeNode root
17
18
TreeType CharBST
Private data root
RetrieveItem
PrintTree . . .
19
A Binary Search Tree (BST) is . . .
  • A special kind of binary tree in which
  • 1. Each node contains a distinct data value,
  • 2. The key values in the tree can be compared
    using greater than and less than, and
  • 3. The key value of each node in the tree is
  • less than every key value in its right subtree,
    and greater than every key value in its left
    subtree.

20
Shape of a binary search tree . . .
  • Depends on its key values and their order of
    insertion.
  • Insert the elements J E F T A
    in that order.
  • The first value to be inserted is put into the
    root node.

21
Inserting E into the BST
  • Thereafter, each value to be inserted begins by
    comparing itself to the value in the root node,
    moving left it is less, or moving right if it is
    greater. This continues at each level until it
    can be inserted as a new leaf.

22
Inserting F into the BST
  • Begin by comparing F to the value in the root
    node, moving left it is less, or moving right if
    it is greater. This continues until it can be
    inserted as a leaf.

23
Inserting T into the BST
  • Begin by comparing T to the value in the root
    node, moving left it is less, or moving right if
    it is greater. This continues until it can be
    inserted as a leaf.

24
Inserting A into the BST
  • Begin by comparing A to the value in the root
    node, moving left it is less, or moving right if
    it is greater. This continues until it can be
    inserted as a leaf.

25
What binary search tree . . .
  • is obtained by inserting
  • the elements A E F J T in
    that order?

26
Binary search tree . . .
  • obtained by inserting
  • the elements A E F J T in
    that order.

27
Another binary search tree

T
E
A
H
M
P
K
Add nodes containing these values in this
order D B L Q S
V Z
28
Is F in the binary search tree?

J
T
E
A
V
M
H
P
29
// BINARY SEARCH TREE SPECIFICATION class
TreeType public TreeType ( )
// constructor TreeType ( )
// destructor bool IsEmpty ( ) const
bool IsFull ( ) const int
NumberOfNodes ( ) const void InsertItem
( ItemType item ) void DeleteItem
(ItemType item ) void
RetrieveItem ( ItemType item , bool found )
void PrintTree (ofstream outFile)
const . . . private
TreeNode root
29
30
// SPECIFICATION (continued) // - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - //
RECURSIVE PARTNERS OF MEMBER FUNCTIONS void
PrintHelper ( TreeNode ptr,
ofstream outFile ) void
InsertHelper ( TreeNode ptr,
ItemType item ) void
RetrieveHelper ( TreeNode ptr,
ItemType item, bool found ) void
DestroyHelper ( TreeNode ptr )

30
31
0
// BINARY SEARCH TREE IMPLEMENTATION // OF
MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS //
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - TreeType TreeType ( ) // constructor
root NULL // - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - bool TreeType
IsEmpty( ) const return ( root
NULL )
31
32
void TreeType RetrieveItem ( ItemType
item, bool found
) RetrieveHelper ( root, item, found )
void RetrieveHelper ( TreeNode ptr,
ItemType item, bool
found) if ( ptr NULL ) found
false else if ( item lt ptr-gtinfo
) // GO LEFT RetrieveHelper( ptr-gtleft ,
item, found ) else if ( item gt
ptr-gtinfo ) // GO RIGHT RetrieveHelper(
ptr-gtright , item, found ) else
item ptr-gtinfo found true

0
32
33
0
void TreeType InsertItem ( ItemType item
) InsertHelper ( root, item ) void
InsertHelper ( TreeNode ptr, ItemType item
) if ( ptr NULL )
// INSERT item HERE AS LEAF ptr new
TreeNode ptr-gtright NULL ptr-gtleft NULL
ptr-gtinfo item else if (
item lt ptr-gtinfo ) // GO LEFT InsertHelper(
ptr-gtleft , item ) else if ( item gt
ptr-gtinfo ) // GO RIGHT InsertHelper(
ptr-gtright , item )
33
34
Inorder Traversal A E H J M T Y
0
Print second
tree

T
E
A
H
M
Y
Print left subtree first
Print right subtree last
35
0
// INORDER TRAVERSAL void TreeType
PrintTree ( ofstream outFile )
const PrintHelper ( root, outFile )
void PrintHelper ( TreeNode ptr,
ofstream outFile ) if ( ptr ! NULL )
PrintHelper( ptr-gtleft , outFile )
// Print left subtree outFile ltlt
ptr-gtinfo PrintHelper( ptr-gtright, outFile
) // Print right subtree
35
36
Preorder Traversal J E A H T M Y
0
Print first
tree

T
E
A
H
M
Y
Print left subtree second
Print right subtree last
37
0
Postorder Traversal A H E M Y T J
Print last
tree

T
E
A
H
M
Y
Print left subtree first
Print right subtree second
38
0
TreeType TreeType ( ) // DESTRUCTOR
DestroyHelper ( root ) void
DestroyHelper ( TreeNode ptr ) // Post All
nodes of the tree pointed to by ptr are
deallocated. if ( ptr ! NULL )
DestroyHelper ( ptr-gtleft )
DestroyHelper ( ptr-gtright ) delete ptr

38
39
Deleting a Node
0
  • Find node (return parent and node)
  • Four Cases
  • Case 1 leaf node
  • Case 2 no left subtree
  • Case 3 no right subtree
  • Case 4 left and right subtrees

40
Case 1 Leaf Node
0
  • Point parent to NULL
  • Delete node

41
Case 2 No Left Subtree
0
  • Point parent to node right subtree
  • Delete node

42
Case 3 No Right Subtree
0
  • Point parent to left subtree
  • Delete node

43
Case 4 Left and Right Subtrees
0
  • Find node with greatest key in left subtree (or
    smallest key in right subtree)
  • Copy greatest (or smallest) key node data to
    delete node data
  • Delete greatest (or smallest) key node
About PowerShow.com