CS 261 – Data Structures Binary Search Trees.

Slides:



Advertisements
Similar presentations
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
Advertisements

Special Purpose Trees: Tries and Height Balanced Trees CS 400/600 – Data Structures.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees Binary Search Trees.  Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the.
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS261 Data Structures Trees Introduction and Applications.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
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.
CS261 Data Structures Binary Search Trees II Bag Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
CS 367 Introduction to Data Structures Lecture 8.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
CS261 Data Structures Binary Search Trees Concepts.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CS 261 – RECITATION 5 SPRING OREGON STATE UNIVERSITY.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Binary Search Trees Chapter 7 Objectives
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Algorithms for Information Processing
CSE 373 Binary search trees; tree height and balance
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Trees.
CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
COMP 103 Binary Search Trees.
Binary Search Trees.
Binary Trees, Binary Search Trees
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
CSE 373: Data Structures and Algorithms
Binary Search Trees.
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSE 373 Data Structures and Algorithms
CSC 143 Binary Search Trees.
Basic Data Structures - Trees
Tree.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
CS 261 – Data Structures AVL Trees.
Presentation transcript:

CS 261 – Data Structures Binary Search Trees

Binary Tree: Useful Collection? Can we make a collection using the idea of a binary tree? How about starting with an implementation of a BAG Goal: make operations proportional to the length of a path, rather than the number of elements in the tree Implication  must know which side of a node a given value will/should be on First step  Binary Search Tree

Binary Search Tree Binary search trees are binary trees where every node’s object value is: Greater than all its descendents in the left subtree Less than or equal to all its descendents in the right subtree In-order traversal returns elements in sorted order If tree is reasonably full (well balanced), searching for an element is O(log n)

Binary Search Tree: Example Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur

Binary Search Tree (BST): Implementation struct BSTree { struct Node *root; int cnt; }; void initBSTree(struct BSTree *tree); void addBSTree(struct BSTree *tree, TYPE val); int containsBSTree(struct BSTree *tree, TYPE val); void removeBSTree(struct BSTree *tree, TYPE val); int sizeBSTree(struct BSTree *tree);

Binary Node: Reminder struct Node { TYPE val; struct Node *left; /* Left child. */ struct Node *rght; /* Right child. */ };

Bag Implementation: Contains Start at root At each node, compare value to node value: Return true if match If value is less than node value, go to left child (and repeat) If value is greater than node value, go to right child (and repeat) If node is null, return false Traverses a path from the root to the leaf Therefore, if tree is reasonably full (an important if), execution time is O( ?? )

BST: Contains/Find Example Agnes Alex Object to find Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur

Bag Implemention: Add Do the same type of traversal from root to leaf When you find a null value, create a new node Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur

Before first call to add BST: Add Example Aaron Alex Object to add Abner Angela Abigail Adela Alice Audrey Aaron Adam Agnes Allen Arthur “Aaron” should be added here Before first call to add

BST: Add Example Ariel Alex Abner Angela Abigail Adela Alice Audrey Next object to add Abner Angela Abigail Adela Alice Audrey Aaron Adam Agnes Allen Arthur “Ariel” should be added here Ariel After first call to add

Useful Trick A useful trick (adapted from the functional programming world): Secondary routine that returns tree with the value inserted Node addNode(Node current, TYPE value) if current is null then return new Node with value otherwise if value < Node.value left child = addNode(left child, value) else right child = addNode(right child, value) return current node

Add: Calls Utility Routine void add(struct BSTree *tree, TYPE val) { tree->root = _addNode(tree->root, val); tree->cnt++; } What is the complexity? O( ?? )

Bag Implementation: Remove Remove is most complex  Leaves a “hole” What value should fill the hole? Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur

BST: Remove Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur How would you remove Abigail? Audrey? Angela?

Who fills the hole? Answer: the leftmost child of the right child (smallest element in right subtree) Try this on a few values Useful to have a couple of private inner routines: TYPE _leftmost(struct Node *cur) { ... /* Return value of leftmost child of current node. */ } struct Node *_removeLeftmost(struct Node *cur) { ... /* Return tree with leftmost child removed. */

BST: Remove Example Alex Abner Angela Abigail Adela Alice Audrey Adam Element to remove Alex Replace with: leftmost(rght) Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur Before call to remove

BST: Remove Example Alex Abner Arthur Abigail Adela Alice Audrey Adam Agnes Allen After call to remove

Special Case What if you don’t have a right child? Try removing “Audrey” Think about it Can just return left child Angela Alice Audrey Allen Arthur

Remove: Easy if you return a tree Node removeNode(Node current, TYPE value) if value = current.value if right child is null return left child else replace value with leftmost child of right child set right child to be removeLeftmost(right) else if value < current.value left child = removeNode(left child, value) else right child = removeNode(right child, value) return current node

Complexity Running down a path from root to leaf What is the complexity? O( ?? ) Questions?? Now the worksheet