Binary Search Tree Traversal Methods. How are they different from Binary Trees? In computer...

13
Binary Search Tree Traversal Methods

Transcript of Binary Search Tree Traversal Methods. How are they different from Binary Trees? In computer...

Page 1: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Binary Search Tree

Traversal Methods

Page 2: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

How are they different from Binary Trees?

In computer science, a binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left and right. Binary trees are commonly used to implement binary search trees and binary heaps.

Page 3: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Size?

Height

Root

Leaves

9

2

4

1,4,7,13

Highlight belowTo reveal answers

Note: The above tree is neither a sorted nor a balanced binary tree

Page 4: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Binary Search Tree

In computer science, a binary search tree (BST) is a binary tree data structure which has the following properties:

Each node (item in the tree) has a distinct value.

Both the left and right subtrees must also be binary search trees.

The left subtree of a node contains only values less than the node's value.

The right subtree of a node contains only values greater than or equal to the node's value.

Page 5: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Diagram of a Binary Search Tree

Size?

Depth

Root

Leaves

9

8

3

1,4,7,13

Highlight belowTo reveal answers

Page 6: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Major advantage of Binary Search Trees?

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.

Page 7: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Which two programming processes are used when searching a Binary Search tree?

Recursive Process or

Iterative Process

In an ordinaryBinary tree, youHave three iterativeProcedures that canBe used.

PreorderPostorderinorder

Page 8: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Recursive or Iterative?

The above code was written in Python (wikipedia example)

We begin by examining the root node. If the tree is null, the value we are searching for does not exist in the tree. Otherwise, if the value equals the root, the search is successful. If the value is less than the root, search the left subtree. Similarly, if it is greater than the root, search the right subtree. This process is repeated until the value is found or the indicated subtree is null. If the searched value is not found before a null subtree is reached, then the item must not be present in the tree

Page 9: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Insertion in a Binary Search Tree

True or false

Insertion Insertion begins as a search would

begin

Page 10: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Insertion

Insertion Insertion begins as a search would begin;

if the root is not equal to the value, we search the left or right subtrees as before. Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value. In other words, we examine the root and recursively insert the new node to the left subtree if the new value is less than the root, or the right subtree if the new value is greater than or equal to the root.

Page 11: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Example in C++ (insertion)

Page 12: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Deletion

What are the ‘rules’ for deleting. a) Leafb) Node with one child c) Node with two children

Page 13: Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.

Deletion

Deleting a leaf (leaf with no children is easy –just remove it)

Deleting a node with one child –how?

Deleting a node with two children –how do we do it?