Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
BST Search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is.
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees part 2.. Full Binary Trees A BC GDE F Full binary tree A binary tree is full if all the internal nodes (nodes other than leaves) has two children.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees … From
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Fundamentals of Algorithms MCS - 2 Lecture # 17. 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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
ADT description Implementations
Cinda Heeren / Geoffrey Tien
Week 6 - Wednesday CS221.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
CMPT 225 Binary Search Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

Binary Search Trees

 Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary search trees  Implement the TreeSort algorithm October 2004John Edgar2

 A set of nodes (or vertices) with a single starting point  called the root  Each node is connected by an edge to another node  A tree is a connected graph  There is a path to every node in the tree  A tree has one fewer edges than the number of nodes October 2004John Edgar4

October 2004John Edgar5 yes! NO! All the nodes are not connected NO! There is an extra edge (5 nodes and 5 edges) yes! (but not a binary tree) yes! (it’s actually the same graph as the blue one)

A BCD GEF NNode v is said to be a child of u, and u the parent of v if TThere is an edge between the nodes u and v, and uu is above v in the tree, TThis relationship can be generalized EE and F are descendants of A DD and A are ancestors of G BB, C and D are siblings FF and G are? October 2004John Edgar6 root edge parent of B, C, D

 A leaf is a node with no children  A path is a sequence of nodes v 1 … v n  where v i is a parent of v i+1 (1  i  n-1)  A subtree is any node in the tree along with all of its descendants  A binary tree is a tree with at most two children per node  The children are referred to as left and right  We can also refer to left and right subtrees October 2004John Edgar7

October 2004John Edgar8 C A B C D GEF EFGDGA leaves: C,E,F,G path from A to D to G subtree rooted at B

October 2004John Edgar9 A BC GDE left subtree of A H IJ F right subtree of C right child of A

 The height of a node v is the length of the longest path from v to a leaf  The height of the tree is the height of the root  The depth of a node v is the length of the path from v to the root  This is also referred to as the level of a node  Note that there is a slightly different formulation of the height of a tree  Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root) October 2004John Edgar10

October 2004John Edgar11 A BC GDE H IJ F A B E height of node B is ? height of the tree is ? depth of node E is ? level 1 level 2 level

October 2004John Edgar13 yes! yes! However, these trees are not “beautiful” (for some applications)

 A binary tree is perfect, if  No node has only one child  And all the leaves have the same depth  A perfect binary tree of height h has how many nodes?  2 h+1 – 1 nodes, of which 2 h are leaves October 2004John Edgar14 A BC GDE F

October 2004John Edgar Each level doubles the number of nodes Level 1 has 2 nodes (2 1 ) Level 2 has 4 nodes (2 2 ) or 2 times the number in Level 1 Therefore a tree with h levels has 2 h+1 - 1nodes The root level has 1 node the bottom level has 2 h nodes, that is, just over ½ the nodes are leaves

 A binary tree is complete if  The leaves are on at most two different levels,  The second to bottom level is completely filled in, and  The leaves on the bottom level are as far to the left as possible  Perfect trees are also complete October 2004John Edgar16 A BC DE F

 A binary tree is balanced if  Leaves are all about the same distance from the root  The exact specification varies  Sometimes trees are balanced by comparing the height of nodes  e.g. the height of a node’s right subtree is at most one different from the height of its left subtree  Sometimes a tree's height is compared to the number of nodes  e.g. red-black trees October 2004John Edgar17

October 2004John Edgar18 A BC FDE A BC FD E G

October 2004John Edgar19 A B CD A BC E D F

 A traversal algorithm for a binary tree visits each node in the tree  Typically, it will do something while visiting each node!  Traversal algorithms are naturally recursive  There are three traversal methods  Inorder  Preorder  Postorder October 2004John Edgar21

// InOrder traversal algorithm void inOrder(Node *n) { if (n != 0) { inOrder(n->leftChild); visit(n); inOrder(n->rightChild); } October 2004John Edgar22 C++

InOrder Traversal October 2004John Edgar23 A BC FDE

// PreOrder traversal algorithm void preOrder(Node *n) { if (n != 0) { visit(n); preOrder(n->leftChild); preOrder(n->rightChild); } October 2004John Edgar24 C++

October 2004John Edgar visit(n) preOrder(n->leftChild) preOrder(n->rightChild) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r)

// PostOrder traversal algorithm void postOrder(Node *n) { if (n != 0) { postOrder(n->leftChild); postOrder(n->rightChild); visit(n); } October 2004John Edgar26 C++

October 2004John Edgar postOrder(n->leftChild) postOrder(n->rightChild) visit(n) postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit

 The binary tree can be implemented using a number of data structures  Reference structures (similar to linked lists)  Arrays  We will look at three implementations  Binary search trees (reference / pointers)  Red – black trees (reference / pointers)  Heap (arrays) October 2004John Edgar29

 Consider maintaining data in some order  The data is to be frequently searched on the sort key e.g. a dictionary  Possible solutions might be:  A sorted array ▪Access in O(logn) using binary search ▪Insertion and deletion in linear time  An ordered linked list ▪Access, insertion and deletion in linear time  Neither of these is efficient October 2004John Edgar30

 The data structure should be able to perform all these operations efficiently  Create an empty dictionary  Insert  Delete  Look up  The insert, delete and look up operations should be performed in at most O(logn) time October 2004John Edgar31

 A binary search tree (BST) is a binary tree with a special property  For all nodes in the tree: ▪All nodes in a left subtree have labels less than the label of the node ▪All nodes in a right subtree have labels greater than or equal to the label of the node  Binary search trees are fully ordered October 2004John Edgar32

October 2004John Edgar

October 2004John Edgar inOrder(n->leftChild) visit(n) inOrder(n->rightChild) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) inOrder(l) visit inOrder(r) An inorder traversal retrieves the data in sorted order

 Binary search trees can be implemented using a reference structure  Tree nodes contain data and two pointers to nodes October 2004John Edgar35 Node *leftChildNode *rightChilddata pointers to Nodes data to be stored in the tree

 To find a value in a BST search from the root node:  If the target is less than the value in the node search its left subtree  If the target is greater than the value in the node search its right subtree  Otherwise return true, or return data, etc.  How many comparisons?  One for each node on the path  Worst case: height of the tree + 1 October 2004John Edgar36

 The BST property must hold after insertion  Therefore the new node must be inserted in the correct position  This position is found by performing a search  If the search ends at the (null) left child of a node make its left child refer to the new node  If the search ends at the right child of a node make its right child refer to the new node  The cost is about the same as the cost for the search algorithm, O(height) October 2004John Edgar37

October 2004John Edgar insert 43 create new node find position insert new node 43

 After deletion the BST property must hold  Deletion is not as straightforward as search or insertion  So much so that sometimes it is not even implemented!  Deleted nodes are marked as deleted in some way  There are a number of different cases that must be considered October 2004John Edgar39

 The node to be deleted has no children  The node to be deleted has one child  The node to be deleted has two children October 2004John Edgar40

 The node to be deleted has no children  Remove it (assigning null to its parent’s reference) October 2004John Edgar41

October 2004John Edgar delete

 The node to be deleted has one child  Replace the node with its subtree October 2004John Edgar43

October 2004John Edgar delete 79 replace with subtree

October 2004John Edgar delete 79 after deletion

 The node to be deleted has two children  Replace the node with its successor, the left most node of its right subtree ▪It is also possible to replace the node with its predecessor, the right most node of its left subtree  If that node has a child (and it can have at most one child) attach it to the node’s parent ▪Why can a predecessor or successor have at most one child? October 2004John Edgar46

October 2004John Edgar delete 32 temp find successor and detach

October 2004John Edgar delete temp find successor attach target node’s children to successor

October 2004John Edgar delete temp - find successor - attach target’s children to successor - make successor child of target’s parent

October 2004John Edgar delete temp note: successor had no subtree

October 2004John Edgar delete 63 temp - find predecessor*: note it has a subtree *predecessor used instead of successor to show its location - an implementation would have to pick one or the other

October 2004John Edgar delete 63 temp - find predecessor - attach predecessor’s subtree to its parent

October 2004John Edgar delete temp - find predecessor - attach subtree - attach target’s children to predecessor

October 2004John Edgar delete temp - find predecessor - attach subtree - attach children - attach pre. to target’s parent

October 2004John Edgar delete 63 59

 The efficiency of BST operations depends on the height of the tree  All three operations (search, insert and delete) are O(height)  If the tree is complete the height is  log(n)   What if it isn’t complete? October 2004John Edgar56

IInsert 7 IInsert 4 IInsert 1 IInsert 9 IInsert 5 IIt’s a complete tree! October 2004John Edgar height =  log(5)  = 2

IInsert 9 IInsert 1 IInsert 7 IInsert 4 IInsert 5 IIt’s a linked list with a lot of extra pointers! October 2004John Edgar height = n – 1 = 4 = O(n)

 It would be ideal if a BST was always close to complete  i.e. balanced  How do we guarantee a balanced BST?  We have to make the insertion and deletion algorithms more complex ▪e.g. red – black trees. October 2004John Edgar59

 It is possible to sort an array using a binary search tree  Insert the array items into an empty tree  Write the data from the tree back into the array using an InOrder traversal  Running time = n*(insertion cost) + traversal  Insertion cost is O(h)  Traversal is O(n)  Total = O(n) * O(h) + O(n), i.e. O(n * h)  If the tree is balanced = O(n * log(n)) October 2004John Edgar60

Tree Quiz I  Write a recursive function to print the items in a BST in descending order October 2004John Edgar62 class Node { public: int data; Node *leftc; Node *rightc; };

Tree Quiz II  Write a recursive function to delete a BST stored in dynamic memory October 2004John Edgar63 class Node { public: int data; Node *leftc; Node *rightc; };

Summary  Trees  Terminology: paths, height, node relationships, …  Binary search trees  Traversal ▪Post-order, pre-order, in-order  Operations ▪Insert, delete, search  Balanced trees  Binary search tree operations are efficient for balanced trees October 2004John Edgar65

Readings  Carrano Ch. 10 October 2004John Edgar66