Binary Search Trees - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Binary Search Trees

Description:

Replace each missing child in the binary tree with a node having ... While x NILL do. y = x. if key[z] key[x] then x = left[x] else x = right[x] p[z] = y ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 27
Provided by: marily199
Category:
Tags: binary | bst | nill | search | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Lecture 12
Topics
  • Binary Search Trees

Reference Introduction to Algorithm by
Cormen Chapter 13 Binary Search Trees
2
Trees
root
Degree?
parent
Depth/Level?
Height?
3
Binary Tree Representation
  • Node
  • data
  • left child
  • right child
  • parent (optional)

4
Full Binary Tree
  • Full Binary Tree
  • Replace each missing child in the binary tree
    with a node having no children (black nodes)
    which are called leaf nodes.
  • Each node is either a leaf or has degree exactly
    2
  • All leaves are at level h (height)
  • All interior nodes are full

3
7
2
4
5
1
6
Binary Tree
Full Binary Tree
5
Complete Binary Tree
  • Complete Binary Tree
  • All leaves have the same depth
  • All interior nodes have degree 2

depth 0
depth 1
height3
depth 2
depth 3
6
Complete Binary Tree
  • The number of internal nodes of a complete binary
    tree of height h is
  • 1 21 22 2h-1 2h -1/2-1
  • 2h -1
  • What is the number of total nodes in a complete
    binary tree of height h?
  • What is the number of leaf nodes in a complete
    binary tree of height h?

7
Applications - Expression Trees
  • To represent infix expressions

(53)(8-4)
8
Applications - Parse Trees
  • Used in compilers to check syntax

statement
if
cond
then
statement
else
statement
statement
if
cond
then
9
Binary Search Trees
  • Binary Search Trees (BSTs) are an important data
    structure for dynamic sets
  • In addition to data, elements have
  • key an identifying field inducing a total
    ordering
  • left pointer to a left child (may be NULL)
  • right pointer to a right child (may be NULL)
  • p pointer to a parent node (NULL for root)

10
Binary Search Trees
  • BST property keyleftSubtree(x) ? keyx ?
    keyrightSubtree(x)
  • Example

11
Traversals for a Binary Tree
  • Pre order
  • visit the node
  • go left
  • go right
  • In order
  • go left
  • visit the node
  • go right
  • Post order
  • go left
  • go right
  • visit the node

12
Traversal Examples
Pre order
A B D G H C E F I
In order
G D H B A E C F I
Post order
G H D B E I F C A
13
Traversal Implementation
  • recursive implementation of preorder
  • pre-order ( node )
  • visit node
  • pre-order ( node.left )
  • pre-order ( node.right )
  • What changes need to be made for in-order,
    post-order?

14
Inorder Tree Walk
  • in-order(x)
  • in-order (x.left)
  • print(x)
  • in-order(x.right)
  • Prints elements in sorted (increasing) order

15
Postorder Tree Walk
  • post-order(x)
  • print(x)
  • post-order (x.left)
  • post-order(x.right)

16
Evaluating an expression tree
  • Walk the tree in postorder
  • When visiting a node, use the results
  • of its children to evaluate it.

17
BST Operations
  • Search
  • Key
  • Minimum
  • Maximum
  • Successor
  • Predecessor
  • Insert node
  • Delete node

18
BST Search Key
  • Search for D and C

19
BST Search Key
  • Given a key and a pointer to a node, returns an
    element with that key or NULL
  • TreeSearch(x, k)
  • if (x NULL or k keyx)
  • return x
  • if (k lt keyx)
  • return TreeSearch(leftx, k)
  • else
  • return TreeSearch(rightx, k)

Cost O(lg2n)
Where height, hlg2n
20
BST Search Minimum
  • TreeMinimum(x)
  • While leftx ltgt NIL do
  • x leftx
  • return x

21
BST Search Maximum
  • TreeMaximum(x)
  • While rightx ltgt NIL do
  • x rightx
  • return x

22
BST Search Successor
  • Case1x has a right subtree
  • successor is minimum node in right subtree

Case2 x has no right subtree successor is first
ancestor of x whose left child is also ancestor
of x
  • Inorder traversal 2 3 4 6 7 9 13 15 17 18 20

23
BST Search Successor
  • if rightx ltgt NIL then
  • return TreeMinimum (rightx)
  • y px
  • while y ltgt NIL and x righty do
  • x y
  • y py
  • return y
  • Predecessor similar algorithm

24
BST Insert Node
  • Example Insert C

C
25
BST Insert Node
  • Adds an element x to the tree so that the binary
    search tree property continues to hold
  • TreeInsert (T, z)
  • y NIL
  • x rootT
  • While x ltgt NILL do
  • y x
  • if keyz lt keyx then x leftx
  • else x rightx
  • pz y
  • if y NIL then rootT z
  • else if keyz ltkeyy then lefty z
  • else
    righty z

Cost O(lg2n)
Where height, hlg2n
26
BST Delete Node
  • Deletion is a bit tricky
  • 3 cases
  • x has no children
  • Remove x
  • x has one child
  • Splice out x
  • x has two children
  • Swap x with successor
  • Perform case 1 or 2 to delete it
Write a Comment
User Comments (0)
About PowerShow.com