Binary Search Tree

Binary Search Tree - CSE205 Concepts of Computer Science...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE205 Concepts of Computer Science and Data Structure Binary Search Trees Outline Binary Search Trees (BSTs) BST Implementation Balanced Binary Search Trees Binary Search Trees (BSTs) A search tree is a tree whose elements are organized to facilitate finding a particular element when needed A binary search tree is a binary tree that, for each node n the left subtree of n contains elements less than the element stored in n the right subtree of n contains elements greater than or equal to the element stored in n Binary Search Trees (BSTs) Binary Search Trees (BSTs) To determine if a particular value exists in a tree start at the root compare target to element at current node move left from current node if target is less than element in the current node move right from current node if target is greater than element in the current node We eventually find the target or encounter the end of a path (target is not found) Binary Search Trees (BSTs) The particular shape of a binary search tree depends on the order in which the elements are added to the tree The shape may also be dependant on any additional processing performed on the tree to reshape it Binary search trees can hold any type of data, so long as we have a way to determine relative ordering Objects implementing the Comparable interface provide such capability Adding an Element to a BST Process of adding an element is similar to finding an element New elements are added as leaf nodes Start at the root, follow path dictated by existing elements until you find no child in the desired direction Add the new element Adding an Element to a BST 77 24 82 58 17 40 97 Next to add: 77 24 58 82 17 40 97 Degenerate Tree Degenerate tree a grossly unbalanced tree, usually favoring one side Occurs if the input is completely or mostly sorted Essentially degrades into a linked list Removes the value of a binary search tree ability to exclude large amount of data with each comparison operation A Degenerate Tree Removing an Element from a BST Removing a target in a BST is not as simple as that for linear data structures After removing the element, the resulting tree must still be valid Three distinct situations must be considered when removing an element The node to remove is a leaf The node to remove has one child The node to remove has two children Removing an Element from a BST Removing an Element from a BST Dealing with the situations Node is a leaf: it can simply be deleted Node has one child: the deleted node is replaced by the child Node has two children: an appropriate node is found lower in the tree and used to replace the node Good choice: inorder successor (node that would follow the removed...
View Full Document

Page1 / 31

Binary Search Tree - CSE205 Concepts of Computer Science...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online