16. Binary Search Tree

# 16. Binary Search Tree - BinarySearchTree 1...

This preview shows pages 1–6. Sign up to view the full content.

Binary Search Tree 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Binary Search Tree (BST)  - It is a hierarchical, nonlinear data structure.  - Its properties are given bellow:     - Each node has a value.     - A total order is defined on these node values.     - Left subtree of a node contains the values less than the node’s value.     - Right subtree of a node contains the values greater than or equal to        the node’s value. Example: Root R 8 3 12 1 6 9 14 2 10 Left Subtree Right Subtree
3 Searching a node with a specific value from BST Rules Examine the root R. If the searching value equals the root, then the value exists in the tree. If it is less than the root, then it must be in the left subtree. If it is greater than the root, then it must be in the right subtree. This process continues until the searching value is found or a leaf node is reached without having that value where it would be. Algorithm Search_BST(Root, Left, Right, Key ) 1. Loc := Search(Root, Key, NULL) 2. If Loc = NULL then Print: “Item not in BST”. 3. Else Print: “Item is in BST”. 4. End. Here, Root = Root of BST Left = Left Subtree Right = Right Subtree Key = Searching Item

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Search(Node, Key, Par) 1. If Node = NULL then return NULL. 2. If Key < Node.Key then Search(Node.Left, Key, Node). 3. Else if Key > Node.Key then Search(Node.Right, Key, Node). 4. Else return Node Example: Searching Item:  12 Steps: 1. Search(Root, 12)                Search(AA,12) 2. Search(AA->Left,12)          Search(BB, 12) 3. Search(BB->Left, 12)         Search(DD, 12) 4. Return DD and Print: Item is in BST. 15 13 100 12 90 AA BB CC DD EE
5 Complexity of Searching a Node from BST  In Binary Search Tree, Depth = log 2 N  For balanced BST, Complexity of Searching=O(log 2 N)  [Average Case]   For unbalanced BST, Complexity of Searching=O(N)     [Worst Case]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern