Insertion of Binary Trees

download Insertion of Binary Trees

of 2

Transcript of Insertion of Binary Trees

  • 8/6/2019 Insertion of Binary Trees

    1/2

  • 8/6/2019 Insertion of Binary Trees

    2/2

    If the value of the new node is greater than the value of the root node, the new node is:

    o appended as the right leaf of the root node if the right subtree is empty

    o else, the search process continues down the right subtree.

    If this search procedure finds that the insertion node already exists in the tree, the procedure terminates as it can notinsert it again.

    Note that careful consideration of the above algorithm reveals that the searching procedure described is actually a modifiednorder traversal of the tree.

    Deletion is the 'hard nut' to crack! Suppose we wish to delete a node from the tree below:

    Fig. 2.3.1: An example binary search tree (shown in abstraction)

    There are basically four cases to deal with as regards the node (holding the value) to be deleted:

    1. A leaf node

    2. A non-leaf node with an empty left subtree

    3. A non-leaf node with an empty right subtree

    4. A non-leaf node with neither of its subtrees empty

    Consistently using the in-order successor or the in-order predecessor for every instance of the two-child case can lead to

    n unbalanced tree, so good implementations add inconsistency to this selection.

    Running Time Analysis: Although this operation does not always traverse the tree down to a leaf, this is always a possibility;hus in the worst case it requires time proportional to the height of the tree. It does not require more even when the node has

    wo children, since it still follows a single path and does not visit any node twice.

    Traversal

    Once the binary search tree has been created, its elements can be retrievedin-orderby recursively traversing the left subtree

    he root node, accessing the node itself, then recursively traversing the right subtree of the node, continuing this pattern with

    ach node in the tree as it's recursively accessed. As with all binary trees, one may conduct a pre-order traversal or a post-ord

    raversal, but neither are likely to be useful for binary search trees.

    Sort

    A binary search tree can be used to implement a simple but efficientsorting algorithm. Similar toheapsort, we insert all the

    alues we wish to sort into a new ordered data structurein this case a binary search treeand then traverse it in order,

    http://en.wikipedia.org/wiki/Balanced_treehttp://en.wikipedia.org/wiki/In-order_traversalhttp://en.wikipedia.org/wiki/In-order_traversalhttp://en.wikipedia.org/wiki/Recursionhttp://en.wikipedia.org/wiki/Pre-order_traversalhttp://en.wikipedia.org/wiki/Post-order_traversalhttp://en.wikipedia.org/wiki/Post-order_traversalhttp://en.wikipedia.org/wiki/Post-order_traversalhttp://en.wikipedia.org/wiki/Sorting_algorithmhttp://en.wikipedia.org/wiki/Sorting_algorithmhttp://en.wikipedia.org/wiki/Sorting_algorithmhttp://en.wikipedia.org/wiki/Heapsorthttp://en.wikipedia.org/wiki/Heapsorthttp://en.wikipedia.org/wiki/Heapsorthttp://en.wikipedia.org/wiki/Balanced_treehttp://en.wikipedia.org/wiki/In-order_traversalhttp://en.wikipedia.org/wiki/Recursionhttp://en.wikipedia.org/wiki/Pre-order_traversalhttp://en.wikipedia.org/wiki/Post-order_traversalhttp://en.wikipedia.org/wiki/Post-order_traversalhttp://en.wikipedia.org/wiki/Sorting_algorithmhttp://en.wikipedia.org/wiki/Heapsort