Review of Chapter 5 張啟中. Threaded Binary Tree 利用 Binary Tree 節點中的 0-links ,指向中序的先行 者或後繼者,以方便中序追蹤。 Threading Rules  A 0 RightChild field at node p is.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Chapter 5. Trees Definition: A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root. –The remaining.
Department of Computer Science University of Maryland, College Park
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
BST Data Structure A BST node contains: A BST contains
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Review of Chapter 5 張啟中. Definition of Tree A tree is a finite set of one or more nodes such that A tree is a finite set of one or more nodes such that.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Review of Chapter 9 張啟中. Inheritance Hierarchy Min PQ Max PQ Min Heap Mergeable Min PQ DeapMin-Max Min-LeftistMin-SkewMinFHeap MinBHeap DEPQMax Heap Symmetric.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
Binary Search Trees Chapter 7 Objectives
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
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:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
 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)
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
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.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Heaps & Priority Queues
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
Binary Search Trees Chapter 7 Objectives
Fundamentals of Programming II Introduction to Trees
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Bohyung Han CSE, POSTECH
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Chapter 8 – Binary Search Tree
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
The DSW Algorithm The building block for tree transformations in this algorithm is the rotation There are two types of rotation, left and right, which.
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Topic 6: Binary Search Tree Data structure Operations
Chapter 20: Binary Trees.
Binary Search Tree (BST)
Binary Trees, Binary Search Trees
Presentation transcript:

Review of Chapter 5 張啟中

Threaded Binary Tree 利用 Binary Tree 節點中的 0-links ,指向中序的先行 者或後繼者,以方便中序追蹤。 Threading Rules  A 0 RightChild field at node p is replaced by a pointer to the node that would be visited after p when traversing the tree in inorder. That is, it is replaced by the inorder successor of p. ( 右樹指標指向該節點中序的後繼者 )  A 0 LeftChild link at node p is replaced by a pointer to the node that immediately precedes node p in inorder (i.e., it is replaced by the inorder predecessor of p). ( 左樹指標指向該節點中序的先行者 )

Threaded Binary Tree A HI B DE C GF Inorder sequence: H, D, I, B, E, A, F, C, G

f -f f Af f Bf f Df t Ht t It tEt f Bf fDf tEt Threaded Binary Tree with Head Node TRUEFALSE LeftThreadLeftChildRightChildRightThread data Head Node

Manipulation of Threaded Binary Tree Traversal (Inorder 不需 Stack) Insert a Node Delete a Node

Insert a Node to Threaded Binary Tree s r s r beforeafter

Insert a Node to Threaded Binary Tree r r s r s before after

Heaps Definition  A max (min) tree is a tree in which the key value in each node is no smaller (larger) than the key values in its children (if any).  A max heap is a complete binary tree that is also a max tree.  A min heap is a complete binary tree that is also a min tree. We can use the max heap to implement the priority Queues.

Priority Queues A data structure supports the below two operations is called max (min) priority queue.  In a priority queue, the element to be deleted is the one with highest (or lowest) priority.  An element with arbitrary priority can be inserted into the queue according to its priority.

Priority Queues 的運用 Suppose a server that serve multiple users. Each user may request different amount of server time. A priority queue is used to always select the request with the smallest time. Hence, any new user’s request is put into the priority queue. This is the min priority queue. If each user needs the same amount of time but willing to pay more money to obtain the service quicker, then this is max priority queue.

Representation of The Priority Queues Unorder Linear List  Addition complexity: O(1)  Deletion complexity: O(n) Chain  Addition complexity: O(1)  Deletion complexity: O(n) Ordered List  Addition complexity: O(n)  Deletion complexity: O(1)

Max Heap Examples

Manipulation of The Heap Create of an empty heap Insertion of a new element into the heap. Deletion of the largest element from the heap Please see book p286 ADT 5.2

Insertion into a Max Heap O(logn)

Insertion into a Max Heap 請自己練習

Deletion from a Max Heap 請同學自己繼續練習刪除 15 O(logn)

Binary Search Tree Definition  A binary serach tree is a binary tree. It may be empty. If it is not empty then it satisfies the following properties: Every element has a key and no two elements have the same key (i.e., the keys are distinct) The keys (if any) in the left subtree are smaller than the key in the root. The keys (if any) in the right subtree are larger than the key in the root. The left and right subtrees are also binary search trees.

Binary Search Tree Examples Not binary search tree Binary search trees Yes!

Manipulation of The Binary Search Tree Searching a Binary Search Tree Insertion into a Binary Search Tree Deletion from a Binary Search Tree Joining and Splitting Binary Search Tree

Searching A Binary Search Tree If the root is 0, then this is an empty tree. No search is needed. If the root is not 0, compare the x with the key of root.  If x equals to the key of the root, then it’s done.  If x is less than the key of the root, then only need to search the left tree.  If x larger than the key of the root, only the right subtree is to be searched.

Insertion into a Binary Search Tree

Deletion from a Binary Search Tree Delete a leaf node  A leaf node which is a right child of its parent  A leaf node which is a left child of its parent Delete a non-leaf node  A node that has one child  A node that has two children Replaced by the largest element in its left subtree, or Replaced by the smallest element in its right subtree Again, the delete function has complexity of O(h)

Deletion from a Binary Search Tree

Deletion from a Binary Search Tree

Deletion from a Binary Search Tree

Joining and Splitting Binary Trees C.ThreeWayJoin(A, x, B)  Creates a binary search tree C that consists of binary search tree A, B, and element x. C.TwoWayJoin(A, B)  Joins two binary search trees A and B to obtain a single binary search tree C. A.Split(i, B, x, C)  Binary search tree A splits into three parts: B (a binary search tree that contains all elements of A that have key less than i); if A contains a key i than this element is copied into x and a pointer to x returned; C is a binary search tree that contains all records of A that have key larger than i

ThreeWayJoin(A, x, B) ABx

C.TwoWayJoin(A, B) A B 80 84

A.Split(i, B, x, C) A i = 30 B C x

A.Split(i, B, x, C) A i = 80 B C Z Y 30 L t t 5 2 L t x L R R