DSchap10

download DSchap10

of 51

Transcript of DSchap10

  • 8/3/2019 DSchap10

    1/51

    Binary Trees

    1

    Chapter 10

  • 8/3/2019 DSchap10

    2/51

    Chapter Objectives

    Data Structures Using Java2

    y Learn about binary trees

    y Explore various binary tree traversal algorithms

    y Learn how to organize data in a binary search tree

    y Discover how to insert and delete items in a binary searchtree

    y Explore nonrecursive binary tree traversal algorithms

    y Learn about AVL (height-balanced) trees

  • 8/3/2019 DSchap10

    3/51

    Binary Trees

    Data Structures Using Java3

    y Definition: A binary tree, T, is either empty or such that:

    y Thas a special node called the root node;

    y Thas two sets of nodes, LTand RT, called the left subtree and

    right subtree ofT, respectively;

    y LTand RTare binary trees

  • 8/3/2019 DSchap10

    4/51

    Binary Tree

    Data Structures Using Java4

  • 8/3/2019 DSchap10

    5/51

    Binary Tree with One Node

    Data Structures Using Java5

    The root node of the binary tree = A

    LA = empty

    RA = empty

  • 8/3/2019 DSchap10

    6/51

    Binary Tree with Two Nodes

    Data Structures Using Java6

  • 8/3/2019 DSchap10

    7/51

    Binary Tree with Two Nodes

    Data Structures Using Java7

  • 8/3/2019 DSchap10

    8/51

    Various Binary Trees with Three Nodes

    Data Structures Using Java8

  • 8/3/2019 DSchap10

    9/51

    Binary Trees

    Data Structures Using Java9

    Following class defines the node of a binary tree:

    protected class BinaryTreeNode

    {

    DataElement info;BinaryTreeNode llink;

    BinaryTreeNode rlink;

    }

  • 8/3/2019 DSchap10

    10/51

    Nodes

    Data Structures Using Java10

    y For each node:

    y Data is stored in info

    y The reference to the left child is stored in llink

    y

    The reference to the right child is stored in rlink

  • 8/3/2019 DSchap10

    11/51

    General Binary Tree

    Data Structures Using Java11

  • 8/3/2019 DSchap10

    12/51

    Binary Tree Definitions

    Data Structures Using Java12

    y Leaf: node that has no left and right children

    y Parent: node with at least one child node

    y Level of a node: number of branches on the path from root to

    nodey Height of a binary tree: number of nodes on the longest path

    from root to node

  • 8/3/2019 DSchap10

    13/51

    Height of a Binary Tree

    Data Structures Using Java13

    Recursive algorithm to find height of binary

    tree: (height(p) denotes height of binary tree

    with root p):

    if(p is NULL)

    height(p) = 0

    else

    height(p) = 1 + max(height(p.llink),height(p.rlink))

  • 8/3/2019 DSchap10

    14/51

    Height of a Binary Tree

    Data Structures Using Java14

    Method to implement above algorithm:

    private int height(BinaryTreeNode p)

    {if(p == NULL)

    return 0;

    else

    return 1 + max(height(p.llink),

    height(p.rlink));}

  • 8/3/2019 DSchap10

    15/51

    Copy Tree

    Data Structures Using Java15

    y Useful operation on binary trees is to make identical copy of

    binary tree

    y Method copy useful in implementing copy constructor and

    method copyTree

  • 8/3/2019 DSchap10

    16/51

    Method copy

    Data Structures Using Java16

    BinaryTreeNode copy(BinaryTreeNode otherTreeRoot)

    {

    BinaryTreeNode temp;

    if(otherTreeRoot == null)

    temp = null;

    else

    {

    temp = new BinaryTreeNode();

    temp.info = otherTreeRoot.info.getCopy();

    temp.llink = copy(otherTreeRoot.llink);

    temp.rlink = copy(otherTreeRoot.rlink);

    }

    return temp;

    }//end copy

  • 8/3/2019 DSchap10

    17/51

    Binary Tree Traversal

    Data Structures Using Java17

    y Must start with the root, then

    y Visit the node first

    or

    y

    Visit the subtrees firsty Three different traversals

    y Inorder

    y Preorder

    y

    Postorder

  • 8/3/2019 DSchap10

    18/51

    Traversals

    Data Structures Using Java18

    y Inorder

    y Traverse the left subtree

    y Visit the node

    y Traverse the right subtree

    y Preordery Visit the node

    y Traverse the left subtree

    y Traverse the right subtree

  • 8/3/2019 DSchap10

    19/51

    Traversals

    Data Structures Using Java19

    y Postorder

    y Traverse the left subtree

    y Traverse the right subtree

    y

    Visit the node

  • 8/3/2019 DSchap10

    20/51

    Binary Tree: Inorder Traversal

    Data Structures Using Java20

  • 8/3/2019 DSchap10

    21/51

    Binary Tree: Inorder Traversal

    Data Structures Using Java21

    private void inorder(BinaryTreeNode p)

    {

    if(p != NULL){

    inorder(p.llink);

    System.out.println(p.info + );

    inorder(p.rlink);}

    }

  • 8/3/2019 DSchap10

    22/51

    Binary Tree: Preorder Traversal

    Data Structures Using Java22

    private void preorder(BinaryTreeNode p)

    {

    if(p != NULL){

    System.out.println(p.info + );

    preorder(p.llink);

    preorder(p.rlink);}

    }

  • 8/3/2019 DSchap10

    23/51

    Binary Tree: Postorder Traversal

    Data Structures Using Java23

    private void postorder(BinaryTreeNode p)

    {

    if(p != NULL){

    postorder(p.llink);

    postorder(p.rlink);

    System.out.println(p.info + );}

    }

  • 8/3/2019 DSchap10

    24/51

    ImplementingBinary Trees:

    class BinaryTree methods

    Data Structures Using Java24

    y isEmpty

    y inorderTraversal

    ypreorderTraversal

    y postorderTraversal

    y treeHeight

    y treeNodeCount

    y treeLeavesCount

    y destroyTree

    y copyTree

    Copy

    Inorder

    Preorder postorder

    Height

    Max

    nodeCount

    leavesCount

  • 8/3/2019 DSchap10

    25/51

  • 8/3/2019 DSchap10

    26/51

    Binary Search Trees

    Data Structures Using Java26

  • 8/3/2019 DSchap10

    27/51

    Operations Performed on Binary

    Search Trees

    Data Structures Using Java27

    y Determine whether the binary search tree is empty

    y Search the binary search tree for a particular item

    y Insert an item in the binary search tree

    y Delete an item from the binary search tree

  • 8/3/2019 DSchap10

    28/51

    Operations Performed on Binary

    Search Trees

    Data Structures Using Java28

    y Find the height of the binary search tree

    y Find the number of nodes in the binary search tree

    y Find the number of leaves in the binary search tree

    y Traverse the binary search tree

    y Copy the binary search tree

  • 8/3/2019 DSchap10

    29/51

    Nonrecursive Inorder Traversal

    Data Structures Using Java29

  • 8/3/2019 DSchap10

    30/51

    Nonrecursive Inorder Traversal:

    General Algorithm

    Data Structures Using Java30

    1. current = root; //start traversing the binary tree at

    // the root node

    2. while(current is not NULL or stack is nonempty)

    if(current is not NULL)

    {

    push current onto stack;

    current = current.llink;

    }

    else

    {

    pop stack into current;

    visit current; //visit the node

    current = current.rlink; //move to the right child}

  • 8/3/2019 DSchap10

    31/51

    Nonrecursive Preorder Traversal

    General Algorithm

    Data Structures Using Java31

    1. current = root; //start the traversal at the root node

    2. while(current is not NULL or stack is nonempty)

    if(current is not NULL)

    {

    visit current;

    push current onto stack;

    current = current.llink;

    }

    else

    {

    pop stack into current;

    current = current.rlink; //prepare to visit

    //the right subtree}

  • 8/3/2019 DSchap10

    32/51

    Nonrecursive Postorder Traversal

    Data Structures Using Java32

    1. current = root; //start traversal at root node

    2. v = 0;

    3. if(current is NULL)

    the binary tree is empty

    4. if(current is not NULL)

    a. push current into stack;

    b. push 1 onto stack;

    c. current = current.llink;

    d. while(stack is not empty)

    if(current is not NULL and v is 0)

    {

    push current and 1 onto stack;

    current = current.llink;

    }

  • 8/3/2019 DSchap10

    33/51

    Nonrecursive Postorder Traversal

    (Continued)

    Data Structures Using Java33

    else

    {

    pop stack into current and v;

    if(v == 1){

    push current and 2 onto stack;

    current = current.rlink;

    v = 0;

    }

    elsevisit current;

    }

  • 8/3/2019 DSchap10

    34/51

    Perfect-Balanced Trees)

    Data Structures Using Java34

    y A perfectly balanced binary tree is a binary tree such that:

    y The height of the left and right subtrees of the root are equal

    y The left and right subtrees of the root are perfectly balanced

    binary trees

  • 8/3/2019 DSchap10

    35/51

    Perfectly Balanced Binary Tree

    Data Structures Using Java35

  • 8/3/2019 DSchap10

    36/51

    AVL (Height-Balanced Trees)

    Data Structures Using Java36

    y An AVL tree (or height-balanced tree) is a binary search tree

    such that:

    y The height of the left and right subtrees of the root differ by at

    most 1

    y The left and right subtrees of the root are AVL trees

  • 8/3/2019 DSchap10

    37/51

    AVL Trees

    Data Structures Using Java37

  • 8/3/2019 DSchap10

    38/51

    Non-AVL Trees

    Data Structures Using Java38

  • 8/3/2019 DSchap10

    39/51

    Balance Factor (bf)

    Data Structures Using Java39

    y The balance factor of x, bf(x) is defined as:

    bf(x) = xr xl

    if x is left high, bf(x) = -1if x is equal high, bf(x) = 0

    if x is right high, bf(x) = 1

    y x violates the balance criteria if |xr xl| > 1

  • 8/3/2019 DSchap10

    40/51

    Insertion Into AVL Tree

    Data Structures Using Java40

  • 8/3/2019 DSchap10

    41/51

    Insertion Into AVL Trees

    Data Structures Using Java41

  • 8/3/2019 DSchap10

    42/51

    Insertion Into AVL Trees

    Data Structures Using Java42

  • 8/3/2019 DSchap10

    43/51

    Insertion Into AVL Trees

    Data Structures Using Java43

  • 8/3/2019 DSchap10

    44/51

    Insertion Into AVL Trees

    Data Structures Using Java44

  • 8/3/2019 DSchap10

    45/51

    AVL Tree Rotations

    Data Structures Using Java45

    y Reconstruction procedure: rotating tree

    y left rotation and right rotation

    y Suppose that the rotation occurs at node x

    y Left rotation: certain nodes from the right subtree ofxmove to its

    left subtree; the root of the right subtree ofxbecomes the new

    root of the reconstructed subtree

    y Right rotation at x: certain nodes from the left subtree ofxmove

    to its right subtree; the root of the left subtree ofxbecomes the

    new root of the reconstructed subtree

  • 8/3/2019 DSchap10

    46/51

    AVL Tree Rotations

    Data Structures Using Java46

  • 8/3/2019 DSchap10

    47/51

    AVL Tree Rotations

    Data Structures Using Java47

  • 8/3/2019 DSchap10

    48/51

    AVL Tree Rotations

    Data Structures Using Java48

  • 8/3/2019 DSchap10

    49/51

    AVL Tree Rotations

    Data Structures Using Java49

  • 8/3/2019 DSchap10

    50/51

    AVL Tree Rotations

    Data Structures Using Java50

  • 8/3/2019 DSchap10

    51/51