About the binary tree, Exams of Computer System Design and Architecture

Explanation of binary tree and its applications

Typology: Exams

2017/2018

Uploaded on 12/22/2018

Rmahajan
Rmahajan 🇮🇳

2 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT
OF
Data structure
SUBMITTED TO: SUBMITTED BY
PROF.parmeet kaur Mandeep kaur
Roll no..2127
MCA-1st
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties
The left sub-tree of a node has a key less than or equal to its parent node's key.
pf3
pf4
pf5

Partial preview of the text

Download About the binary tree and more Exams Computer System Design and Architecture in PDF only on Docsity!

ASSIGNMENT

OF

Data structure

SUBMITTED TO: SUBMITTED BY

PROF.parmeet kaur Mandeep kaur

Roll no..

MCA-1st

A Binary Search Tree (BST) is a tree in which all the nodes follow the below-

mentioned properties −

  • The left sub-tree of a node has a key less than or equal to its parent node's key.
  • The right sub-tree of a node has a key greater than to its parent node's key.

Thus, BST divides all its sub-trees into two segments; the left sub-tree and

the right sub-tree and can be defined as −

left_subtree (keys)Operations on Binary search tree: ≤ node (key) ≤ right_subtree (keys)

  1. Searching : is a method to find out whether a particular data item is present in tree or not. Other operations are MIN, MAX , Successor and predecessor, these operations run in O(h) time where h is height of tree Algorithm: While(root ≠ NULL) { If(data(root) = n) return root; else if(data(root) < n) search(right(root),n) else search(left(root),n) } return NULL; } Example:

Else { Parent.left = node;} Return root; Example: 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18

  1. Deletion: Let x be a value to be deleted from the BST and let X denote the node containing the value x. Deletion of an element in a BST again uses the BST property in a critical way. When we delete the node X containing x, it would create a "void" that should be filled by a suitable existing node of the BST. There are two possible candidate nodes that can fill this void, in a way that the BST property is not violated: (1). Node containing highest valued element among all descendants of left child of X. (2). Node containing the lowest valued element among all the descendants of the right child of X. In case (1), the selected node will necessarily have a null right link which can be conveniently used in patching up the tree. In case (2), the selected node will necessarily have a null left link which can be used in patching up the tree. Figure 4.15 illustrates several scenarios for deletion in BSTs. Algo: Case1: Node to be deleted having no children
    1. Initialize CHILD
    2. If LEFT[LOC] = NULL and RIGHT[LOC] = NULL then
  1. Set CHILD =NULL
  2. Else if LEFT[LOC] ≠ NULL then
  3. Set CHILD = LEFT[LOC]
  4. Else set CHILD = RIGHT[LOC]
  5. End of if structure
  6. If Par ≠ NULL then
  7. If LOC = LEFT[PAR] then 10.Set LEFT[PAR] = CHILD 11.Else set RIGHT[PAR] = CHILD 12.End of if 13.Else set ROOT = CHILD 14.End of if 15.Return Example:

Case2: Node to be deleted having two child Algorithm: [Find SUC and PARSUC], which points to inorder successor of N and parent successor points to parent node of inorder successor. a. Set PTR= RIGHT[LOC] and save[LOC] b. Repeat while LEFT[PTR]≠ NULL SET SAVE = PTR AND PTR = LEFT[PTR] [end while] c. Set SUC = PTR AND PARSUC = SAVE Step2: Call the CASEA(INFO, LEFT, RIGHT, ROOT, SUC, PARSUC) Step3: a. if PAR≠ NULL then If LOC = LEFT[PAR] then