Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Binary Search Tree Traversal Methods

2 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.computer sciencetree data structurechildren binary search treesbinary heaps

3 Size? Height Root Leaves 9 2 4 1,4,7,13 Highlight below To reveal answers Note: The above tree is neither a sorted nor a balanced binary tree

4 Binary Search Tree In computer science, a binary search tree (BST) is a binary tree data structure which has the following properties:computer sciencebinary treedata structure  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.subtree  The right subtree of a node contains only values greater than or equal to the node's value.

5 Diagram of a Binary Search Tree Size? Depth Root Leaves 9 8 3 1,4,7,13 Highlight below To reveal answers

6 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. sorting algorithmssearch algorithmsin-order traversal

7 Which two programming processes are used when searching a Binary Search tree? Recursive Process or Iterative Process In an ordinary Binary tree, you Have three iterative Procedures that can Be used. Preorder Postorder inorder

8 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 treeroot node

9 Insertion in a Binary Search Tree  True or false  Insertion  Insertion begins as a search would begin

10 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.

11 Example in C++ (insertion)

12 Deletion What are the ‘rules’ for deleting. a)Leaf b)Node with one child c) Node with two children

13 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?


Download ppt "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."

Similar presentations


Ads by Google