The Binary Search Tree Data Structure

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

Trees Types and Operations
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Computer Science C++ High School Level By Guillermo Moreno.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
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.
Binary Search Trees Chapter 6.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
CSCS-200 Data Structure and Algorithms Lecture
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture No.15 Data Structures Dr. Sohail Aslam
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Chapter 20: Binary Trees.
The Binary Tree Data Structure
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.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Node Removal From BST Source:
Find in a linked list? first last 7  4  3  8 NULL
AVL Trees: AVL Trees: Balanced binary search tree
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
Lecture No.16 Data Structures Dr. Sohail Aslam.
Binary Search Trees (BSTs)
The Heap Data Structure
Binary Search Trees (BSTs)
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
Chapter 20: Binary Trees.
Building Java Programs
Basic Data Structures - Trees
Binary Search Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees Trees.
Binary Search Tree.
Binary Search Trees (BSTs)
Presentation transcript:

The Binary Search Tree Data Structure Mugurel Ionuț Andreica Spring 2012

The Properties of a Binary Search Tree A binary search tree is a binary tree with some extra properties This implies that it maintains the same information as a binary tree (left son, right son, parent, pointer to some useful information) Extra properties: The elements (useful information) stored in the tree nodes are comparable The elements stored in the left sub-tree of a node p are ≤ the element stored by p The elements stored in the right sub-tree of a node p are > the element stored by p Faster search More complicated removal (because the property of the BST must be maintained)

Binary Search Tree - example

Binary Search Tree – C++ code template<typename T> class BSTNode { public: BSTNode<T> *root, *left_son, *right_son, *parent; T *pinfo; BSTNode() { left_son = right_son = NULL; root = this; pinfo = NULL; } void setInfo(T info) { pinfo = new T; *pinfo = info; void insertInfo(T x) { if (pinfo == NULL) setInfo(x); else insert_rec(x); void insert_rec(T x) { int next_son; if (x <= (*pinfo)) next_son = 0; else next_son = 1; if (next_son == 0) { // left son if (left_son == NULL) { left_son = new BSTNode<T>; left_son->pinfo = new T; *(left_son->pinfo) = x; left_son->left_son = left_son->right_son = NULL; left_son->parent = this; left_son->root = root; } else left_son->insert_rec(x); } else { // right son if (right_son == NULL) { right_son = new BSTNode<T>; right_son->pinfo = new T; *(right_son->pinfo) = x; right_son->left_son = right_son->right_son = NULL; right_son->parent = this; right_son->root = root; right_son->insert_rec(x); }

Binary Search Tree – C++ code (cont.) BSTNode<T>* findInfo(T x) { BSTNode<T> *rez; if (pinfo == NULL) return NULL; if ((*pinfo) == x) return this; if (x <= (*pinfo)) { if (left_son != NULL) return left_son->findInfo(x); else } else { if (right_son != NULL) return right_son->findInfo(x); } void removeInfo(T x) { BSTNode<T> *t = findInfo(x); if (t != NULL) t->remove(); void remove() { BSTNode<T> *p; T *paux; if (left_son == NULL && right_son == NULL) { if (parent == NULL) { // this == root delete this->pinfo; root->pinfo = NULL; } else { if (parent->left_son == this) parent->left_son = NULL; else parent->right_son = NULL; delete this; } if (left_son != NULL) { p = left_son; while (p->right_son != NULL) p = p->right_son; } else { // right_son != NULL p = right_son; while (p->left_son != NULL) p = p->left_son;

Binary Search Tree – C++ code (cont.) paux = p->pinfo; p->pinfo = this->pinfo; this->pinfo = paux; p->remove(); } void inOrderTraversal() { if (left_son != NULL) left_son->inOrderTraversal(); printf("%d\n", *pinfo); // we should use the correct format // for printing type T values if (right_son != NULL) right_son->inOrderTraversal(); }; int main() { srand(7290); BSTNode<int> *r = new BSTNode<int>; r->insertInfo(6); r->insertInfo(8); r->insertInfo(1); r->insertInfo(9); r->insertInfo(10); r->insertInfo(4); r->insertInfo(13); r->insertInfo(1); r->insertInfo(12); r->inOrderTraversal(); printf("%d\n", r->findInfo(100)); printf("%d\n", r->findInfo(1)); printf("%d\n", r->findInfo(12)); printf("%d\n", r->findInfo(8)); printf("%d\n", r->findInfo(10)); printf("%d\n", r->findInfo(4)); (r->findInfo(6))->remove(); r->removeInfo(9); return 0; }