1.2 operations of tree representations

34
. Binary Search Trees Operations and Implementation

Upload: krishver2

Post on 17-Aug-2015

26 views

Category:

Education


1 download

TRANSCRIPT

.Binary Search TreesOperations and Implementation

Definition

Binary search tree is a binary tree that satisfies the following constraint.

•The key value of left child is smaller than its parent key

•the key value of right child is greater than its parent.

OPERATIONS

Insertion

Deletion

Search

Find Max

Find Min

Insertion

Steps

1.Locate the parent of the node to be inserted

2.create a binary tree node

3.insert the node (attach the node to the parent)

Do by yourself

Insert the values 13, 3, 4,12, 14,

10, 5, 1, 8, 2, 7, 9,11, 6, 18 in

that order, starting from an

empty tree.

Deletion Operation

Case 1: Node to be deleted has no children

•simply delete the node.Case 2: Node to be deleted has either left or right

empty subtree

•append nonempty subtree to its grand parent nodeCase 3: Node to be deleted has both left and right

subtree

•Find the inorder successor node of the node to be deleted

•Attach the right subtree of inorder successor node to its grand parent

•Replace the node to be deleted by its inorder successor

9

7

5

64 8 10

9

7

5

6 8 10

Case 1: removing a node with 2 EMPTY SUBTREES

parent

cursor

Removal in BST: Example

Removing 4replace the link in the parent with null

Case 2: removing a node with 2 SUBTREES

9

7

5

6 8 10

9

6

5

8 10

cursorcursor

- replace the node's value with the max value in the left subtree- delete the max node in the left subtree

44

Removing 7

Removal in BST: Example

What other elementcan be used as replacement?

9

7

5

6 8 10

9

7

5

6 8 10

cursor

cursor

parent

parent

the node has no left child:link the parent of the node to the right (non-empty) subtree

Case 3: removing a node with 1 EMPTY SUBTREE

Removal in BST: Example

9

7

5

8 10

9

7

5

8 10

cursor

cursor

parent

parent

the node has no right child:link the parent of the node to the left (non-empty) subtree

Case 4: removing a node with 1 EMPTY SUBTREE

Removing 5

4 4

Removal in BST: Example

Deletion - Example

20

13 34

2815 36

22 29

332521

3

51

Search Operation

1. Start at the root node

2. Branch either left or right repeatedly until the element is found

3. if the search ends with empty subtree, the element is not present in the tree.

Do the following operations

Delete 4,10, 27,13 from the tree

shown before and show the

step by step results.

IMPLEMENTATION

Definining the node

struct node

{

int key;

node *parent;

node *left;

node *right;

};

Insert operationvoid insert_key(int key)

{

//scan through the binary tree

node *prev=NULL, *curr=root;

while (curr != NULL)

{

prev = curr;

if (key < curr->key)

curr = curr->left;

else if (key > curr->key)

curr = curr->right;

else

{

printf("Data Already Exists");

return ;

}

}

//create a node and assign the key

node *newnode = (struct node *)malloc(sizeof(struct node));

newnode->key = key;

newnode->parent=NULL;

newnode->left = newnode->right = NULL;

//insert the node

if (prev==NULL)

root = newnode;

else

{

if (key < prev->key)

prev->left = newnode;

else

prev->right = newnode;

newnode->parent=prev;

}

}

In-order Traversal

void inorder_traversal(node *ptr)

{

if (ptr !=NULL)

{

inorder_traversal(ptr->left);

printf(" %d", ptr->key);

inorder_traversal(ptr->right);

}

}

Pre-order and Post-order traversalvoid preorder_traversal(node *ptr)

{

if (ptr !=NULL)

{

printf(" %d", ptr->key);

preorder_traversal(ptr->left);

preorder_traversal(ptr->right);

}

}

void postorder_traversal(node *ptr)

{

if (ptr !=NULL)

{

postorder_traversal(ptr->left);

postorder_traversal(ptr->right);

printf(" %d", ptr->key);

}

}

Delete Operationvoid delete_key(int key){

if(root==NULL){

printf("\nTree is empty");return;

}//z -m node to be deleted //y - succssor node - x successor subtreenode *z=search_key(key);node *x,*y;if(z==NULL)

printf("Key not found\n");else{

if(z->left==NULL || z->right==NULL)y=z;

elsey=Tree_Successor(z);

if(y->left!=NULL)x=y->left;

elsex=y->right;

x->parent=y->parent;

if(y->parent==NULL)

root=x;

else

{

if(y==y->parent->left)

y->parent->left=x;

else

y->parent->right=x;

}

if(y!=z)

z->key=y->key;

free(y);

}

}

if(x!=NULL)

Finding the inorder successornode* Tree_Successor(node *temp){

node *y;if(temp->right!=NULL){

temp=temp->right;while(temp->left!=NULL)

temp=temp->left;return temp;

}else{

y=temp->parent;while(y!=NULL && temp==y->right){

temp=y;y=y->parent;

}return y;

}}

Search Operation

node* search_key(int key){

node *prev=NULL, *curr=root;while (curr != NULL){

prev = curr;if (key < curr->key)

curr = curr->left;else if (key > curr->key)

curr = curr->right;else {

return curr;//key found}

}return NULL;//key not found

}

1. Start with this grid of 12 matchsticks, remove two of them so that there are only two squares left.

Answer

3.Move two matchsticks to make only four identical squares.

Answer

GUIDED READING

ASSESSMENT

1.Which traversal technique traverses the elements of binary search tree in ascending order.

A. pre-order traversal

B. post-order traversal

C. in-order traversal

D. converse post-order traversal.

Contd..

2. The height of the binary tree in the best and worst case is

A. n and log n respectively

B. log n and n respectively

C. log n and n/2 respectively

D. n/2 and log n respectively

Contd..

3. Create a binary search tree using the following operations: insert 3, insert 7, insert 8, insert 1, insert 5, insert 0, insert 4, insert 6, insert 9.  Then, the left and right child of the inorder successor of node 5 iso0 and 6 respectivelyo4 and null respectivelyo3 and 9 respectivelyo4 and 6 respectively

Contd..

4.The data structure used in in-order traversal is

A. list

B. stack

C. queue

D. linked list

Contd..

5. The successor used in deletion operation of binary search tree is

A. inorder successor

B. preorder successor

C. postorder successor

D. converse preorder successor