Binary Search Tree C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Search Trees Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
Department of Computer Science University of Maryland, College Park
Trees, Binary Trees, and Binary Search Trees COMP171.
Queue C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Tree C and Data Structures Baojian Hua
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
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.
Queue C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
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.
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.
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 Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Binary search trees. What’s on the menu? ADT DictionaryBST & Algorithms SimulatorComplexity.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
Lecture1 introductions and Tree Data Structures 11/12/20151.
Trees and beyond Tutorial #3 CPSC 261. Trees are just an example The next two weeks in labs we are playing with trees – Trees are interesting – Trees.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
Hash C and Data Structure Baojian Hua
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
1 Trees. Objectives To understand the concept of trees and the standard terminology used to describe them. To appreciate the recursive nature of a tree.
1 Balanced trees. Binary Search Tree: Balancing Problem nodeT *t=NULL; … InsertNode(&t, ‘G’); InsertNode(&t, ‘F’); InsertNode(&t, ‘E’); InsertNode(&t,
Lecture 9 Binary Trees Trees General Definition Terminology
Node Declaration and Example typedef struct nodeT { int key; struct nodeT *left, *right; } nodeT, *treeT;
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.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Trees.
Binary Search Trees.
Binary Search Tree Chapter 10.
Binary Search Trees.
Rick Mercer, Allison Obourn, Marty Stepp
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees (BSTs)
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Non-Linear Structures
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSC 143 Binary Search Trees.
Basic Data Structures - Trees
Binary Search Trees (BSTs)
Presentation transcript:

Binary Search Tree C and Data Structures Baojian Hua

Dictionary-like Data Structure A dictionary-like data structure contains a collection of tuple data: data = key is comparable and distinct supports these operations: new () insert (dict, k, v) lookup (dict, k) delete (dict, k) We ’ d discussed a linear list-based representation of dictionary, these slides study a strategy based on binary trees

Binary Search Tree A binary search tree is a binary tree satisfies: every node contain data=, and every key is unique all keys in left sub-tree is less than that in the root all keys in right sub-tree is greater than that in the root both the left and right sub-trees are also binary search trees

Example

Operations All the tree operations we ’ ve discussed also apply to binary search tree And BST also supports (as a general dictionary-like data structure): search (bst, key) insert (bst, key, value) delete (bst, key)

Abstract Data Types in C: Interface // in file “bst.h” #ifndef BST_H #define BST_H typedef struct bst *bst; bst new (); bst new2 (bst l, bst r, poly key, poly value); bst insert (bst t, poly key, poly value); poly lookup (bst t, poly key); void delete (bst t, poly key); … #endif

Implementation // in file “bst.c” #include “bst.h” struct bst { poly key; poly value; bst left; bst right; }; t leftkeyrightv

Operations: “ new ” bst new () { bst t = checkedMalloc (sizeof (*t)); t->key = NULL; t->value = NULL; t->left = NULL; t->right = NULL; return t; } t /\key/\value

How to search in a BST?--- lookup (bst, key)

How to search in a BST?--- lookup (bst, key) Top-down recursion: if bst if NULL, search fails else compare key with bst->key: if (key == bst->key), search succeeds, return t->value if (key key), then key may only appear in left sub-tree, so we continue search (bst->left, key) if (key > t->key), then key may only appear in right sub- tree, so we continue search(bst->right, key)

Operations: “ lookup ” poly lookup (bst t, poly key) { if (!t) return NULL; else if (key == t->key) // what’s “==“ ? return t->value; else if (k k) return lookup (t->left, key); else return lookup (t->right, key); }

Example: search

Example: search

Example: search

Example: search

Example: search

Example: search

Example: search

Example: search

An Iterative Version poly iterLookup (bst t, poly key) { if (!t) return NULL; bst p = t; while (p && p->key!=key) // what’s “!=“ ? { if (key key) p = p->left; else p = p->right; } return p; }

Adding New Bindings: insert (bst t, poly key, poly value) Main idea: search the tree, if these already exists a key k ’ ==k, then insertion fails else if tree t==NULL, return a new bst else search a proper position to insert the tuple What ’ s a proper position?

Example: insert

Example: insert searchParent

Operations: “ insert ” bst insert (bst t, poly key, poly value) { bst newTree = new2 (NULL, NULL, key, value); if (!t) return newTree; bst p = searchParent (t, key); if (!p || (p->left && key key) || (p->right && key>p->key)) error (“key already exists”); if (key key) p->left = newTree; else p->right = newTree; return t; }

How to Search a Parent? bst searchParent (bst t, poly key) { if (!t) error (“empty tree”); else if (key == t->key) return NULL; else if (key key) { if (!t->left || key==t->left->key) // “==”? return t; else return searchParent (t->left, key); } else {…} // symmetry case for right sub-tree }

A Functional Version of Insert bst insert (bst t, poly key, poly value) { if (search (t, k)) error (“key already exists”); else if (!t) return new2 (NULL, NULL, k, v); else if (key key) return new2 (insert (t->left, key, value), t->right, t->key, t->value); else return new2 (t->left, insert (t->right, key, value), t->key, t->value); }

Example: insert

Functional Style Always construct new data from older ones older ones left untouched and unchanged Support equational reasoning very well this time f(x)=5, then next time f(x)=5 much like mathematical functions (hence the name) Rely on garbage collection

remove case#1: leaf node

remove case#2: 1-degree node

remove case#3: 2-degree node

remove case#3: 2-degree node