Binary Search Trees - PowerPoint PPT Presentation

1 / 95
About This Presentation
Title:

Binary Search Trees

Description:

Binary Search Trees CISC2200, Fall 09 * Recursive solutions so far. Now, let s try to solve some of the problem iteratively . Recursive solutions so far. – PowerPoint PPT presentation

Number of Views:892
Avg rating:5.0/5.0
Slides: 96
Provided by: MEDIA152
Category:
Tags: binary | search | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Binary Search Trees
  • CISC2200, Fall 09

2
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
Jakes Pizza Shop
3
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
4
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
5
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 0
6
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
7
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEVEL 2
8
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
LEFT SUBTREE OF ROOT NODE
9
Trees
Owner Jake
Manager Chef
Brad Carol Waitress
Waiter Cook
Helper Joyce
Chris
Max Len
RIGHT SUBTREE OF ROOT NODE
10
Binary Trees
A structure with i) a unique starting node (the
root), in which ii) each node has up to two child
nodes and iii) a unique path exists from the root
to every other node Root top node of a tree
structure a node with no parent Leaf Node
A tree node that has no children
11
Trees level and height
  • Level distance of a node from the root
  • Height the maximum level

12
Trees
Why is this not a tree?
A tree is a structure with i) a unique starting
node (the root), in which ii) each node is
capable of having two child nodes and iii) a
unique path exists from the root to every other
node
13
Descendants
Descendant of a node is a child of the node, and
any child of the children, etc.
V
Q
L
T
A
E
K
S
How many descendants does Q have?
14
Ancestors
Ancestor of a node a parent of the node, the
parent of the parent, etc.
V
Q
L
T
A
E
K
S
How many ancestors does S have?
15
Many different shapes of Binary Trees
How many different shape binary trees can be
made from 2 nodes? 4 nodes? 6 nodes?
16
Facts of Binary Tree
  • Max. number of nodes at Nth level 2N.
  • 0th level root node 1
  • 1th level 2
  • Double as level increases by 1
  • Suppose f(n) denote the maximum number of nodes
    at nth level
  • f(0)1
  • f(n)2f(n-1) for ngt1
  • A recursive formula!
  • f(n)2n

17
Facts of Binary Tree
  • Max. total number of nodes in a tree of height N
  • All levels are full, so add the max. number of
    nodes at each level together
  • 2021222N 2N1-1

18
Facts of Binary Tree
  • Given a binary tree with N node, what is the min.
    number of levels?
  • Try to fill in each level
  • The answer is log2N 1
  • Max. number of levels with N nodes ?
  • One node at each level gt degenerate to a linked
    list
  • The answer is N

19
Binary Search Trees (BST)
  • A BST is a binary tree with search property
  • A binary tree in which, for each node
  • key value in the node is greater than the key
    value in its left child and any of the left
    childs descendents (left sub-tree)
  • key value in the node is less than the key value
    in its right child and any of the right childs
    descendents (right sub-tree)

20
Binary Search Trees
Each node is the root of a subtree rooted at the
node
21
Binary Search Tree ADT
  • Application level same as List
  • Logic Level
  • void MakeEmpty()
  • bool IsEmpty()
  • bool IsFull()
  • int GetLength()
  • RetrieveItem(ItemType item, bool found)
  • InsertItem (ItemType item)
  • DeleteItem (ItemType item)
  • Print(ofstream outFile)
  • ResetTree(OrderType order)
  • GetNextItem (ItemType item, OrderType order,bool
    finished)

22
Tree node in BST
Can you define a structure to represent a binary
tree node ?
23
Recursive Count
Lets start by counting the number of nodes in a
tree Size? Base cases(s)? General case(s)?
24
Recursive Count version 1
if (Left(tree) is NULL) AND (Right(tree) is
NULL) return 1 else return Count (Left(tree))
Count(Right(tree)) 1
Apply to these trees
25
Recursive Count version 2
if (left(tree) is NULL) AND (Right(tree) is
NULL) return 1 else if (Left(tree) is
NULL) return Count(Right(tree)) 1 else if
(Right(tree) is NULL) return Count(Left(tree))
1 else return Count(Left(tree))
Count(Right(tree)) 1
Apply to an empty tree
26
Recursive Count version 3
if (tree is NULL) return 0 if
(left(tree) is NULL) AND (Right(tree) is
NULL) return 1 else if (Left(tree) is
NULL) return Count(Right(tree)) 1 else if
(Right(tree) is NULL) return Count(Left(tree))
1 else return Count(Left(tree))
Count(Right(tree)) 1
27
Recursive Count version 4
if (tree is NULL) return 0 else return
Count(Left(tree)) Count(Right(tree)) 1
28
Recursive Count implementation
int TreeTypeGetLength() const return
Count(root) ) int TreeTypeCount(TreeNode
tree) const if (tree NULL) return 0
else return Count(tree-gtleft)
Count(tree-gtright) 1
Why do we need two functions?
29
Recursive Search

J
T
E
A
V
M
H
P
Are D, Q, and N in the tree?
30
Recursive Search
Retrieve(tree, item, found) Size? Base
case(s)? General case(s)?
31
Recursive Search
  • void TreeTypeRetrieve(TreeNode tree,
  • ItemType item, bool found) const
  • if (tree NULL)
  • found false //base case
  • else if (item.ComparedTo(tree-gtinfo) LESS)
  • Retrieve(tree-gtleft, item, found)
  • else if (item.ComparedTo(tree-gtinfo) LARGER)
  • Retrieve(tree-gtright, item, found)
  • else //base case
  • item tree-gtinfo
  • found true

32
Shape of BST
  • Shape depends on the order of item insertion
  • Insert the elements J E F T A
    in that order
  • The first value inserted is always put in the
    root

33
Shape of BST
  • Thereafter, each value to be inserted
  • compares itself to the value in the root node
  • moves left it is less or
  • moves right if it is greater
  • When does the process stop?

34
Shape of BST
  • Trace path to insert F

35
Shape of BST
  • Trace path to insert T

36
Shape of BST
  • Trace path to insert A

37
Shape of BST
  • Now build tree by inserting A E F
    J T in that order

And the moral is?
38
Recursive Insertion
Insert an item into a tree Where does each new
node get inserted?
Insert(tree, item) if (tree is NULL) Get a new
node Set right and left to NULL Set info to
item Else if item is larger than tree.info
Insert(tree-gtright, item) Else
Insert(tree-gtleft, item)
39
Recursive Insertion
40
Recursive Insertion
Insert item 12
41
Recursive Insertion
How must the tree be passed?
42
Recursive Insertion
  • void TreeTypeInsert(TreeNode tree, ItemType
    item)
  • if (tree NULL)
  • // Insertion place found.
  • tree new TreeNode
  • tree-gtright NULL
  • tree-gtleft NULL
  • tree-gtinfo item
  • else if (item.ComparedTo(tree-gtinfo) LESS)
  • Insert(tree-gtleft, item)
  • else
  • Insert(tree-gtright, item)

43
Recursive Deletion
Delete Z
44
Recursive Deletion
Delete R
45
Recursive Deletion
Delete Q
46
Delete an existing item from BST
  • Can you summarize the three deletion cases?
  • Deleting a leaf node.
  • Deleting a node with only one child.
  • Deleting a node with two children.

47
Predecessor
  • Predecessor the element whose key immediately
    precedes (less than) the key of item
  • If the item node has left child, the largest
    element in the left subtree (right-most child)
  • If the item has no left child,

48
Successor
  • Successor the element whose key immediately
    follows (greater than) the key of item
  • If the item node has two children, the smallest
    element in the right subtree (left-most child)
  • If the item node has no child,

49
Recursive Deletion
  • DeleteItem(tree, item)
  • if (Left(tree) is NULL) AND (Right(tree) is NULL)
    // delete Z
  • Set tree to NULL
  • else if Left(tree) is NULL AND (Right(tree)) is
    not NULL, delete R
  • Set tree to Right(tree)
  • else if Right(tree) is NULL AND (Left(tree)) is
    not NULL
  • Set tree to Left(tree)
  • else // delete Q, maintain a binary search
    tree
  • Find predecessor
  • Set Info(tree) to Info(predecessor)
  • Delete predecessor

50
Recursive Deletion
  • TreeTypeDeleteItem (ItemType item)
  • deletes item from the current object (a tree
    object)
  • void Delete( TreeNode tree, ItemType item)
  • deletes item from a tree rooted at tree
  • void DeleteNode( TreeNode tree)
  • deletes node pointed to by tree from a BST
  • void GetPredecessor( TreeNode tree, ItemType
    data)
  • finds datas predecessor the largest item in
    datas left subtree and save the info into data.

51
Recursive Deletion
  • // first, find which node should be deleted.
  • void TreeTypeDelete(TreeNode tree,
  • ItemType item)
  • if (item lt tree-gtinfo)
  • Delete(tree-gtleft, item)
  • else if (item gt tree-gtinfo)
  • Delete(tree-gtright, item)
  • else
  • DeleteNode(tree) // Node found

52
Recursive Deletion
  • void TreeTypeDeleteNode(TreeNode tree)
  • ItemType data
  • TreeNode tempPtr
  • tempPtr tree
  • if ( tree-gtleft NULL)
  • tree tree-gtright
  • delete tempPtr
  • else if (tree-gtright NULL)
  • tree tree-gtleft
  • delete tempPtr
  • else
  • GetPredecessor(tree-gtleft, data)
  • tree-gtinfo data
  • Delete(tree-gtleft, data)

Tracing this function using various examples
53
Find Predecessor
  • void TreeTypeGetPredecessor( TreeNode tree,
  • ItemType data)
  • //the largest item is located in its rightmost
    node.
  • while (tree-gtright ! NULL)
  • tree tree-gtright
  • data tree-gtinfo
  • This function should be named GetLargestItem() as
    it returns the largest item stored in tree rooted
    at tree
  • Why is the code not recursive?
  • Can you write the recursive solution?

54
Recursive Deletion
55
Traversals
  • Tree Traversal visiting all the nodes of a tree
  • Depth-First Traversal, Breadth-First Traversal
  • Depth-First Traversal
  • Inorder Traversal
  • Preorder Traversal
  • Postorder Traversal

56
Inorder Traversal
 
  • Inorder traversal visits the root in between
    visiting the left and right subtrees
  • Inorder traversal only makes sense for binary
    trees.
  • Inorder(tree)
  • if tree is not NULL
  • Inorder(Left(tree))
  • Visit Info(tree)
  • Inorder(Right(tree))

A B C D E F G
57
Preorder Traversal
  • Preorder traversal visits the root first.
  • PreOrder(tree)
  • if tree is not NULL
  • Visit Info(tree)
  • Preorder(Left(tree))
  • Preorder(Right(tree)) D B A C F E G

58
Postorder Traversal
  • Postorder traversal visits the root last.
  • PostOrder(tree)
  • if tree is not NULL
  • Postorder(Left(tree))
  • Postorder(Right(tree))
  • Visit Info(tree)

A C B E G F D
59
Traversals
60
Printing the Tree
Traversal Algorithm Inorder traversal
What is the order of the output?
61
Iterator
  • Recall iterator functions for ADT List?
  • ResetList()
  • GetNextItem()
  • Binary Search Tree ResetTree(), GetNextItem()
  • Provide different traversal order
  • In-order, pre-order, post-order
  • Use a parameter to select which traversal order

62
Iterator Implementation
  • ResetTree generates a queue of node contents in
    the indicated order
  • Add private data members three queues storing
    ItemTypes (tree nodes content)
  • QueType inQue, preQue, postQue
  • GetNextItem returns next node content from the
    appropriate queue

63
Iterator
  • void TreeTypeResetTree(OrderType order)
  • // Calls function to create a queue of the tree
  • // elements in the desired order.
  • switch (order)
  • case PRE_ORDER preQue new QueueType
  • PreOrder(root, preQue)
  • break
  • case IN_ORDER inQue new QueueType
  • InOrder(root, inQue)
  • break
  • case POST_ORDER postQue new QueueType
  • PostOrder(root, postQue)
  • break

Insert the info field of nodes in tree (given by
root) into preQue in pre-order
What if the queue is not empty when ResetTree()
is called ?
64
  • void TreeTypeGetNextItem(ItemType item,
  • OrderType order, bool finished)
  • finished false
  • switch (order)
  • case PRE_ORDER preQue-gtDequeue(item)
  • if (preQue-gtIsEmpty())
  • finished true delete
    preQue
  • preQue NULL
  • break
  • case IN_ORDER inQue-gtDequeue(item)
  • if (inQue-gtIsEmpty())
  • finished true delete
    inQue
  • inQue NULL
  • break
  • case POST_ORDER postQue-gtDequeue(item)
  • if (postQue-gtIsEmpty())
  • finished true delete
    postQue

65
PreOrder() function
  • void PreOrder(TreeNode treeRoot, QueType
    preQue)
  • if (treeRoot!NULL)
  • preQue-gtenqueue (treeRoot-gtinfo)
  • PreOrder (treeRoot-gtleft, preQue)
  • PreQrder (treeRoot-gtright, preQue)

Hint for recursive functions on a binary tree,
making an empty tree case your base case will
lead to a cleaner and simpler code.
66
Printing the Tree
  • PrintTree operation
  • Size?
  • Base case(s)?
  • General case(s)?

67
Printing the Tree
  • void TreeTypePrintTree(TreeNode tree,
    stdofstream outFile)
  • if (tree ! NULL)
  • PrintTree(tree-gtleft, outFile)
  • outFile ltlt tree-gtinfo
  • PrintTree(tree-gtright, outFile)

Does this allow a reconstruction of the tree
(same shaped) based on the output?
68
Lab6 breadth-first traversal of tree
  • Visit the nodes in the order of their layers,
    within same layer, visit nodes from left to
    right
  • For the tree in figure
  • D B F A C E G
  • Printout in this order can be used to reconstruct
    the tree, why?
  • Could make tree printout more readable
  • D
  • / \
  • B F
  • /\ /\
  • A C E G

69
BST Destructor
  • Do we need a destructor?
  • Yes as dynamic allocated memory is used
  • delete all nodes (free memory space taken by
    them).
  • In which order shall we delete the tree nodes?
  • i.e., which Traversal order inorder, preorder,
    postorder?
  • TreeTypeTreeType()
  • DeleteTree(root)
  • root NULL
  • void TreeTypeDeleteTreeType(TreeNode root)
  • if (root!NULL)
  • DeleteTree(root-gtleft)
  • DeleteTree(root-gtright)
  • delete root

69
70
Copy a Tree
  • Copy constructor allow initialization of an
    object by copying from an existing one
  • TreeType newTree(oldTree)
  • Syntax of a copy constructor
  • TreeTypeTreeType(const TreeType
    originalTree)
  • Assignment operator overloaded
  • TreeType newTree
  • newTreeoldTree
  • Syntax of assignment operator
  • Void TreeTypeoperator(const TreeType
    originalTree)
  • Both involves copying a tree
  • Use which traversal order?
  • Preorder Traversal

71
Copying a Tree
  • CopyTree(copy, originalTree)
  • if (originalTree is NULL)
  • Set copy to NULL
  • else
  • Set copy to new node
  • Set Info(copy) to Info(originalTree)
  • CopyTree(Left(copy), Left(originalTree))
  • CopyTree(Right(copy), Right(originalTree))

How must copy be passed? How must originalTree be
passed?
72
Copy a Tree
  • void CopyTree (TreeNode copy, const TreeNode
    originalTree)
  • if (originalTreeNULL)
  • copyNULL
  • else
  • copy new TreeNode
  • copy-gtinfo originalTree-gtinfo
  • CopyTree (copy-gtleft, originalTree-gtleft)
  • CopyTree (copy-gtright, originalTree-gtright)

72
73
Recursive solutions so far.Now, lets try to
solve some of the problem iteratively.
74
Iterative Search
  • FindNode(tree, item, nodePtr, parentPtr)
  • Searching an item
  • If a node is found, get two pointers, one points
    to the node and one points to its parent node.
  • If root is the node matching, the first pointers
    points to the root and the second pointer points
    to NULL.
  • If no node is found, the first pointers points to
    NULL and the second pointers points to its
    logical parent node.

75
Iterative Versions
  • FindNode(tree, item, nodePtr, parentPtr)
  • Set nodePtr to tree
  • Set parentPtr to NULL
  • Set found to false
  •  
  • while more elements to search AND NOT found
  • if item lt Info(nodePtr) // search left subtree
  • Set parentPtr to nodePtr
  • Set nodePtr to Left(nodePtr)
  • else if item gt Info(nodePtr) // search right
    subtree
  • Set parentPtr to nodePtr
  • Set nodePtr to Right(nodePtr)
  • else //match
  • Set found to true

76
  • void TreeTypeFindNode(TreeNode tree, ItemType
    item,
  • TreeNode nodePtr, TreeNode
    parentPtr)
  • nodePtr tree
  • parentPtr NULL
  • bool found false
  • while( nodePtr ! NULL found false)
  • if (item.ComparedTo(nodePtr-gtinfo) LESS)
  • parentPtr nodePtr
  • nodePtr nodePtr-gtleft
  • else if (item.ComparedTo(nodePtr-gtinfo)
    LARGER)
  • parentPtr nodePtr
  • nodePtr nodePtr-gtright
  • else
  • found true

77
Iterative insertion
  • InsertItem
  • Create a node to contain the new item
  • Find the insertion place
  • Attach new node
  • Find the insertion place
  • FindNode(tree, item, nodePtr, parentPtr)
  • If no node is found, the first pointers points to
    NULL and the second pointers points to its
    logical parent node.

78
Iterative Versions
Insert 13
79
Iterative Versions
80
Iterative Versions
AttachNewNode if item lt Info(parentPtr) Set
Left(parentPtr) to newNode else Set
Right(parentPtr) to newNode
81
Iterative Version
  • AttachNewNode(revised)
  • if parentPtr equals NULL
  • Set tree to newNode
  • else if item lt Info(parentPtr)
  • Set Left(parentPtr) to newNode
  • else
  • Set Right(parentPtr) to newNode

82
Iterative Version DeleteItem
  • void TreeTypeDeleteItem(ItemType item)
  • TreeNode nodePtr
  • TreeNode parentPtr
  • FindNode(root, item, nodePtr, parentPtr)
  • if (nodePtr root)
  • DeleteNode(root)
  • else
  • if (parentPtr-gtleft nodePtr)
  • DeleteNode(parentPtr-gtleft)
  • else DeleteNode(parentPtr-gtright)

Why not directly delete nodePtr?
83
Recursive Deletion review
  • void TreeTypeDeleteNode(TreeNode tree)
  • ItemType data
  • TreeNode tempPtr
  • tempPtr tree
  • if ( tree-gtleft NULL)
  • tree tree-gtright
  • delete tempPtr
  • else if (tree-gtright NULL)
  • tree tree-gtleft
  • delete tempPtr
  • else
  • GetPredecessor(tree-gtleft, data)
  • tree-gtinfo data
  • Delete(tree-gtleft, data)

83
84
Iterative Version
parentPtr and nodePtr are external
parentPtr-gtleft is internal (to the tree)
85
Recursion vs. Iteration
Compare versions of Tree algorithms Is the depth
of recursion relatively shallow? Is the
recursive solution shorter or cleaner? Is the
recursive version much less efficient?
86
Nonlinked Representation
What is the mapping into the index?
87
Nonlinked Representation
  • For any node tree.nodesindex
  • its left child is in tree.nodesindex2 1
  • right child is in tree.nodesindex2 2
  • its parent is in tree.nodes(index - 1)/2
  • Can you determine which nodes are leaf nodes?

88
Nonlinked Representation
Full Binary Tree A binary tree in which all of
the leaves are on the same level and every
nonleaf node has two children
89
Nonlinked Representation
Complete Binary Tree A binary tree that is either
full or full through the next-to-last level, with
the leaves on the last level as far to the left
as possible
90
Nonlinked Representation
91
Nonlinked Representation
A technique for storing a non-complete tree
92
Binary Search Trees
The same set of keys may have different BSTs
  • Average depth of a node is O(log2 N)
  • Maximum depth of a node is O(N)

93
Time Complexity
  • Time complexity of Searching
  • O(height of the tree)
  • Time complexity of Inserting
  • O(height of the tree)
  • Time complexity of Deleting
  • O(height of the tree)

94
Comparison
  • Assume that we have a Complete tree.

Binary Search Tree Array-based Linear List Linked List
Constructor O(1) O(1) O(1)
Destructor O(N) O(1) (non-pointers) O(N)
MakeEmpty O(N) O(1) O(N)
GetLength O(N) O(1) O(1)
IsFull O(1) O(1) O(1)
IsEmpty O(1) O(1) O(1)
RetrieveItem O(log2N) O(log2N) O(N)
InsertItem O(log2N) O(N) O(N)
DeleteItem O(log2N) O(N) O(N)
95
Reference
  • Reproduced from C Plus Data Structures, 4th
    edition by Nell Dale.
  • Reproduced by permission of Jones and Bartlett
    Publishers International.
Write a Comment
User Comments (0)
About PowerShow.com