Rachel Moss
Rachel Moss

Reputation: 11

Binary Search Trees, how do you find maximum?

I've been working with Binary Search Trees in my spare time, and I want to be able to delete nodes from a tree.

In order to get this to work, I need to find the maximum value. How do you go about doing that? Pseudo-code or hints would be appreciated. I'm stuck and not exactly sure how to even begin this.

Upvotes: 1

Views: 3716

Answers (2)

gcochard
gcochard

Reputation: 11744

A binary search tree has the following properties:

The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.

With that definition in mind, it should be very easy to find the max.

Upvotes: 5

Starx
Starx

Reputation: 79069

A simple pseudocode would be this. It it is indepandant to binary search I think.

int maxi = 0
foreach(array as item) // or any other loop
    if item>maxi then maxi = item

Upvotes: 0

Related Questions