Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Similar documents
CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Binary Trees, Binary Search Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Lecture 6: Analysis of Algorithms (CS )

Binary Trees. Recursive definition. Is this a binary tree?

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Binary search trees :

TREES. Trees - Introduction

Lecture: Analysis of Algorithms (CS )

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

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

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

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

Analysis of Algorithms

Search Trees. Undirected graph Directed graph Tree Binary search tree

SFU CMPT Lecture: Week 9

Binary Trees. Examples:

8. Binary Search Tree

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

CS102 Binary Search Trees

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Associate Professor Dr. Raed Ibraheem Hamed

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

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

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

12 Binary Search Tree

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

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

Binary Trees and Binary Search Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Binary search trees (BST) Binary search trees (BST)

CS350: Data Structures Binary Search Trees

Chapter 20: Binary Trees

Trees, Binary Trees, and Binary Search Trees

Binary Trees

CS 350 : Data Structures Binary Search Trees

Garbage Collection: recycling unused memory

1 A node in a tree. Trees. Datatypes ISC

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

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Binary Trees. Height 1

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

Friday Four Square! 4:15PM, Outside Gates

Binary Search Trees. See Section 11.1 of the text.

Data Structures. Dynamic Sets

Programming II (CS300)

Partha Sarathi Mandal

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

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Binary Search Trees. CSE2100 DS & Algorithms 1

Trees. Truong Tuan Anh CSE-HCMUT

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Trees. CSE 373 Data Structures

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

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

Self-Balancing Search Trees. Chapter 11

Search Trees: BSTs and B-Trees

Successor/Predecessor Rules in Binary Trees

Introduction to Computers and Programming. Concept Question

Principles of Computer Science

Algorithms. AVL Tree

Data Structures and Algorithms for Engineers

CSCI Trees. Mark Redekopp David Kempe

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

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

Lecture 7. Binary Search Trees / AVL Trees

Dictionaries. Priority Queues

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

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

CMSC351 - Fall 2014, Homework #2

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

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Properties of red-black trees

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

BBM 201 Data structures

March 20/2003 Jayakanth Srinivasan,

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

Multi-Way Search Tree

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

Algorithms. Deleting from Red-Black Trees B-Trees

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

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)


B-Trees. Version of October 2, B-Trees Version of October 2, / 22

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

Data Structures and Algorithms

COMP Analysis of Algorithms & Data Structures

We assume uniform hashing (UH):

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

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

CMSC 441: Algorithms. Hillol Kargupta, Professor.

9. Heap : Priority Queue

Searching: Introduction

Transcription:

0/2/ Preview Binary Tree Tree Binary Tree Property functions In-order walk Pre-order walk Post-order walk Search Tree Insert an element to the Tree Delete an element form the Tree A binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left child and right child. Binary trees are commonly used to implement binary search trees and heaps. 2 Binary Tree Binary Tree Balanced Binary Tree Unbalanced Binary Tree Binary Tree Ex) Prove that a binary tree of height n has at most 2 n leaves Lets L (n) denote a maximum number of leaves of binary tree of height n. Then we need to show that L (n) 2 n By induction) Base) n = 0, a tree with height 0 is a tree with only root. Number of leave is one root itself. Left side is 2 0 =. So it is true. Induction step) Induction hypothesis: L (k) 2 k is true. Now we need to prove L(k+) 2 k+ Observation) we can construct a binary tree T k+ with height k+ from a binary tree T k with height k by adding two (left and right children) node for each leaf. From the induction hypothesis For T k : L (k) 2 k For T k+ : L(k+) 2 k+ Binary search tree a binary tree with binary-search-tree property. Binary-Search-Tree Property Let x be a node in a binary search tree. If y is a node in the left subtree of x then key(y) key(x). If y is a node in the right subtree of x, then key(x) key(y). Binary-Search-Tree with discrete data Let x be a node in a binary search tree. If y is a node in the left subtree of x then key(y)<key(x). If y is a node in the right subtree of x, then key(x) <key(y). 6

0/2/ 2 Unbalanced Binary Tree 6 8 8 Operations Inorder walk Preorder walk Postorder walk Search Tree Insert a element to the Tree Delete a element form the Tree (Inorder, Preorder, Postorder) The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. Inorder_Tree_Walk(x) If x NIL 2 Inorder_Tree_Walk(x leftchild); print x key Inorder_Tree_Walk(x rightchild); Running Time T(n) = 2T(n/2) + = O(n) 0 (Inorder, Preorder, Postorder) (Inorder, Preorder, Postorder) Preorder_Tree_Walk(x) If x NIL 2 Print x key; Preorder_Tree_Walk(x leftchild); Preorder_Tree_Walk(x rightchild); Inorder_Tree_Walk(x) If x NIL 2 Inorder_Tree_Walk(x leftchild); print x key Inorder_Tree_Walk(x rightchild); Postorder_Tree_Walk(x) If x NIL 2 Postorder_Tree_Walk(x leftchild); Postorder_Tree_Walk(x rightchild); Print x key; 2 8 6 6 2 2

0/2/ (Inorder, Preorder, Postorder) Preorder_Tree_Walk(x) If x NIL 2 print x key; Preorder_Tree_Walk(x leftchild); Preorder_Tree_Walk(x rightchild); (Inorder, Preorder, Postorder) Postorder_Tree_Walk(x) If x NIL 2 Postorder_Tree_Walk(x leftchild); Postorder_Tree_Walk(x rightchild); Print x key; 6 6 2 8 6 2 6 8 Searching a binary search tree for a specific value can be a recursive or iterative process. We begin by examining the root node. If the tree is null, the value we are searching for does not exist in the tree. Otherwise, if the value equals the root, the search is successful. If the value is less than the root, search the left sub-tree. If it is greater than the root, search the right subtree. This process is repeated until the value is found or the indicated sub-tree is null. If the searched value is not found before a null sub-tree is reached, then the item must not be present in the tree. Searching Input: pointer to the root of the tree and a key k, Output: returns a pointer to a node with key k if one exists; otherwise return NIL. Tree_Search (x, k) if x == NIL or k == x key return x; 2 if k < x key return Tree_Search (x leftchild, k); else return Tree_Search(x rightchild, k); T(n) = T(n/2) + = O(log n) if a binary tree is balanced T(n)= T(n-) + = O(n) if a binary tree is not balanced 6 Search (x, ) = or > or <? x 6 Search (x, ) x = or > or <? 0 8 2 0 2 2 8

0/2/ Search (x, ) Searching a binary search tree for a specific value can be a recursive or iterative process. Following shows the iterative version of tree search x = or > or <? 0 = or > or <? 2 Tree_Search_II(x, k) y = x; //better not modify pointer x 2 while y NIL and k y key if k < y key y = y leftchild; else 6 y = y rightchild; return y; Search (x, ) = or > or <? x = or > or <? 6 Search (x, ) x 6 = or > or <? 0 8 2 0 8 2 2 2 2 22 Search (x, ) x 6 0 2 = 2 or > 2 or < 2? = or > or <? 8 2 (Minimum Element) Minimum An element in a binary search tree whose key is a minimum can always be found by leftmost child. Tree_Minimum(x) 0 y = x; while y leftchild NIL 2 y = y leftchild; return y; T(n) = O(log 2 n) or possible worst case O(n) 2 2

0/2/ (Maximum Element) Maximum An element in a binary search tree whose key is a maximum can always be founded by rightmost child. Tree_Maximum(x) 0 y = x; while y rightchild NIL 2 y = y rightchild; return y; T(n) = O( log 2 n) (Successor of a Node) The successor of a node x is the node with the smallest key greater than x->key We need to concern two cases. If the right subtree of x is not empty, then the successor of x is the minimum of right subtree. 2. If the right subtree of x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x. 2 26 (Successor of a Node) (Successor of a Node) 6 2 Ex) successor of node is (min of right subtree) Ex) successor of node is. 8 Tree_Successor (x) if x rightchild NIL return Tree_Minimum(x rightchild); 2 else y = x parent; // if x is y s leftchild then y is successor of x while y NIL and x ==y rightchild x = y; 6 y = y parent; return y; 2 28 (Predecessor of a Node) The predecessor of a node x is the node with the largest key smaller than key(x) We need to concern two cases. If the left subtree of x is not empty, then the predecessor of x is the maximum of right subtree. 2. If the left subtree of x is empty and x has a predecessor y, then y is the lowest ancestor of x whose right child is also an ancestor of x. (Predecessor of a Node) 2 6 8 Ex) Predecessor of node is (Maximum of left subtree) Ex) Predecessor of node is. 2 0

0/2/ (Predecessor of a Node) Tree_Predecessor (x) if x leftchild NIL return Tree_Maximum(x leftchild) 2 else y = x parent // if x is y s rightchild then y is predecessor of x while y NIL and x ==y leftchild x = y 6 y = y parent return y Insertion begins as a search would begin; if the root is not NIL, we search the left or right sub-trees as before. Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value. 2 // T is point to root of binary tree z is point to new node Tree_Insert (T, z) y = NIL; (Insert a node) 2 x = T; //while loop find out the location for new node Tree_Insert while (T, x z) NIL y = NIL x = Root (T) While x NIL y = x; if z key < x key 6 y = x x = x leftchild; 8 If z key < else x key x = x leftchild x = x rightchild; Else z parent = y; x = x rightchild 0 if y = NIL; // means there is any node in the BST z parent y T = z; //new node become root if y = NIL 2 else if z key < y key root(t) = z y leftchild = z; // insert new node as a left child o y else if z else key < y key y leftchild y = z rightchild = z; //insert new node as a right child of y else y rightchild = z 6 2< or 2 >? 6 6 6 6

0/2/ 8< or 8 >? 2 2 8 6 8 6 8 < or >? 6 6 0 <8 or >8? 6 6 2

0/2/ < or >? <8 or >8? 6 6 < or >? 6 6 6 < or >? <2 or >2? 6 6 8 8

0/2/ < or >? 6 6 0 <2 or >2? 6 6 2 6< or 6 >? 6<8 or 6 >8? 6 6

0/2/ 6< or 6 >? 6 6 6 < or >? <2 or >2? 6 6 8 < or >? 6 6 60 0

0/2/ < or >? <2 or >2? 6 6 6 62 < or >? 6 6 6 6 (Delete a Node) There are three cases needed to be concern in binary tree deletion.. A deleting node has no children ( deleting node is a leaf) 2. A deleting node has one children.. A deleting node has two children. a) If successor of deleting node is a leaf b) If successor of deleting node is not a leaf Delete Leaf Node 6 z = y 0 8 2 2 6 66

0/2/ 6 z = y Delete a Node with one child 6 Delete a Node with one child 0 8 2 0 8 2 z = y 2 2 6 68 z 6 Delete node with two children z 0 6 Delete node with two children 0 y 8 2 0 y 8 2 Successor of node is a leaf node Copy contents of successor in node Successor of node is a leaf node Copy contents of successor in node 6 0 z 6 Delete node with two children z 0 6 Delete node with two children y 0 8 2 y 0 8 2 Successor of node is not a leaf node 2 Copy contents of successor in node Successor of node is not a leaf node 2 Copy contents of successor in node 2 2

0/2/ z 6 Delete node with two children y 6 6 Delete node with two children 0 8 2 0 8 2 Successor of node is not a leaf node 2 Copy contents of successor in node Successor of node is not a leaf node 2 Copy contents of successor in node Tree_Delete (T, z) if z leftchild == NIL or z rightchild == NIL y = z (Delete else a node) y = Tree_Successor(z) // y is pointing a node which will be deleted if y leftchild NIL x = y leftchild else x = y rightchild // x might be pointing NIL if x NIL x parent = y parent if y parent ==NIL //y is pointing root since root does not have parent T root = x else if y == y parent left //if y is left child of it parent y parent leftchild = x else // if y is right child of it s parent y parent rightchild = x if y z z key =y key delete y