Trees - BST

download Trees - BST

of 101

Transcript of Trees - BST

  • 7/30/2019 Trees - BST

    1/101

    Trees

  • 7/30/2019 Trees - BST

    2/101

    Inorder Traversal

    Left Root Right manner

    Left + Right

    [Left*Right]+[Left+Right]

    (A*B)+[(Left*Right)+E)

    (A*B)+[(C*D)+E]

    +

    * +

    A B * E

    C D

    (A*B)+(C*D+E)

  • 7/30/2019 Trees - BST

    3/101

    Preorder Traversal

    Root Left Right manner

    + Left Right

    + [*Left Right] [+Left Right]

    +(*AB) [+ *Left Right E]+*AB + *C D E

    +

    * +

    A B * E

    C D

  • 7/30/2019 Trees - BST

    4/101

  • 7/30/2019 Trees - BST

    5/101

    Trees Traversal

    Inorder(Left) Root (Right)

    PreorderRoot (Left) (Right)

    Postorder

    (Left) (Right) Root

    Root

    Left Right

  • 7/30/2019 Trees - BST

    6/101

    Expression Trees

    Expression tree for:

    (a+b*c) +((d*e+f)*g)

    Inorder traversal

    Preorder traversal

    Postorder traversal

  • 7/30/2019 Trees - BST

    7/101

    Algorithm to convert postfix expression

    into expression tree

    e.g. input: ab+cde+**

    Expression Trees

  • 7/30/2019 Trees - BST

    8/101

    Expression Trees

  • 7/30/2019 Trees - BST

    9/101

    BINARY SEARCH TREE (BST)

  • 7/30/2019 Trees - BST

    10/101

    Number guessing game

    Im thinking of a number between 1 and n

    You are trying to guess the answer

    For each guess, Ill tell you correct,higher or lower

    Describe an algorithm that minimizes thenumber of guesses

  • 7/30/2019 Trees - BST

    11/101

    For every node, X, in the tree, the values

    of all the keys in its left subtree are

    smaller than the key value of X, and thevalues of all the keys in its right subtree

    are larger than the key value of X.

    X

    Binary Search Tree

  • 7/30/2019 Trees - BST

    12/101

    Binary Search Trees

    BST A binary tree where a parents value is

    greater than its left child and less than or equal to

    its right child

    Why not?

    Can be implemented with pointers or an array

    )()( irightiileft right = newNode;

    break;

    }

    }

    else

    { cout

  • 7/30/2019 Trees - BST

    79/101

    Program

    // This program builds a binary tree with 5 nodes.

    #include

    #include "IntBinaryTree.h

    void main(void)

    {

    IntBinaryTree tree;

    cout

  • 7/30/2019 Trees - BST

    80/101

    Program

    Figure shows the structure of the binary tree built by the program.

    Note: The shape of the tree is

    determined by the orderin

    which the values are inserted.The root node in the diagram

    above holds the value 5 because

    that was the first value inserted.

    The IntBinaryTree class can displayall the values in the tree using

  • 7/30/2019 Trees - BST

    81/101

    y p y g

    all 3 of these algorithms.

    The algorithms are initiated by the following inline public memberfunctions -

    void showNodesInOrder(void)

    { displayInOrder(root); }

    void showNodesPreOrder()

    { displayPreOrder(root); }void showNodesPostOrder()

    { displayPostOrder(root); }

    Each of these public member functions calls a recursive private

    member function, and passes the root pointer as argument.

    The code for these recursive functions is simple -

    void IntBinaryTree::displayInOrder(TreeNode *nodePtr)

  • 7/30/2019 Trees - BST

    82/101

    y p y ( )

    {

    if (nodePtr)

    {

    displayInOrder(nodePtr->left);cout value right);

    }

    }

    void IntBinaryTree::displayPreOrder(TreeNode *nodePtr)

    {

    if (nodePtr)

    { cout value left);

    displayPreOrder(nodePtr->right);

    }

    }

    void IntBinaryTree::displayPostOrder(TreeNode *nodePtr)

  • 7/30/2019 Trees - BST

    83/101

    {

    if (nodePtr)

    {

    displayPostOrder(nodePtr->left);displayPostOrder(nodePtr->right);

    cout value

  • 7/30/2019 Trees - BST

    84/101

    Program

    // This program builds a binary tree with 5 nodes.// The nodes are displayed with inorder, preorder,

    // and postorder algorithms.

    #include

    #include "IntBinaryTree.h

    void main(void)

    {

    IntBinaryTree tree;

    cout

  • 7/30/2019 Trees - BST

    85/101

    cout Inorder traversal:\n ;

    tree.showNodesInOrder();

    cout

  • 7/30/2019 Trees - BST

    86/101

    5

    3

    8

    12

    9

    Postorder traversal:

    3

    9

    12

    8

    5

    As an intellectual exercise, convince yourself that for each of the 3

    algorithms, the 3 output orders of the values of the 3 traversals are

    correct.

    Searching the Tree

  • 7/30/2019 Trees - BST

    87/101

    Searching the Tree

    The IntBinaryTree class has a public member function called

    searchNode, that returns true if a value is found in the tree, or

    false otherwise.

    The functionstarts at the root node, and traverses the tree, until it

    finds the search value, orruns outof nodes.

    bool IntBinaryTree::searchNode(int num){

    TreeNode *nodePtr = root;

    while (nodePtr)

    {

    if (nodePtr->value == num)

    return true;else if (num < nodePtr->value)

    nodePtr = nodePtr->left;

    else

    nodePtr = nodePtr->right;

    }

    return false;

    }

    Program

  • 7/30/2019 Trees - BST

    88/101

    Program

    // This program builds a binary tree with 5 nodes.

    // The SearchNode function determines if the// value 3 is in the tree.

    #include

    #include "IntBinaryTree.h

    void main(void){

    IntBinaryTree tree;

    cout

  • 7/30/2019 Trees - BST

    89/101

    cout

  • 7/30/2019 Trees - BST

    90/101

    We want to delete the node, but preserve the sub-trees, that the

    node links to.

    There are 2 possible situations to be faced when deleting a non leaf

    node -

    1) The node has one child.

    2) The node has two children.

  • 7/30/2019 Trees - BST

    91/101

    The problem is not as easily solved if the node has two children.

  • 7/30/2019 Trees - BST

    92/101

    We cannot attach both of the nodes subtrees to its parent.

    One solution is to -

    a) find a position in the right subtree to attach the left subtree.

    b) attach the nodes right subtree to the parent

  • 7/30/2019 Trees - BST

    93/101

    Now the code - to delete a node from the IntBinaryTree, call thepublic memberremove. The argument passed to the function is the

    value of the node you want to delete.

    void IntBinaryTree::remove(int num)

  • 7/30/2019 Trees - BST

    94/101

    void IntBinaryTree::remove(int num)

    {

    deleteNode(num, root);

    }

    The remove member function calls the deleteNode member

    function. It passes the value of the node to delete, and the root

    pointer.

    The deleteNode member function is shown below -

    void IntBinaryTree::deleteNode(int num, TreeNode *&nodePtr)

    {

  • 7/30/2019 Trees - BST

    95/101

    {

    if (num < nodePtr->value)

    deleteNode(num, nodePtr->left);

    else if (num > nodePtr->value)

    deleteNode(num, nodePtr->right);

    else

    makeDeletion(nodePtr);

    }

    Notice the declaration of the nodePtr parameter:

    TreeNode *&nodePtr;

    nodePtr is not simply a pointer to a TreeNode structure, but a reference to a

    pointer to a TreeNode structure. Any action performed on nodePtr is actually

    performed on the argument passed into nodePtr

    The reason for doing this is explained shortly.

    The deleteNode function uses an if/else statement.

    if(num < nodePtr->value)

    d l t N d ( d Pt l ft)

  • 7/30/2019 Trees - BST

    96/101

    deleteNode(num, nodePtr->left);

    The above statement compares the parameter num with the value

    member of the node that nodePtr point to.

    If num is less, the value being searched for will appear somewhere

    in the nodePtrs left subtree (if it appears at all).

    So recall the deleteNode function recursively, with num as the first

    argument, and nodePtr->left as the second argument.

    If num is not less than nodePtr->value, the the following else ifstatement is executed.

    else if(num > nodePtr->value)

    deleteNode(num, nodePtr->right);

    If num is greater than nodePtr->value, then the value being searched

  • 7/30/2019 Trees - BST

    97/101

    g , g

    for will appear somewhere in nodePtrs right subtree (if it appears

    in the tree at all).

    If num is equal to nodePtr->value, then neither of the if statements

    above will find a true condition.

    So, nodePtr points to the node to be deleted, and the trailing else willbe executed.

    else

    makeDeletion(nodePtr);

    The makeDeletion function actually deletes the node from the tree

    and reattaches the deleted nodes sub trees.

    It must have access to the actual pointer in the tree to the node that

    is being deleted (not just a copy of the pointer)

  • 7/30/2019 Trees - BST

    98/101

    is being deleted (not just a copy of the pointer).

    This is why the nodePtrparameter in the deleteNode function is a

    reference. It must pass to makeDeletion, the actual pointer, to the

    node to be deleted.

    void IntBinaryTree::makeDeletion(TreeNode *&nodePtr)

    {

    TreeNode *tempNodePtr; // Temporary pointer, used in

    // reattaching the left subtree.

    if (nodePtr == NULL)

    cout right == NULL)

    { tempNodePtr = nodePtr;

    nodePtr = nodePtr->left; // Reattach the left child

    delete tempNodePtr;

    }

    else if (nodePtr->left == NULL)

    {

  • 7/30/2019 Trees - BST

    99/101

    {

    tempNodePtr = nodePtr;

    nodePtr = nodePtr->right; // Reattach the right child

    delete tempNodePtr;

    }

    // If the node has two children.

    else

    {

    // Move one node the right.

    tempNodePtr = nodePtr->right;

    // Go to the end left node.while (tempNodePtr->left)

    tempNodePtr = tempNodePtr->left;

    // Reattach the left subtree.

    tempNodePtr->left = nodePtr->left;

    tempNodePtr = nodePtr;

    // Reattach the right subtree.

    nodePtr = nodePtr->right;

    delete tempNodePtr;

    }

    }

    Program

  • 7/30/2019 Trees - BST

    100/101

    // This program builds a binary tree with 5 nodes.

    // The DeleteNode function is used to remove two

    // of them.#include

    #include "IntBinaryTree.h

    void main(void)

    {

    IntBinaryTree tree;

    cout

  • 7/30/2019 Trees - BST

    101/101

    tree.remove(8);

    cout