BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Similar documents
Programming II (CS300)

Chapter 20: Binary Trees

TREES. Trees - Introduction

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees, Binary Search Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees. (Trees) Data Structures and Programming Spring / 28

Algorithms. Deleting from Red-Black Trees B-Trees

CS24 Week 8 Lecture 1

Binary Trees

BBM 201 Data structures

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Why Do We Need Trees?

Programming Languages and Techniques (CIS120)

We have the pointers reference the next node in an inorder traversal; called threads

Associate Professor Dr. Raed Ibraheem Hamed

INF2220: algorithms and data structures Series 1

Algorithms and Data Structures

CE 221 Data Structures and Algorithms

Binary Trees. Height 1

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Data Structures And Algorithms

ITI Introduction to Computing II

Data Structure and Algorithm Homework #3

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS 171: Introduction to Computer Science II. Binary Search Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Binary Tree Applications

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Cpt S 122 Data Structures. Data Structures Trees

ITI Introduction to Computing II

Trees. CSE 373 Data Structures

Trees. Eric McCreath

CSI33 Data Structures

Binary Trees. Examples:

Data Structures and Algorithms

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Trees. Truong Tuan Anh CSE-HCMUT

CS302 - Data Structures using C++

Module 8: Binary trees

Binary Search Trees. See Section 11.1 of the text.

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Data Structures and Algorithms

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Unit III - Tree TREES

Binary Trees: Practice Problems

Objectives. In this session, you will learn to:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

IX. Binary Trees (Chapter 10)

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Principles of Computer Science

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Multi-Way Search Tree

m-way Search Tree: Empty, or if not empty then: Each internal node has q children and q 1 elements, for 2 q m.

CS350: Data Structures Binary Search Trees

Topic 14. The BinaryTree ADT

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

CSE 230 Intermediate Programming in C and C++ Binary Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree

Trees, Binary Trees, and Binary Search Trees

Module 9: Binary trees

Partha Sarathi Mandal

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

A set of nodes (or vertices) with a single starting point

Tree traversals and binary trees

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Data Structures and Algorithms for Engineers

Binary Search Tree (3A) Young Won Lim 6/2/18

CS F-11 B-Trees 1

Chapter 5. Binary Trees

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Friday Four Square! 4:15PM, Outside Gates

CS 350 : Data Structures Binary Search Trees


Garbage Collection: recycling unused memory

Binary Trees and Binary Search Trees

CS350: Data Structures Tree Traversal

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Name CPTR246 Spring '17 (100 total points) Exam 3

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

March 20/2003 Jayakanth Srinivasan,

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Binary Trees Fall 2018 Margaret Reid-Miller

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CSE 214 Computer Science II Introduction to Tree

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Transcription:

Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012

Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root node 2. Each node has at most two child nodes (a left and a right child) 3. Nodes are organized by the Binary Search property: Every node is ordered by some key data field(s) For every node in the tree, its key is greater than its left child s key and less than its right child s key root 25 15 10 22 4 12 18 24 50 35 70 31 44 66 90

Some BST Terminology 1. The Root node is the top node in the hierarchy 2. A Child node has exactly one Parent node, a Parent node has at most two child nodes, Sibling nodes share the same Parent node (ex. node 22 is a child of node 15) 3. A Leaf node has no child nodes, an Interior node has at least one child node (ex. 18 is a leaf node) 4. Every node in the BST is a Subtree of the BST rooted at that node root 25 subtree (a BST 15 50 w/root 50) 10 22 4 12 18 24 35 70 31 44 66 90

BST Operations bool find(t value)/ lookup. void insert(t value)/ add. bool delete(t value). void print()/ traverse;

BST Node Structure template<class T> class Node { T data; // the node content Node* left; // pointer to the left child Node* right; // pointer to the right child }

Find a Node into the BST Use the search key to direct a recursive binary search for a matching node 1. Start at the root node as current node 2. If the search key s value matches the current node s key then found a match 3. If search key s value is greater than current node s 1. If the current node has a right child, search right 2. Else, no matching node in the tree 4. If search key is less than the current node s 1. If the current node has a left child, search left 2. Else, no matching node in the tree

Example: search for 45 in the tree (key fields are show in node rather than in separate obj ref to by data field): 1. start at the root, 45 is greater than 25, search in right subtree 2. 45 is less than 50, search in 50 s left subtree 3. 45 is greater than 35, search in 35 s right subtree 4. 45 is greater than 44, but 44 has no right subtree so 45 is not in the BST root (1) 25 (2) 15 10 22 4 12 18 24 (3) 50 35 (4) 70 31 44 66 90

Insert Node into the BST Always insert new node as leaf node 2. Start at root node as current node 3. If new node s key < current s key 1. If current node has a left child, search left 2. Else add new node as current s left child 4. If new node s key > current s key 1. If current node has a right child, search right 2. Else add new node as current s right child

Example: insert 60 in the tree: 1. start at the root, 60 is greater than 25, search in right subtree 2. 60 is greater than 50, search in 50 s right subtree 3. 60 is less than 70, search in 70 s left subtree 4. 60 is less than 66, add 60 as 66 s left child 15 root (1) 25 50 (2) (3) 10 22 4 12 18 24 35 70 (4) 31 44 66 90 60

Traversals Visit every node in the tree and perform some operation on it (ex) print out the data fields of each node Three steps to a traversal 1. Visit the current node 2. Traverse its left subtree 3. Traverse its right subtree The order in which you perform these three steps results in different traversal orders: Pre-order traversal: (1) (2) (3) In-order traversal: (2) (1) (3) Post-order traversal: (2) (3) (1)

Traversal Code public void InOrder(bst_node root) { // stop the recursion: if(root == null) { return; } // recursively traverse left subtree: InOrder(root.leftChild()); // visit the current node: Visit(root); // recursively traverse right subtree: InOrder(root.rightChild()); }

// in main: a call to InOrder passing root InOrder(root); // The call stack after the first few // calls to InOrder(root.leftChild()): Call Stack (drawn upside down): main: root calls InOrder: InOrder: root root 15 25 InOrder: root 10 22 InOrder: root 4 12 18 24

Traversal Examples InOrder(root) visits nodes in the following order: 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90 A Pre-order traversal visits nodes in the following order: 25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90 A Post-order traversal visits nodes in the following order: 4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25 root 15 10 22 4 12 18 24 25 50 35 70 31 44 66 90

Deleting a node from a BST There are three cases: 1. The node to be deleted is a leaf node. 2. The node to be deleted has one non-empty child. 3. The node to be deleted has two non-empty children.

Deleting a node from a BST Case 1: deleting a leaf node

Deleting a node from a BST Case 2: deleting a node with single child

Deleting a node from a BST Case 3: deleting a node with two children DELETION BY COPYING: METHOD#1 Copy the minimum key in the right subtree of x to the node x, then delete the one-child or leaf-node with this minimum key.

Deleting a node from a BST Case 3: deleting a node with two children DELETION BY COPYING: METHOD#1 Copy the maximum key in the left subtree of x to the node x, then delete the one-child or leaf-node with this maximum key.