# Binary Search Tree - PowerPoint PPT Presentation

PPT – Binary Search Tree PowerPoint presentation | free to view - id: 73031f-MDVlN The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## Binary Search Tree

Description:

### Chapter 8 Binary Search Tree Fall 2010 * – PowerPoint PPT presentation

Number of Views:147
Avg rating:3.0/5.0
Slides: 54
Provided by: Media211
Category:
Tags:
Transcript and Presenter's Notes

Title: Binary Search Tree

1
• Chapter 8
• Binary Search Tree

Fall 2010
2
Binary Trees
• A structure with
• a unique starting node (the root), in which
• each node has up to two child nodes, and
• 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

3
Trees level and height
• Level distance of a node from the root
• Height the maximum level

4
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 can have
up to two child nodes and iii) a unique path
exists from the root to every other node
5
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?
6
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?
7
Binary Trees Come in Different Shapes
How many structurally different binary trees can
be made from 2 nodes? 4 nodes? 6 nodes?
8
Binary Tree Facts
• Max. number of nodes at Nth level 2N.
• 0th level root node 1
• 1st level 2
• Double as level increases by 1
• Suppose f(n) denotes the maximum number of nodes
at the nth level
• f(0)1
• f(n)2f(n-1) for ngt1
• A recursive formula!
• f(n)2n

9
Binary Tree Facts
• 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

10
Binary Tree Facts
• Given a binary tree with N nodes, 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 degenerates to a linked
list

11
Binary Search Trees (BST)
• A BST is a binary tree with a 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)

12
Binary Search Trees
Each node is the root of a subtree rooted at the
node
13
• 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)

14
Tree node in BST
Can you define a structure to represent a binary
tree node ?
15
Recursive Count
Lets start by counting the number of nodes in a
tree Size? Base cases(s)? General case(s)?
16
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
17
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
18
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
19
Recursive Count version 4
if (tree is NULL) return 0 else return
Count(Left(tree)) Count(Right(tree)) 1
20
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?
21
Recursive Search

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

24
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

25
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?

26
Shape of BST
• Trace path to insert F

27
Shape of BST
• Trace path to insert T

28
Shape of BST
• Trace path to insert A

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

And the moral is?
30
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 and insert at this location Set right and
left to NULL Set info to item else if (item is
larger than tree-gtinfo)
Insert(tree-gtright, item) else
Insert(tree-gtleft, item)
31
Recursive Insertion
32
Recursive Insertion
Insert item 12
33
Recursive Insertion
How must the tree be passed?
34
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 lt tree-gtinfo)
• Insert(tree-gtleft, item)
• else
• Insert(tree-gtright, item)

35
Deleting a Leaf Node
36
Deleting a Node with One Child
37
Deleting a Node with Two Children
38
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.

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

40
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 right child,

41
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

42
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 saves the info into data.

43
Recursive Deletion
• void TreeTypeDeleteItem(ItemType item)
• Delete(root, item)
• //Calls the recursive function Delete to
• //delete item from tree.

43
44
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

45
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
46
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 could be named GetLargestItem() as
it returns the largest item stored in tree rooted
at tree.

47
Recursive Deletion
48
Traversals
• Tree Traversal visiting all the nodes of a tree
• Depth-First Traversal
• Inorder Traversal
• Preorder Traversal
• Postorder Traversal

48
49
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
49
50
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

50
51
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
51
52
Traversals
52
53
Printing the Tree
Traversal Algorithm Inorder traversal
53