Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

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.
Trees Types and Operations
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
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:
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Binary Search Tree Qamar Abbas.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Discrete Mathematics Chapter 5 Trees.
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Search Trees (BST)
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Data Structures Review Session 2
CS200: Algorithms Analysis
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees Ming-Te Chi
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order walk  Search Tree  Insert a element to the Tree  Delete a element form the Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 1

Graph  Directed graph (or digraph) G = (V, E) V: Set of vertex (node) E: Set of edges (ordered vertex pair)  Undirected graph G = (V, E) V: Set of vertex E: Set of edges (unordered vertex pair) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 2

Graph COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 3 A C B D Digraph G = (V, E) V = {A, B, C, D, E, F} E = {(A, B), (C, A), (B, D), (B, C), (C, D), (D, C), (B, B) (F, E)} E F

Graph COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 4 A C B D E F Undirected Graph G = (V, E) V = {A, B, C, D, E, F} E = {(A, B), (A, C), (B, D), (B, C), (E, F)}

Tree  A undirected graph is connected if every pair of vertices is connected by a path.  Free Tree – is a connected acyclic, undirected graph.  Forest – a acyclic but possibly disconnected undirected graph  Rooted tree – a free tree where one of the vertices is distinguished from the other. The distinguished vertex is called root. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 5

Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 6 Free Tree Forest

Tree  If the last edge on the path from from the root R of a tree T to a node is (y,x) then y is parent of x and x is child of y.  If two nodes have same parent, they are siblings  A node has no children is leaf.  A non-leaf node is an internal node  The length of the path from the root R to a node x is the depth of x in the tree.  The largest depth of any node in tree T is the height of tree T. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 7 R ACDB G F H E

Binary 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. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 8

Binary Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 9 Balanced Binary Tree

Binary Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 10 Unbalanced Binary Tree

Binary Search Tree  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). COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 11

Binary Search Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park

Binary Search Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park Unbalanced Binary Tree

Operations on Binary Search Tree  Binary Search Tree Operations Inorder walk Preorder walk Postorder walk Search Tree Insert a element to the Tree Delete a element form the Tree COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 14

Operations on Binary Search 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. Running Time T(n) = 2T(n/2) + 1 = O(n) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 15 Inorder_Tree_Walk(x) { 1If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3print x  key 4Inorder_Tree_Walk(x  rightchild); }

Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 16 Preorder_Tree_Walk(x) { 1If x  NIL { 2Print x  key; 3Inorder_Tree_Walk(x  leftchild); 4Inorder_Tree_Walk(x  rightchild); } Postorder_Tree_Walk(x) { 1If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3Inorder_Tree_Walk(x  rightchild); 4Print x  key; }

Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park Inorder_Tree_Walk(x) { 1 If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3print x  key 4Inorder_Tree_Walk(x  rightchild); }

Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park Preorder_Tree_Walk(x) { 1 If x  NIL { 2print x  key; 3Inorder_Tree_Walk(x  leftchild); 4Inorder_Tree_Walk(x  rightchild); }

Operations on Binary Search Tree (Inorder, Preorder, Postorder) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park Postorder_Tree_Walk(x) { 1 If x  NIL { 2Inorder_Tree_Walk(x  leftchild); 3Inorder_Tree_Walk(x  rightchild); 4Print x  key; }

Operations on Binary Search Tree (Search an Element)  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. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 20

Operations on Binary Search Tree (Search an Element) 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. T(n) = T(n/2) + 1 = O(log n) if a binary tree is balanced T(n)= T(n-1) + 1 = O(n) if a binary tree is not balanced COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 21 Tree_Search (x, k) { 1if x == NIL or k == x  key return x; 2if k < x  key 3return Tree_Search (x  leftchild, k); 4else 5return Tree_Search(x  rightchild, k); }

Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park Search (x, 14) x 14 = 5 or 14> 5 or 14 < 5 ?

Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park Search (x, 14) x 14= 13 or 14> 13 or 14 < 13 ? 14= 14 or 14> 14 or 14 < 14 ?

Operations on Binary Search Tree (Search an Element)  Searching a binary search tree for a specific value can be a recursive or iterative process. Following shows the iterative version of tree search COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 24 Tree_Search_II(x, k) { 1y = x; //better not modify pointer x 2while y  NIL and k  y  key { 3if k < y  key 4y = y  leftchild; 5else 6y = y  rightchild; } 7return y; }

Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park Search (x, 14) x 14 = 5 or 14> 5 or 14 < 5 ?

Operations on Binary Search Tree (Search an Element) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park Search (x, 14) x 14 = 12 or 14> 12 or 14 < 12 ? 14 = 13 or 14> 13 or 14 < 13 ?

Operations on Binary Search Tree (Insert a New Node)  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. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 27

Operations on Binary Search Tree (Insert a New Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 28 // T is point to root of binary tree z is point to new node Tree_Insert (T, z) { 1y = NIL; 2x = T; //while loop find out the location for new node 3while x  NIL { 4y = x; 5if z  key < x  key 6x = x  leftchild; 7else 8x = x  rightchild; } 9z  parent = y; 10if y = NIL; // means there is any node in the BST 11T = z; //new node become root 12else if z  key < y  key 13y  leftchild = z; // insert new node as a left child o y 14else 15y  rightchild = z; //insert new node as a right child of y }

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park ?

Operations on Binary Search Tree (Insert a New Node) COSC320 Algorithm Design & Analysis, Fall12 Dr. Sang-Eon Park

Operations on Binary Search Tree (Minimum Element) Minimum  An element in a binary search tree whose key is a minimum can always be found by leftmost child. T(n) = O(log 2 n) or possible worst case O(n) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 60 Tree_Minimum(x) { 0y = x; 1while y  leftchild  NIL 2y = y  leftchild; 3return y; }

Operations on Binary Search Tree (Maximum Element) Maximum  An element in a binary search tree whose key is a maximum can always be founded by rightmost child. T(n) = O( log 2 n) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 61 Tree_Maximum(x) { 0y = x; 1while y  rightchild  NIL 2y = y  rightchild; 3return y; }

Operations on Binary Search Tree (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 1. 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. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 62

Operations on Binary Search Tree (Successor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park Ex) successor of node 15 is 17 (min of right subtree) Ex) successor of node 13 is 15.

Operations on Binary Search Tree (Successor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 64 Tree_Successor (x) { 1if x  rightchild  NIL return Tree_Minimum(x  rightchild); 2else { 3y = x  parent; // if x is y’s leftchild then y is successor of x 4while y  NIL and x ==y  rightchild { 5x = y; 6y = y  parent; } 7return y; }

Operations on Binary Search Tree (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 1. 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. COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 65

Operations on Binary Search Tree (Predecessor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park Ex) Predecessor of node 15 is 13 (Maximum of left subtree) Ex) Predecessor of node 9 is 7.

Operations on Binary Search Tree (Predecessor of a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 67 Tree_Predecessor (x) { 1if x  leftchild  NIL return Tree_Maximum(x  leftchild) 2else { 3y = x  parent // if x is y’s rightchild then y is predecessor of x 4while y  NIL and x ==y  leftchild { 5x = y 6y = y  parent } 7return y }

Operations on Binary Search Tree (Delete a Node)  There are three cases needed to be concern in binary tree deletion. 1. A deleting node has no children ( deleting node is a leaf) 2. A deleting node has one children. 3. 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 COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 68

Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 69 Delete Leaf Node

Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 70 Delete a Node with one child

Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 71

Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 72

Operations on Binary Search Tree (Delete a Node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 73

Binary Search Tree (Delete a node) COSC 220 Computer Science II, Spr.2012 Dr. Sang-Eon Park 74 Tree_Delete (T, z) { if z  leftchild == NIL or z  rightchild == NIL y = z else y = Tree_Successor(z) if y  leftchild  NIL x = y  leftchild else x = y  rightchild if x  NIL x  parent = y  parent if y  parent ==NIL T  root = x else if y == y  parent  left y  parent  leftchild = x else y  parent  rightchild = x if y  z z  key =y  key delete y }