tree and binary search tree

124
TREES

Upload: muhazzab-chouhadry

Post on 21-Mar-2017

75 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Tree and Binary Search tree

TREES

Page 2: Tree and Binary Search tree

Arrays

Linked Lists

17 20 29 34 483020 34 29 30 4817

30

21

6 14

28

48

6 48

14

21

28

30

Storing and modifying datafast searching, slow insertion

slow searching, fast insertion

7

Page 3: Tree and Binary Search tree

Hierarchically Structure

Page 4: Tree and Binary Search tree

Trees A tree is often used to represent a hierarchy . Because the relationships between the items in the

hierarchy suggest the branches of a botanical tree. For example, a tree-like organization chart is often

used to represent the lines of responsibility in a business as shown in Figure 

Page 5: Tree and Binary Search tree

Trees

Page 6: Tree and Binary Search tree

Trees As we see in the figure, the tree is upside-down. This is the usual way the data structure is drawn. The president is called the root of the tree and the

clerks are the leaves. A tree is extremely useful for certain kinds of

computations. For example, suppose we wish to determine the total

salaries paid to employees by division or by department.

The total of the salaries in division A can be found by computing the sum of the salaries paid in departments A1 and A2 plus the salary of the vice-president of division A.

Page 7: Tree and Binary Search tree

Trees Similarly, the total of the salaries paid in

department A1 is the sum of the salaries of the manager of department A1 and of the two clerks below her.

Clearly, in order to compute all the totals, it is necessary to consider the salary of every employee.

Therefore, an implementation of this computation must visit all the employees in the tree.

An algorithm that systematically visits all the items in a tree is called a tree traversal.

Page 8: Tree and Binary Search tree

Trees in our life Tree is an important data structure that

represent a hierarchy Trees/hierarchies are very common in

our life: Tree of species(class/type) (is-a) Component tree (part-of) Family tree (parent-child)

Page 9: Tree and Binary Search tree

9

Tree Terminology A tree is a collection of elements (nodes) Each node may have 0 or more successors

(Unlike a list, which has 0 or 1 successor) Each node has exactly one predecessor

Except the starting / top node, called the root Links from node to its successors are called

branches Successors of a node are called its children Predecessor of a node is called its parent Nodes with same parent are siblings Nodes with no children are called leaves

Page 10: Tree and Binary Search tree

10

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has a Root Node

ROOT NODE

Page 11: Tree and Binary Search tree

11

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Leaf nodes have no children

LEAF NODES

Page 12: Tree and Binary Search tree

More Terminologies Path

A sequence of edges Length of a path

number of edges on the path Depth/Level of a node

length of the unique path from the root to that node Height of a node

length of the longest path from that node to a leaf all leaves are at height 0

The height of a tree = the height of the root = the depth of the deepest leaf Ancestor and descendant

If there is a path from n1 to n2 n1 is an ancestor of n2, n2 is a descendant of n1 Proper ancestor and proper descendant

Page 13: Tree and Binary Search tree

13

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has Levels

LEVEL 0

Page 14: Tree and Binary Search tree

14

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Level One

LEVEL 1

Page 15: Tree and Binary Search tree

15

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Level Two

LEVEL 2

Page 16: Tree and Binary Search tree

16

Representation Of a General Tree-- first child/next sibling

Example for this tree:A

B D

F G

H

E

C

A

null

First child Next sibling

B

E

null

H

null null

C

null

D

null

F

null null

G

null

Cannot directly access D from A.

ParentPtr

Key value

sibling1st child

Page 17: Tree and Binary Search tree

Tree Formal Definitions A tree is a collection of nodes. The collection can be

empty, Otherwise, a tree consists of a distinguished node r,

called the root, and zero or more (sub) trees T1, T2,.……, Tk, each of whose roots are connected by a directed edge to r.

The root of each subtree is said to be a child of r and r is the parent of each subtree root.

Page 18: Tree and Binary Search tree

Binary Tree The simplest form of tree is a binary tree. A binary tree

consists of a. a node (called the root node) and b. left and right sub-trees.

Both the sub-trees are themselves binary trees We now have a recursively defined data structure.

Page 19: Tree and Binary Search tree

Binary Tree

Page 20: Tree and Binary Search tree

What is a binary tree? Each node can have up to two successor

nodes (children) The predecessor node of a node is called its parent The "beginning" node is called the root (no parent) A node without children is called a leaf

Page 21: Tree and Binary Search tree

21

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Subtree

LEFT SUBTREE OF ROOT NODE

Page 22: Tree and Binary Search tree

22

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Another Subtree

RIGHT SUBTREE OF ROOT NODE

Page 23: Tree and Binary Search tree

Binary TreesSome Binary Trees

One node Two nodes

Three nodes

Page 24: Tree and Binary Search tree

Convert a Generic Tree to a Binary Tree

Page 25: Tree and Binary Search tree

Binary Search Trees Binary Search Tree Property: The

value stored at a node is greater than the value stored at its left child and less than the value stored at its right child.

Page 26: Tree and Binary Search tree
Page 27: Tree and Binary Search tree

Tree node structure

template<class ItemType>class TreeNode { ItemType info; TreeNode* left; TreeNode* right; };

Page 28: Tree and Binary Search tree

Binary

TreeNotationIn C++ the structure of binary tree node can be specified as a

class named Node:

class Node{public:int key; // key stored in the node

Node *left; // link to left childNode *right; // link to right child};

parentp[x]

Using pseudo code convention, the links aredefined as follows:x -pointer to a nodeleft[x]- pointer to left child of x right[x]-pointer to right child of x p[x]-pointer to parent of xkey[x]-key stored in node x

x key

left[x] right[x]

left child right child

Page 29: Tree and Binary Search tree

Function RetrieveItem

Page 30: Tree and Binary Search tree

Function RetrieveItem What is the base case(s)?

1) When the key is found2) The tree is empty (key was not found)

What is the general case?Search in the left or right subtrees

Page 31: Tree and Binary Search tree

Binary Tree Traversal Tree Traversal is the process of visiting each node in the

tree exactly one time. Tree traversals are of two types

Depth First Traversal Breadth First Traversal

The three Depth First Traversal techniques are Preorder tree traversal

Inorder tree traversal

Postorder tree traversal

Page 32: Tree and Binary Search tree

On a visit to node the data stored

·Preorder Tree Traversal

is processed, printed, etc.

A1

In preorder traversal first the root is visited , then left subtree is visitedpreorder , and finally the right subtree is visited preorder. B C

2

Preorder: A B C

A·Inorder Tree TraversalIn inorder traversal first left subtree is visited inorder then root is visited and finally the right subtree is visited inorder .

2

1B C

Inorder: B A C

2A·Postorder Tree TraversalIn postorder traversal first the left subtree is visited post order, then right subtree is visited postorder, and finally root is visited . B C

1Postorder: B C A

In tree traversal each node is visited once only. Traversal is also referred to as walk.The standard procedures of traversing a tree are known as preorder, inorder, and postorder.

Binary Tree Traversal

Page 33: Tree and Binary Search tree

Binary TreeTraversal

Euler TourEuler tour traversal of binary tree is a path around the tree.Tour starts from the root toward the left child. The tree edges are kept to the left . Each node is encountered three times: from left, from below and from right

Postorder lists nodes on left of Euler path. Inorder lists nodes above the pathPostorder lists nodes on the right.

Page 34: Tree and Binary Search tree

TreeTraversal

Recursive AlgorithmsThe recursive procedures for tree traversal use recursive functions. Initially, the pointer to the root is passed as argument to the function.

POSTORDER(x)1 if x ≠ nil2 then POSTORDER(left[x]) ► Traverse left subtree postorder3 POSTORDER(right[x]) ► Traverse right subtree postorder4 print key[x] ►Print key

INORDER(x)1 if x ≠ nil2 then INORDER(left[x]) ► Traverse left subtree inorder3 print key[x] ► Print the key4 INORDER(right[x]) ►Traverse right subtree inorder

PREORDER(x)1 if x ≠ nil2 then print key[x] ► Print key3 PREORDER(left[x]) ► Traverse left subtree preorder4 PREORDER(right[x]) ►Traverse right subtree preorder

Page 35: Tree and Binary Search tree

Inorder Tree TraversalIterative Algorithm

The iterative procedure uses a stack to hold addresses of the nodes waiting to be processed in traversal order. It includes following steps:

Step #1:Push address of root on to the stack.

Step #2: Traverse the leftmost path of the, pushing the address of each node on to thestack, and stopping when a leaf is reached.

Step #3: Pop the stack, and process the popped off node.

Step #4: If null is popped, exit, otherwise proceed to next step.

Step#5: If a right child of the processed node exists, push its address to stack.

Step#6: Repeat step # 2

Page 36: Tree and Binary Search tree

Inorder Traversal

Step#:1Stack Step#:2Stack

Step#:4Step#:3

Step#:6Step#:5

B

A

D

B

A

H

D

B

A

D

B

A

B

AA

Page 37: Tree and Binary Search tree

Inorder Traversal-contdStep#:8Stack StackStep#:7

Step#:10Step#:9

Step#:12Step#:11

E

A

J

E

A

E

AA

B

A

I

B

A

Page 38: Tree and Binary Search tree

Inorder Traversal-contdStep#:13 Step#:14Stack Stack

Step#:16Step#:15

Step#:17 Step#:18

C

C

F

C

A

Page 39: Tree and Binary Search tree

Inorder Traversal: Example-contd

Step#:19 Step#:20Stack Stack

The inorder traversal of the sample binary tree is given byH D I B J E A F C G

G

Page 40: Tree and Binary Search tree

40

Tree sizeint TreeSize (root: TreePointer) begin if root==null //this is left/right child point of a leaf then return 0; else return 1 + TreeSize(root->left) + TreeSize(root->right); end;

Size of a Node: the number of descendants the node has (including the node itself). The size of root is the size of a tree. The size of a leaf is 1.

Page 41: Tree and Binary Search tree

Properties of Binary Trees A binary tree is a A binary tree is a fullfull binary tree if and binary tree if and

only if:only if: Each non leaf node has Each non leaf node has exactly twoexactly two child child

nodesnodes All leaf nodes lies at the same levelAll leaf nodes lies at the same level

It is called It is called fullfull since all possible node since all possible node slots are occupiedslots are occupied

Page 42: Tree and Binary Search tree

COMPLETE BINARY TREE

A complete binary tree of depth d is the strictly binary tree all of whose leaves are at level d.

A

CB

F GDE

HI

J K

LM

N O

Page 43: Tree and Binary Search tree

ALMOST COMPLETE BINARY TREEA binary tree of depth d is an almost binary tree if–Any node n at level less than d - 1 has two sons –For any node n in the tree with a right or left child at level I, the node must have left child (if it has right child) and all the nodes on the left hand side of the node must have two children.

Not an almost complete treeViolate condition 1 Almost complete binary tree

Page 44: Tree and Binary Search tree

A Full Binary Tree - Example

Page 45: Tree and Binary Search tree

Complete Binary Trees - Example

BB

AA

CC

DD EE

HH II JJ KK

FF GG

Figure 13.8 A complete binary treeFigure 13.8 A complete binary tree

Page 46: Tree and Binary Search tree

Strictly Binary Trees A binary tree in which every non-leaf node has

non-empty left and right sub-trees A strict binary tree with n leaves always contain

2n-1 nodes. Level of a node = depth of a node

Page 47: Tree and Binary Search tree

Almost Complete Binary Tree A binary tree of depth d is an almost

complete binary tree if Any node n at level less than d - 1 has two

sons (complete tree until level d-1) For any node n in the tree with a right or

left child at level d, the node must have left child (if it has right child) and all the nodes on the left hand side of the node must have two children.

Page 48: Tree and Binary Search tree

Almost Complete Binary Tree

Examples:

Not an almost complete treeViolate condition 1

Not an almost complete treeViolate condition 2

Page 49: Tree and Binary Search tree

Almost Complete Binary Tree

Almost complete binary tree

1

2 3

4 5 6 7

8 9

Almost complete binary tree

but not strictly binary tree

7

1

2 34 5 6

8 910

Numbering of an almost complete binary tree

Page 50: Tree and Binary Search tree

Numbering of an Almost Complete Binary Tree

Almost complete binary tree

1

2 3

4 5 6 7

8 9

Almost complete binary tree

but not strictly binary tree

1

2 34 5 6 7

8 910

n nodes of an almost complete binary tree can be numbered from 1 to n

Page 51: Tree and Binary Search tree

Function Insert Item

Use the binary search tree property to insert the new item at the correct place

Page 52: Tree and Binary Search tree

Function InsertItem(cont.)

• Implementing insertion using recursion

Insert 11

Page 53: Tree and Binary Search tree

What is the base case(s)?The tree is empty

What is the general case?Choose the left or right subtree

Function InsertItem (cont.)

Page 54: Tree and Binary Search tree

template<class ItemType>void TreeType<ItemType>::InsertItem(ItemType item){

Insert(root, item);} template<class ItemType>void Insert(TreeNode<ItemType>*& tree, ItemType item){

if(tree == NULL) { // base case tree = new TreeNode<ItemType>; tree->info = item; }

else if(item < tree->info) Insert(tree->left, item);

else Insert(tree->right, item);

}

Function InsertItem (cont.)

Page 55: Tree and Binary Search tree

Binary Tree Traversal Breadth First Tree Traversal Implementation of this kind of traversal

is straightforward when a queue is used. Consider a top down left-to-right,

breadth first traversal. After a node is visited, its children, if any

are placed at the end of a queue, and the node at the beginning of the queue is visited.

Page 56: Tree and Binary Search tree

Breadth First Traversal Example

A

C

D

B

E

FG

I

H

Page 57: Tree and Binary Search tree

Breadth First Traversal Example

C

Queue

B

H

A

C

D

B

E

FG

I

A

Page 58: Tree and Binary Search tree

Breadth First Traversal Example

Dqueue C

B

H

A

C

D

B

E

FG

I

AC

Page 59: Tree and Binary Search tree

Breadth First Traversal Example

B

Enqueu D

D

H

A

C

D

B

E

FG

I

AC

Page 60: Tree and Binary Search tree

Breadth First Traversal Example

Dqueue B

D

H

A

C

D

B

E

FG

I

ACB

Page 61: Tree and Binary Search tree

Breadth First Traversal Example

D

Enqueue E, H

E H

H

A

C

D

B

E

FG

I

ACB

Page 62: Tree and Binary Search tree

Breadth First Traversal Example

Dqueue D

E H

H

A

C

D

B

E

FG

I

ACBD

Page 63: Tree and Binary Search tree

Breadth First Traversal Example

Dqueue E

H

H

A

C

D

B

E

FG

IACBDE

Page 64: Tree and Binary Search tree

Breadth First Traversal Example

Enqueue F, G

H F G

H

A

C

D

B

E

FG

IACBDE

Page 65: Tree and Binary Search tree

Breadth First Traversal Example

Dqueue H

F G

H

A

C

D

B

E

FG

I

ACBDEH

Page 66: Tree and Binary Search tree

Breadth First Traversal Example

I

Enqueue I

F G

H

A

C

D

B

E

FG

I

ACBDEH

Page 67: Tree and Binary Search tree

Breadth First Traversal Example

H

A

C

D

B

E

FG

I

I

Dqueue F

G

ACBDEHF

Page 68: Tree and Binary Search tree

Breadth First Traversal Example

H

A

C

D

B

E

FG

I

I

Dqueue G

ACBDEHFG

Page 69: Tree and Binary Search tree

Breadth First Traversal Example

H

A

C

D

B

E

FG

I

Dqueue I

ACBDEHFGI

Page 70: Tree and Binary Search tree

Simple Search Algorithm Let us now state a simple search algorithm that will try to give

you an idea about the sort of data structures that will be used while searching, and the stop criteria for your search. The strength of the algorithm is such that we will be able to use this algorithm for both Depth First Search (DFS) and Breadth First Search (BFS).

Let S be the start state1. Initialize Q with the start node Q=(S) as the only entry;

set Visited =(S)2. If Q is empty, fail. Else pick node X from Q3. If X is a goal, return X, we’ve reached the goal4. (Otherwise) Remove X from Q5. Find all the children of state X not in Visited6. Add these to Q; Add Children of X to Visited7. Go to Step 2

Page 71: Tree and Binary Search tree

Simple Search Algorithm Here Q represents a priority queue. The

algorithm is simple and doesn’t need much explanation. We will use this algorithm to implement blind and uninformed searches. The algorithm however can be used to implement informed searches as well.

Page 72: Tree and Binary Search tree

Simple Search Algorithm Applied to Depth First Search Depth First Search dives into a tree

deeper and deeper to fine the goal state. As mentioned previously we will give

priority to the element with minimum P(n) hence the node with the largest value of height will be at the maximum priority to be picked from Q. The following sequence of diagrams will show you how DFS works on a tree using the Simple Search Algorithm.

Page 73: Tree and Binary Search tree

We start with a tree containing nodes S, A, B, C, D, E, F, G, and H, with H as the goal node. In the bottom left table we show the two queues Q and Visited. According to the Simple Search Algorithm, we initialize Q with the start node S,

Page 74: Tree and Binary Search tree

• If Q is not empty, pick the node X with the minimum P(n) (in this case S), as it is• the only node in Q. Check if X is goal, (in this case X is not the goal). Hence find• all the children of X not in Visited and add them to Q and Visited. Goto Step 2.

Again check if Q is not empty, pick the node X with the minimum P(n) (in this case either A or B), as both of them have the same value for P(n). Check if X is goal, (in this case A is not the goal). Hence, find all the children of A not in Visited and add them to Q and Visited. Go to Step 2.

Page 75: Tree and Binary Search tree

Go on following the steps in the Simple Search Algorithm till you find a goal node.The diagrams below show you how the algorithm proceeds.

Page 76: Tree and Binary Search tree

Here, from the 5th row of the table when we remove H and check if it’s the goal, the algorithm says YES and hence we return H as we have reached the goal state.

Page 77: Tree and Binary Search tree

The diagram below also shows that DFS didn’t have to search the entire search space, rather only by traveling in half the tree, the algorithm was able to search the solution.

Page 78: Tree and Binary Search tree

Simple Search Algorithm Applied to Breadth First Search Self study

Page 79: Tree and Binary Search tree

Problems with DFS and BFS ????

Page 80: Tree and Binary Search tree

Binary Search Trees

Page 81: Tree and Binary Search tree

BinarySearch Tree

DefinitionA binary search tree (BST) is organized such that key in a node is larger than any key in the left subtree and smaller than any key in right subtree.Iif x is pointer to a tree node then binary-search-tree property implies

key[x] > key[ left[x] ]

key[x] < key[right[x]] for all x

In BST, nodes with duplicate keys are not allowed.

Binary Search Tree

Page 82: Tree and Binary Search tree

Binary SearchTreeOperationsCommon operations performed on binary search tree are :

·INSERT- Add a new node with a given key

·SEARCH-Search a node with a given key

·DELETE-Delete a node with a given key

·MAXIMUM-Find largest key in the tree or in a subtree

·MINIMUM-Find smallest key in the tree or in a subtree

·SUCCESSOR- Find a node with smallest key which larger than the key in given node

·TRAVERSE- Traverse tree inorder, preorder, or postorder

Page 83: Tree and Binary Search tree

Binary Search TreeInsertion-1

Inserting a new node with key 10 into a Binary Search Tree

Page 84: Tree and Binary Search tree

Binary Search TreeInsertion-2

Insertion key 10 compared with key in the root node. It is smaller . Next, the left child is examined.

Page 85: Tree and Binary Search tree

Binary Search TreeInsertion-3

Insertion key 10 compared with key in the left child. It is smaller . Next, the left child is examined.

Page 86: Tree and Binary Search tree

Binary Search TreeInsertion-4

Insertion key 10 compared with key in the left child . It is smaller . Next, the left child is examined.

Page 87: Tree and Binary Search tree

Binary Search TreeInsertion-5

Insertion key 10 compared with key in the left child. It is larger . Next, the right child is examined.

Page 88: Tree and Binary Search tree

Binary Search TreeInsertion-6

Insertion key 10 compared with key in the right child. It is smaller . Next, the left child is examined.

Page 89: Tree and Binary Search tree

Binary Search TreeInsertion-7

Insertion key 10 compared with key in the left child. It is larger . Next, the right child is examined.

Page 90: Tree and Binary Search tree

Binary Search TreeInsertion-8

Insertion key 10 compared with key in the right child. It is smaller . Node with key 11 is a leaf. The new key is inserted as left child of the leaf being examined

Page 91: Tree and Binary Search tree

Binary SearchInsertion-9

Tree

key 10 is placed in new left child

Page 92: Tree and Binary Search tree

Binary SearchTree

INSERT MethodThe INSERT Method adds a new node. The pointer to new node is passed as argument. to the insert method. Starting with root, the input key is compared successively with left or right child until a leaf is reached.. The link pointer of the leaf is reset to point to the new node.

INSERT(T, z) ► z is pointer to new node1 y←NIL ► y is a temporary pointer used to track the parent2 x←root[T] ►x is another pointer to track current node. It is set initially to point to root3 while x ≠ NIL4 do y←x5 if key[z] <key[x] ►Compare given key with the key in node being examined6 then x←left[x] ►If given key is smaller proceed to left child7 else x←right[x] ►If given key is larger proceed to right child8 p[z]←y ► y holds the pointer to parent of the node being examined9 if y=NIL ►The node being added is the first node ( has no parent)10 then root[T] ← z11 else if key[z] < key[y] ►Check whether new node to be added is left child or right child12 then left[y]←z ►If left child, reset the parent node pointer link to left child13 else right[y]←z►If right child reset parent node pointer to link to right child

Page 93: Tree and Binary Search tree

Binary

SearchSearching -1

Binary tree is searched for node with key=71

Tree

Page 94: Tree and Binary Search tree

Binary Search TreeSearching -2

Search key 71 is compared with the key in root. It is larger.Next, the right child is examined

Page 95: Tree and Binary Search tree

Binary Search TreeSearching -3

Search key 71 is compared with the key 85 in right child. It is smaller.

Next, the left child is examined

Page 96: Tree and Binary Search tree

Binary Search TreeSearching -4

Search key 71 is compared with the key 81 in left child. It is smaller.Next, the left child is examined

Page 97: Tree and Binary Search tree

Binary Search TreeSearching -5

Search key 71 is compared with the key 68 in left child. It is larger.Next, the right child is examined

Page 98: Tree and Binary Search tree

Binary Search TreeSearching -6

Search key 71 is compared with the key 76 in left child. It is smaller.Next, the left child is examined

Page 99: Tree and Binary Search tree

Binary SearchSearching -7

Search key 71 matches. Search is successful.

Tree

Page 100: Tree and Binary Search tree

Binary Search TreeSEARCH Method

SEARCH(x, k) ►x is pointer to root and k is given key1 while x ≠ NIL and k ≠ key[x] ► Loop until a match is found or a leaf is reached2 do if k < key[x] ► Check if given key is smaller3 then x←left[x] ► Proceed to left child if key smaller4 else x←right[x] ► Proceed to right child if key is larger5 return x ►Return NIL if no match found and pointer to node with matched key

The SEARCH method finds a node with a given key. The search procedure starts with the root. It successively compares keys in the left or right children, depending on whether the key in the parent node is larger or smaller than the given key. The search terminates when either a matching key is found, or a leaf is reached. The method returns a pointer to the node which has the matching key or null pointer if no match is found..

Page 101: Tree and Binary Search tree

Binary SearchTreeSmallest KeyThe smallest key is found by starting with root node and traversing leftmost path of BST

Page 102: Tree and Binary Search tree

Binary Search TreeMinimum key in a Subtree

The minimum key is found by starting with root node of the subtree andtraversing leftmost path of in the subtree

Page 103: Tree and Binary Search tree

Binary SearchTree

MINIMUM MethodThe MINIMUM method finds the smallest key in a binary search tree or in any of the subtrees. It starts with the root ,or root of subtree, and proceeds down the leftmost path in the tree or subtree.

The procedure returns pointer to the node with the smallest key.

MINIMUM(x) ►x is pointer to root, or root of the subtree1 while left[x] ≠ NIL ►Move down the leftmost path until leaf is reached2 do x ←left[x] ►Move to left child3 return x ►Return pointer to the leaf , which contains the smallest key

Page 104: Tree and Binary Search tree

Binary SearchTree

Largest KeyThe largest key is found by starting with root node and traversing rightmost path of BST

Page 105: Tree and Binary Search tree

Binary Search TreeMaximum Key in a Subtree

The maximum key is found by starting with root node of the subtree andtraversing rightmost path of in the subtree

Page 106: Tree and Binary Search tree

Binary SearchTreeMAXIMUM MethodThe MAXIMUM method finds the largest key in a binary search tree or in any of the subtrees.It starts with the root ,or root of subtree, and proceeds down the rightmost path in the tree or subtree.

The procedure returns pointer to the node with the largest key.

MAXIMUM(x) ►x is pointer to root or root of the subtree1 while right[x] ≠ NIL ►Move down the rightmost path until leaf is reached2 do x ←right[x] ►Move to right child3 return x ►Return pointer to the leaf , which contains the largest key

Page 107: Tree and Binary Search tree

Binary Search TreeDeletinga leaf node

Leaf Node -1Node with key 73 is

Page 108: Tree and Binary Search tree

Binary Search TreeDeleting Leaf Node -2

First, the node with key 73 is searched. The key 73 is compared successively with keys 38, 82,47,62,75 until it matches with key in a node. The search path is highlighted

Page 109: Tree and Binary Search tree

Binary SearchTreeDeleting Leaf Node-3

Node with key 73 is de-linked from the parent node with key 75, by setting the left link-pointer in parent node to null

Page 110: Tree and Binary Search tree

BinarySearch TreeDeleting Leaf Node-4

Finally, the de-linked node with key 73 is removed

Page 111: Tree and Binary Search tree

Binary Search TreeDeleting Node With Single Child-1

Node with key 7 has a single left child with key 22

Page 112: Tree and Binary Search tree

Binary Search TreeDeleting Node With Single Child-2

First, the node with key 7 is searched. The key is compared successively with keys 38,25,and 6, until a match is found The search path is highlighted. The sub-tree, rooted at the node to be deleted, is shown in yellow shade.

Page 113: Tree and Binary Search tree

Binary SearchTreeDeleting Node With Single Child-3

The node to be deleted with key 7, is de-linked from its parent node with key 6 and its right child with key 22The right child link-pointer of parent node with key 6, is reset to point to the node with key 22

Page 114: Tree and Binary Search tree

BinarySearch TreeThe de-linked node with key 7 is removed

Deleting Node With Single Child-4

Page 115: Tree and Binary Search tree

BinarySearch TreeThe BST after node with 7 has been removed

Deleting Node With Single Child-5

Page 116: Tree and Binary Search tree

Binary Search TreeDeleting Node With Both Children-1

The node with key 33 is to be deleted. Itkeys 32 and 41.

has both left and right children with

Page 117: Tree and Binary Search tree

Binary Search TreeDeleting Node With Both Children-2

First, the node with key 33 is searched. The key 33 is successively compared with the keys 47 and 25 until a match is found. The search path is highlighted.

Page 118: Tree and Binary Search tree

Binary SearchTreeDeleting Node With Both Children-3In order to delete the node, its in-order successor is searched, which contains smallest of all the keys contained in the right sub-tree. Sub-tree is traversed by following the leftmost path, beginning with sub-tree node with .The search path is shown in yellow shade.

The in-order successor of node with key 33 is the node with key 34.

Page 119: Tree and Binary Search tree

Binary Search TreeDeleting Node With Both Children-4

in the node that contains key 33.The key 34 of in-order successor is to be copied

Page 120: Tree and Binary Search tree

Binary Search TreeDeleting Node With Both Children-5

The in-order successor with key 34 is de-linked from its parent node with key 38.

Page 121: Tree and Binary Search tree

Binary Search TreeDeleting Node Both Children-6

The key 34 in the in-order successorThe in-order successor is deleted.

is copied into the node that holds key 33.

Page 122: Tree and Binary Search tree

BinarySearchTreeDeleting Node Both Children-7

The node with key 33 is removed. The BST property is preserved.

Page 123: Tree and Binary Search tree

Binary SearchTreeSUCCESSOR Method

SUCCESSOR(x) ►x is pointer to given node1 If right[x] ≠ NIL ►If right subtree exists,then return pointer to node with smallest key2 then return MINUMUM(x)3 y ←p[x] ►Proceed upward to parent node4 while y ≠ NIL and x=right[y] ►Continue until root is reached, or else a node with left child is found5 do x ← y6 y← p[y] ► Move to parent node7 return y ► Return pointer to the successor node

The SUCCESSOR method finds the inorder successor of a node with given key. If the right subtree of the node exists, then the method finds the smallest key in the subtree. If the right subtree does not exist, then the method proceeds upward until an ancestor with a node with a left child of its parent is encountered.. It returns pointer to the successor node.

Page 124: Tree and Binary Search tree

Binary SearchTree

DELETE Method

DELETE(T, z)1 if left[z]=NIL or right[z]=NIL ►Check if node has no left or right child2 then y ← z ►if left or right child exists then y holds pointer to current node3 else y ← SUCCESSOR(z) ►otherwise, y holds pointer to the successor node4 if left[y] ≠ NIL ► check if the left child exists5 then x← left[x] ►if so, move to left child6 else x← right[y] ►otherwise, move to right child7 if x ≠ NIL8 then p[x] ← p[y] ► Set pointer of parent node9 if p[y]=NIL10 then root[T]← x11 else if y=left[[p[y]]►Set pointer of the parent to point to grand parent12 then left[p[y]]←x13 else right[p[y]] ←x14 if y≠ z15 then key[z]←key[y] ► Copy key of successor node16 return y

The DELETE method removes a node with given key. After deletion, the tree is restructured so that the binary-search-tree property is restored. The method deals with three cases(1) node is a leaf (2) Node is not a leaf and has one child (3) Node is not a leaf ,and has two children