Construct Tree From Given Inorder and Preorder Traversals

Embed Size (px)

Citation preview

  • 7/26/2019 Construct Tree From Given Inorder and Preorder Traversals

    1/5

    Construct Tree from given Inorder and Preorder traversals

    Let us consider the below traversals:

    Inorder sequence: D B E A C

    Preorder sequence: A B D E C

    In a Preorder sequence! leftmost element is the root of the tree" #o we $now %A&

    is root for given sequences" B' searching %A& in Inorder sequence! we can (nd out

    all elements on left side of %A& are in left subtree and elements on right are in

    right subtree" #o we $now below structure now"

    A

    ) *

    ) *

    D B E C

    +e recursivel' follow above ste,s and get the following tree"

    A

    ) *

    ) *

    B C

    ) * )

    ) * )

    D E

    Algorithm: buildTree-.

    /. Pic$ an element from Preorder" Increment a Preorder Inde0 1ariable -,reInde0

    in below code. to ,ic$ ne0t element in ne0t recursive call"

    2. Create a new tree node t3ode with the data as ,ic$ed element"

    4. ind the ,ic$ed element&s inde0 in Inorder" Let the inde0 be inInde0"5. Call buildTree for elements before inInde0 and ma$e the built tree as left

    subtree of t3ode"

    6. Call buildTree for elements after inInde0 and ma$e the built tree as right

    subtree of t3ode"

    7. return t3ode"

    Than$s to 8ohini and Tushar for suggesting the code"

    C

    9ava

  • 7/26/2019 Construct Tree From Given Inorder and Preorder Traversals

    2/5

    P'thon

    ) ,rogram to construct tree using inorder and ,reorder traversals )

    ;include

  • 7/26/2019 Construct Tree From Given Inorder and Preorder Traversals

    3/5

    return 3HLL?

    ) Pic$ current node from Preorder traversal using ,reInde0

    and increment ,reInde0 )

    struct node t3ode G new3ode-,re,reInde0.?

    ) If this node has no children then return )

    if-in#trt GG inEnd.

    return t3ode?

    ) Else (nd the inde0 of this node in Inorder traversal )

    int inInde0 G search-in! in#trt! inEnd! t3ode=data.?

    ) Hsing inde0 in Inorder traversal! construct left and

    right subtress )

    t3ode=left G buildTree-in! ,re! in#trt! inInde0/.?

    t3ode=right G buildTree-in! ,re! inInde0/! inEnd.?

    return t3ode?

    @

    ) HTILITJ H3CTIK3# )

    ) unction to (nd inde0 of value in arrstart"""end

    The function assumes that value is ,resent in in )

    int search-char arr! int strt! int end! char value.

    >

    int i?

    for-i G strt? i

  • 7/26/2019 Construct Tree From Given Inorder and Preorder Traversals

    4/5

    >

    if-arri GG value.

    return i?

    @

    @

    ) el,er function that allocates a new node with the

    given data and 3HLL left and right ,ointers" )

    struct node new3ode-char data.

    >

    struct node node G -struct node.malloc-sieof-struct node..?

    node=data G data?

    node=left G 3HLL?

    node=right G 3HLL?

    return-node.?

    @

    ) This funtcion is here Must to test buildTree-. )

    void ,rintInorder-struct node node.

    >

    if -node GG 3HLL.

    return?

    ) (rst recur on left child )

    ,rintInorder-node=left.?

    ) then ,rint the data of node )

  • 7/26/2019 Construct Tree From Given Inorder and Preorder Traversals

    5/5

    ,rintf-NOc N! node=data.?

    ) now recur on right child )

    ,rintInorder-node=right.?

    @

    ) Driver ,rogram to test above functions )

    int main-.

    >

    char in G >FDF! FBF! FEF! FAF! FF! FCF@?

    char ,re G >FAF! FBF! FDF! FEF! FCF! FF@?

    int len G sieof-in.)sieof-in.?

    struct node root G buildTree-in! ,re! ! len /.?

    ) Let us test the built tree b' ,rinting Insorder traversal )

    ,rintf-NInorder traversal of the constructed tree is *nN.?

    ,rintInorder-root.?

    getchar-.?

    @