02. binary tree

Upload: akif-vohra

Post on 09-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 02. Binary Tree

    1/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 11

    DATASTRUCTURES

    MAHESH GOYANI

    MAHATMAGANDHI INSTITUE OF TECHNICALEDUCATION & RESEARCH CENTER

    [email protected]

  • 8/8/2019 02. Binary Tree

    2/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 22

    BINARY TREE

  • 8/8/2019 02. Binary Tree

    3/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 33

    BINARY TREE

    A binary tree is a finite set of elements that is either empty or ispartitioned into three disjoint subsets.

    The first subset contains a single element called the root of the tree.

    The other two subsets are themselves binary trees called the left and right

    sub trees.

    A Binary Tree With n nodes, n>0 has exactly n-1 edges.

    A Binary tree of depth d, d>0 has at least d and at most2d 1 nodes

    If a binary tree contains n node at level l, than it contains at most 2n

    nodes at level l+1

  • 8/8/2019 02. Binary Tree

    4/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 44

    LEVEL OF BINARY TREE

    Root has level 0,

    Level of any other node is one more than the level of its parent

    A

    B

    D

    H

    C

    E F

    G I

    1

    0

    1

    2 2 2

    3 3 3

    Level 0

    Level 1

    Level 2

    Level 3

  • 8/8/2019 02. Binary Tree

    5/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 55

    Array Representation

    Suppose a binary tree T of depth d then at most 2d-1 nodes can be there in T.So the array of size SIZE is used to represent the binary tree.

    For depth 3,

    SIZE = (23-1)= 7, so A[7] is enough to store the tree

    A

    B

    D

    C

    E G

    0

    1 2

    3 4 5 6

    A B C D E GA[ ]

    [0] [1] [2] [3] [4] [5] [6]

  • 8/8/2019 02. Binary Tree

    6/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 66

    The father of a node having index n can be obtained by (n-1)/2. For example to find thefather of D, Index of D is 3. Then the father node can be obtained

    = (3-1) / 2

    = 2 / 2

    = 1, i.e, A[1] is father of D, that is B.

    The left child of a node having index n can be obtained by (2n + 1). For example to find theleft child of C, index of C = 2

    = (2*2 + 1)

    =5, i.e A[5] is Left child of C, that is NULL

    The Right child of a node having index n can be obtained by (2n + 2). For example to findthe Right child of B, index of B = 1

    = (2*1 + 2)

    =4, i.e A[4] is Left child of C, that is E

    Left and Right brother (Siblings) of node having index n are at location n-1 and n+1respectively.

  • 8/8/2019 02. Binary Tree

    7/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 77

    Linked List Representation

    LChild DATA RChild

    A

    B NULL C

    NULL F NULLNULL E NULLNULL D NULL

  • 8/8/2019 02. Binary Tree

    8/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 88

    Struct node

    {

    };

    ROOT

    LChild RChild

    int ROOT;

    Struct node *LChild;

    Struct node *RChild;

    Node Creation

    ROOTLChild RChild

  • 8/8/2019 02. Binary Tree

    9/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 99

    If a binary tree has only LEFT sub tree, then it is called Left Skewed Binary Tree

    If a binary tree has only RIGHT sub tree, then it is called Right Skewed Binary Tree

    X

    Z

    Y

    A

    C

    BLEFTSKEWED

    RIGHTSKEWED

    A

    B

    D

    H

    C

    E F

    G I

    BINARYTREE

  • 8/8/2019 02. Binary Tree

    10/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1010

    A

    B

    D

    H

    C

    E F

    G I

    Left sub tree

    root

    Right sub tree

    BINARY TREE

  • 8/8/2019 02. Binary Tree

    11/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1111

    BINARY TREE

    Recursive Definition

    B

    D

    H

    C

    E F

    G ILeft sub tree

    root

    Right sub tree

    A

  • 8/8/2019 02. Binary Tree

    12/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1212

    BINARY TREE

    Recursive Definition

    A

    B

    D

    H

    C

    E F

    G I

    Left sub tree

    root

  • 8/8/2019 02. Binary Tree

    13/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1313

    BINARY TREE

    Recursive Definition

    A

    B

    D

    H

    C

    E F

    G I

    root

    Right sub treeLeft sub tree

  • 8/8/2019 02. Binary Tree

    14/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1414

    NOT A TREE

    A

    B

    D

    H

    C

    E F

    G I

  • 8/8/2019 02. Binary Tree

    15/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1515

    A

    B

    D

    H

    C

    E F

    G I

    NOT A TREE

  • 8/8/2019 02. Binary Tree

    16/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1616

    NOT A TREE

    A

    B

    D

    H

    C

    E F

    G I

  • 8/8/2019 02. Binary Tree

    17/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1717

    OPERATION ON BINARY TREE

    There are a number of operations that can be defined for a binary tree.

    If p is pointing to a node in an existing tree then

    left(p) returns pointer to the left subtree

    right(p) returns pointer to right subtree

    parent(p) returns the father of p

    brother(p) returns brother of p.

    info(p) returns content of the node.

    In order to construct a binary tree, the following can be useful:

    setLeft(p,x) creates the left child node of p. The child node contains the info x.

    setRight(p,x) creates the right child node of p. The child node contains the info x.

  • 8/8/2019 02. Binary Tree

    18/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1818

    APPLICATION OF BINARY TREE

    A binary tree is a useful data structure when two-way decisions must be

    made at each point in a process.

    For example, suppose we wanted to find all duplicates in a list ofnumbers:

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    One way of finding duplicates is to compare each number with all those

    that precede it.

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  • 8/8/2019 02. Binary Tree

    19/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1919

    The first number in the list is placed in a node that is designated as the root ofa binary tree.

    Initially, both left and right sub trees of the root are empty.

    We take the next number and compare it with the number placed in the root.

    If it is the same then we have a duplicate.

    Otherwise, we create a new tree node and put the new number in it.

    The new node is made the left child of the root node if the second number isless than the one in the root.

    The new node is made the right child if the number is greater than the one inthe root.

    CREATION OF BINARY TREE

  • 8/8/2019 02. Binary Tree

    20/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2020

    If the list of numbers is large and is growing, this procedure involves a largenumber of comparisons.

    A linked list could handle the growth but the comparisons would still be large.

    The number of comparisons can be drastically reduced by using a binary tree.

    The tree grows dynamically like the linked list.

    SEARCHING FOR DUPLICATES

  • 8/8/2019 02. Binary Tree

    21/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2121

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    SEARCHING FOR DUPLICATES

  • 8/8/2019 02. Binary Tree

    22/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2222

    15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 515, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    1415

    SEARCHING FOR DUPLICATES

  • 8/8/2019 02. Binary Tree

    23/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2323

    SEARCHING FOR DUPLICATES

    15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 515, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    15

  • 8/8/2019 02. Binary Tree

    24/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2424

    SEARCHING FOR DUPLICATES

    4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 54, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    15

    4

  • 8/8/2019 02. Binary Tree

    25/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2525

    SEARCHING FOR DUPLICATES

    4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 54, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

  • 8/8/2019 02. Binary Tree

    26/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2626

    SEARCHING FOR DUPLICATES

    9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 59, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

  • 8/8/2019 02. Binary Tree

    27/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2727

    SEARCHING FOR DUPLICATES

    9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 59, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

  • 8/8/2019 02. Binary Tree

    28/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2828

    SEARCHING FOR DUPLICATES

    7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 57, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

  • 8/8/2019 02. Binary Tree

    29/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2929

    SEARCHING FOR DUPLICATES

    7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 57, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

  • 8/8/2019 02. Binary Tree

    30/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3030

    SEARCHING FOR DUPLICATES

    18, 3, 5, 16, 4, 20, 17, 9, 14, 518, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    18

  • 8/8/2019 02. Binary Tree

    31/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3131

    SEARCHING FOR DUPLICATES

    18, 3, 5, 16, 4, 20, 17, 9, 14, 518, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    18

  • 8/8/2019 02. Binary Tree

    32/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3232

    SEARCHING FOR DUPLICATES

    3, 5, 16, 4, 20, 17, 9, 14, 53, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    18

    3

  • 8/8/2019 02. Binary Tree

    33/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3333

    SEARCHING FOR DUPLICATES

    3, 5, 16, 4, 20, 17, 9, 14, 53, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

  • 8/8/2019 02. Binary Tree

    34/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3434

    SEARCHING FOR DUPLICATES

    5, 16, 4, 20, 17, 9, 14, 55, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

  • 8/8/2019 02. Binary Tree

    35/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3535

    SEARCHING FOR DUPLICATES

    5, 16, 4, 20, 17, 9, 14, 55, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

  • 8/8/2019 02. Binary Tree

    36/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3636

    SEARCHING FOR DUPLICATES

    16, 4, 20, 17, 9, 14, 516, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

  • 8/8/2019 02. Binary Tree

    37/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3737

    SEARCHING FOR DUPLICATES

    16, 4, 20, 17, 9, 14, 516, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

  • 8/8/2019 02. Binary Tree

    38/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3838

    SEARCHING FOR DUPLICATES

    4, 20, 17, 9, 14, 54, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

    4

  • 8/8/2019 02. Binary Tree

    39/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 3939

    SEARCHING FOR DUPLICATES

    20, 17, 9, 14, 520, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

    20

  • 8/8/2019 02. Binary Tree

    40/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4040

    SEARCHING FOR DUPLICATES

    20, 17, 9, 14, 520, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

  • 8/8/2019 02. Binary Tree

    41/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4141

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17pq

  • 8/8/2019 02. Binary Tree

    42/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4242

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17p

    q

    TRACE OF INSERT

  • 8/8/2019 02. Binary Tree

    43/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4343

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/8/2019 02. Binary Tree

    44/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4444

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/8/2019 02. Binary Tree

    45/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4545

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

  • 8/8/2019 02. Binary Tree

    46/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4646

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/8/2019 02. Binary Tree

    47/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4747

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

  • 8/8/2019 02. Binary Tree

    48/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4848

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/8/2019 02. Binary Tree

    49/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 4949

    TRACE OF INSERT

    17, 9, 14, 517, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    p->setRight( node );

    node

    TREE TRAVSERSAL

  • 8/8/2019 02. Binary Tree

    50/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5050

    TREE TRAVSERSAL14

    154

    (4,14,15), (4,15,14)

    (14,4,15), (14,15,4)

    (15,4,14), (15,14,4)

    Three common ways:node

    Preorder: (N,L,R)

    Inorder: (L,N,R)

    Postorder: (L,R,N)left

    subtreeright

    subtreeL

    N

    R

  • 8/8/2019 02. Binary Tree

    51/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5151

    3

    1

    2

    5

    6

    7 9

    8

    4

    INORDER

  • 8/8/2019 02. Binary Tree

    52/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5252

    INORDER

    Inorder: 3 4 5 7 9 14 15 16 17 18 20Inorder: 3 4 5 7 9 14 15 16 17 18 20

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/8/2019 02. Binary Tree

    53/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5353

    Make Money Fast!

    1. Motivations References2. Methods

    2.1 StockFraud

    2.2 PonziScheme

    1.1 Greed 1.2 Avidity2.3 BankRobbery

    1

    2

    3

    5

    46 7 8

    9

    PREORDER

  • 8/8/2019 02. Binary Tree

    54/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5454

    Preorder: 14 4 3 9 7 5 15 18 16 17 20Preorder: 14 4 3 9 7 5 15 18 16 17 20

    14

    154

    9

    7

    183

    5

    16 20

    17

    PREORDER

  • 8/8/2019 02. Binary Tree

    55/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5555

    cs16/

    homeworks/ todo.txt1K

    programs/

    DDR.java10K

    Stocks.java25K

    h1c.doc3K

    h1nc.doc2K

    Robot.java20K

    9

    3

    1

    7

    2 4 5 6

    8

    POSTORDER

  • 8/8/2019 02. Binary Tree

    56/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5656

    POSTORDER

    Postorder: 3 5 7 9 4 17 16 20 18 15 14Postorder: 3 5 7 9 4 17 16 20 18 15 14

    14

    154

    9

    7

    183

    5

    16 20

    17

    COMPLEXITY

  • 8/8/2019 02. Binary Tree

    57/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5757

    COMPLEXITY

    Average Case : O (log2n)

    Best Case : O (log2n)

    Worst Case : O (log2n)

    BINARY SEARCH TREE

  • 8/8/2019 02. Binary Tree

    58/58

    (C) GOYANI MAHESH(C) GOYANI MAHESH 5858

    BINARY SEARCH TREE

    Binary Search Tree (BST) is a binary tree, which is either empty or satisfiesfollowing properties :

    Every node has a value and no two nodes have the same property

    If there exists a LEFT CHILD then its value is less than the value ofROOT.

    If there exists a RIGHT CHILD then its value is greater than the valueofROOT.

    50

    25

    20 40

    30 77 90

    80

    75