Chapter Objectives - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Chapter Objectives

Description:

Chapter Objectives Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Discover how to insert ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 46
Provided by: Tayl120
Category:

less

Transcript and Presenter's Notes

Title: Chapter Objectives


1
Chapter Objectives
  • Learn about binary trees
  • Explore various binary tree traversal algorithms
  • Learn how to organize data in a binary search
    tree
  • Discover how to insert and delete items in a
    binary search tree

2
Introduction
  • Many data structures are linear
  • unique first component
  • unique last component
  • other components have unique predecessor and
    successor
  • hierarchical
  • non-linear
  • each component may have several successors

3
Trees
  • Hierarchy in which each component except top is
    immediately beneath one other component
  • root - single component at the top of a tree
  • leaves - component having no successors
  • nodes - trees components
  • parent - node immediately above(predecessor)
  • children - nodes directly below it(successor)
  • ancestor
  • descendant

4
General tree
  • An empty node is a tree
  • A single node is a tree
  • The structure formed by taking a node R and one
    or more separate trees and making R the parent of
    all roots of the trees is a tree

5
More tree terminology
  • Level of a node
  • level of root is 1
  • level of any other node is one more than its
    parent
  • height or depth of a tree
  • maximum of the levels of its leaves

6
Binary Tree
  • A node in a binary tree can have at most two
    children
  • the two children of a node have special names
    the left child and the right child
  • every node of a binary tree has 0, 1, or 2
    children

7
Binary Trees
  • A binary tree, T, is either empty or
  • T has a special node called the root node
  • T has two sets of nodes, LT and RT, called the
    left subtree and right subtree
  • LT and RT are binary trees
  • A binary tree can be shown pictorially

8
Binary Trees (continued)
Figure 19-1 Binary tree
9
Binary Trees (continued)
Figure 19-6 Various binary trees with three nodes
10
Binary Trees (continued)
  • You can write a class that represents each node
    in a binary tree
  • Called BinaryTreeNode
  • Instance variables of the class BinaryTreeNode
  • info stores the information part of the node
  • lLink points to the root node of the left
    subtree
  • rLink points to the root node of the right
    subtree

11
Binary Trees (continued)
Figure 19-7 UML class diagram of the class
BinaryTreeNode and the
outer-inner class relationship
12
Binary Trees (continued)
Figure 19-8 Binary tree
13
Binary Trees (continued)
  • A leaf is a node in a tree with no children
  • Let U and V be two nodes in a binary tree
  • U is called the parent of V if there is a branch
    from U to V
  • A path from a node X to a node Y is a sequence of
    nodes X0, X1, ..., Xn such that
  • X X0,Xn Y
  • Xi-1 is the parent of Xi for all i 1, 2, ..., n

14
Binary Trees (continued)
  • Length of a path
  • The number of branches on that path
  • Level of a node
  • The number of branches on the path from the root
    to the node
  • Height of a binary tree
  • The number of nodes on the longest path from the
    root to a leaf

15
Binary Trees (continued)
  • Method height
  • private int height(BinaryTreeNodeltTgt p)
  • if (p null)
  • return 0
  • else
  • return 1 Math.max(height(p.lLink),
    height(p.rLink))

16
Clone Tree
  • Method copyTree
  • private BinaryTreeNodeltTgt copyTree
  • (BinaryTreeNodeltTgt
    otherTreeRoot)
  • BinaryTreeNodeltTgt temp
  • if (otherTreeRoot null)
  • temp null
  • else
  • temp (BinaryTreeNodeltTgt)
    otherTreeRoot.clone()
  • temp.lLink copyTree(otherTreeRoot.lLink)
  • temp.rLink copyTree(otherTreeRoot.rLink)
  • return temp
  • //end copyTree

17
Binary Tree Traversal
  • Item insertion, deletion, and lookup operations
    require that the binary tree be traversed
  • Commonly used traversals
  • Inorder traversal
  • Preorder traversal
  • Postorder traversal

18
Inorder Traversal
  • Binary tree is traversed as follows
  • Traverse left subtree
  • Visit node
  • Traverse right subtree

19
Inorder Traversal (continued)
  • Method inOrder
  • private void inorder(BinaryTreeNodeltTgt p)
  • if (p ! null)
  • inorder(p.lLink)
  • System.out.print(p )
  • inorder(p.rLink)

20
Inorder0 3 5 6 7 8 9
5
8
3
7
9
0
6
21
Preorder Traversal
  • Binary tree is traversed as follows
  • Visit node
  • Traverse left subtree
  • Traverse right subtree

22
Preorder Traversal (continued)
  • Method preOrder
  • private void preorder(BinaryTreeNodeltTgt p)
  • if (p ! null)
  • System.out.print(p )
  • preorder(p.lLink)
  • preorder(p.rLink)

23
Preorder 5 3 0 8 7 6 9
5
8
3
7
9
0
6
24
Postorder Traversal
  • Binary tree is traversed as follows
  • Traverse left subtree
  • Traverse right subtree
  • Visit node

25
Postorder Traversal (continued)
  • Method postOrder
  • private void postorder(BinaryTreeNodeltTgt p)
  • if (p ! null)
  • postorder(p.lLink)
  • postorder(p.rLink)
  • System.out.print(p )

26
Postorder 0 3 6 7 9 8 5
5
8
3
7
9
0
6
27
  • Example

28
Implementing Binary Trees
Figure 19-11 UML class diagram of the interface
BinaryTreeADT
29
Implementing Binary Trees (continued)
Figure 19-12 UML class diagram of the class
BinaryTree
30
Binary Search Trees
  • To search for an item in a normal binary tree,
    you must traverse entire tree until item is found
  • Search process will be very slow
  • Similar to searching in an arbitrary linked list
  • Binary search tree
  • Data in each node is
  • Larger than the data in its left child
  • Smaller than the data in its right child

31
Binary Search Tree
  • Definition Binary search tree (BST)
    Specialized binary tree in which the nodes
    contain values that satisfy the search property
    for each node, the data value is greater than any
    data value in its left subtree and less than any
    data value in its right subtree. More properties
    of BST No two nodes in the tree contain the same
    data value (values are unique, no duplicates).
  • The data in the tree should have a data type in
    which less than (lt) or greater than (gt) operators
    are defined.
  • In any subtree, the leftmost node contains the
    min value.
  • In any subtree, the rightmost node contains the
    max value.
  • Degenerate BST every node except the single leaf
    node has exactly one child.
  • Balanced BST most nodes have two children. For
    any node, the number of nodes in the left subtree
    is not much larger or much smaller than the
    number of nodes in the right subtree. (Formal
    definition is more complicated)

32
Binary Search Trees (continued)
Figure 19-14 Binary search tree
33
Binary Search Trees (continued)
Figure 19-15 UML class diagram of the class
BinarySearchTree and the
inheritance hierarchy
34
Search
  • Searches tree for a given item
  • General steps
  • Compare item with info in root node
  • If they are the same, stop the search
  • If item is smaller than info in root node
  • Follow link to left subtree
  • Otherwise
  • Follow link to right subtree

35
  • The methods search/recSearch (recursive
    implementation)
  • public boolean search(T item)
  • return recSearch(root, item)
  • public boolean recSearch(BinaryTreeNodeltTgt
    tree, T item)
  • if(tree null)
  • return false
  • else
  • ComparableltTgt temp (ComparableltTgt)
    tree.info
  • if (temp.compareTo(item) 0)
  • return true //found
  • else if (temp.compareTo(item) gt
    0)
  • return recSearch(tree.lLink,
    item)//go left
  • else
  • return recSearch(tree.rLink,
    item)//go right

36
  • Analysis In the worst case, we may have to
    traverse the whole longest path from root to
    leaves. So, it takes O(h) time, h height of the
    tree.
  • If the tree is unbalanced, this is still O(n). If
    the tree is balanced, h O (log n), where n
    number of nodes in the tree.
  • On average (over all permutations in which n
    distinct elements are inserted) the height is
    O(log n).
  • There are special binary search trees (Red-Black
    trees, AVL trees, and splay trees) that maintain
    the trees balanced. Next semester

37
Insert
  • Inserts a new item into a binary search tree
  • General steps
  • Search tree and find the place where new item is
    to be inserted
  • Search algorithm is similar to method search
  • Insert new item
  • Duplicate items are not allowed

38
Insert a new node in a BST (insert x) p the
parent of the empty subtree at which search
terminates when it seeks for x's location if(BST
is empty) Create a new node p and let the
root point to it Copy x into new node's info
Set the pointers (left, right) in the new
node to NULL. else if(x lt data item of the root)
Insert x into left subtree else Insert
x into right subtree
39
insert/recInsert (recursive implementation)
public void insert(T item) root
recInsert(root, item) public
BinaryTreeNodeltTgt recInsert(BinaryTreeNodeltTgt
tree, T item) if(tree null)
//create new node tree new
BinaryTreeNodeltTgt(item) else
ComparableltTgt temp (ComparableltTgt)
tree.info if (temp.compareTo(item)
0) System.err.print("Already in -
duplicates are not allowed.") return
null else if (temp.compareTo(item) gt
0) tree.lLink recInsert(tree.lLink,
item) else tree.rLink
recInsert(tree.rLink, item) return
tree
40
Delete
  • Deletes item from a binary search tree
  • After deleting items, resulting tree must be a
    binary search tree
  • General steps
  • Search the tree for the item to be deleted
  • Searching algorithm is similar to method search
  • Delete item

41
Delete (continued)
  • Delete operation has four cases
  • Node to be deleted is a leaf
  • Node to be deleted has no left subtree
  • Node to be deleted has no right subtree
  • Node to be deleted has nonempty left and right
    subtrees

42
(No Transcript)
43
The methods delete/recDelete (recursive
implementation) public void delete(T item)
root recDelete(root, item) public
BinaryTreeNodeltTgt recDelete(BinaryTreeNodeltTgt
tree, T item) if(tree null) //empty
tree System.err.println("Cannot delete
from an empty tree.") return null
else ComparableltTgt temp
(ComparableltTgt) tree.info if
(temp.compareTo(item) gt 0)
tree.lLink recDelete(tree.lLink, item)
else if(temp.compareTo(item) lt 0)
tree.rLink recDelete(tree.rLink, item)
else if(tree.lLink ! null tree.rLink !
null) // 2 children tree.info
findMin(tree.rLink).info //tree.info
findMax(tree.lLink).info tree.rLink
removeMin(tree.rLink) else
if(root.lLink ! null) //1 left child
tree tree.lLink else if(root.rLink !
null) //1 right child tree tree.rLink
return tree
44
Binary Search Tree Analysis
  • Performance depends on shape of the tree
  • If tree shape is linear, performance is the same
    as for a linked list
  • Average number of nodes visited
  • 1.39log2n O(log2n)
  • Average number of key comparisons
  • 2.77log2n O(log2n)

45
Efficiency
  • Maximum number of loop iterations equals the
    height of the tree
  • degenerate binary tree - every node except the
    single leaf node has exactly one child
  • linear search
  • full binary tree
  • balanced - most nodes have two children
  • O(log2N)
Write a Comment
User Comments (0)
About PowerShow.com