Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
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,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Binary Search Trees Data Structures Ananda Gunawardena
David Stotts Computer Science Department UNC Chapel Hill.
Discrete Mathematics Chapter 5 Trees.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
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.
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.
Binary Search Trees … From
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
CSE 373 Data Structures Lecture 7
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 202 Trees.
CMSC 341 Splay Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Binary Tree

Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions

Some Terminologies Child and Parent  Every node except the root has one parent.  A node can have zero or more children. Leaves  Leaves are nodes with no children.  1,3,7,8 Sibling  nodes with the same parent.  2 and 8; 1 and 4; 3 and 7

Some Terminologies Path  A sequence of edges.  Length of a path: number of edges on the path Depth of a node = no. of edges from root  Root is also a node, depth of root = ? Height of a node = length of longest path to a leaf  Height of leaves = ?  Height of 2 = ?  Height of root = height of tree

Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions

Short review on binary tree At most two children for each node A root node at the top (or the tree is empty)‏ Nodes with no child=leaf nodes Left complete tree if:  All levels are full except last level  Last level filled from left to right

Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions

Tree traversals To visit each node of the tree Recursively visiting left and right sub-trees Pre-order (NLR)‏ Node  Current Node, Left sub-tree, Right sub-tree In-order (LNR)‏ Node  Left, Node, Right Post-order (LRN)‏ Node  Left, Right, Node

Tree traversals Examples Pre-order (NLR)‏  9,4,2,3,8,6,7  Always print the node first

Tree traversals Examples In-order (LNR)‏  2,4,3,9,6,8,7  Print whenever return from left

Tree traversals Examples Post-order (LRN)‏  2,3,4,6,7,8,9  Print whenever return from right

Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions

Binary Search Tree Properties A Binary Tree (nodes at most two children)‏ Node Left sub-tree < Node < Right sub-tree Which one(s) is/are BST(s)? (1)‏(2)‏ (3)‏

Binary Search Tree Which one(s) is/are BST(s)? (2)‏ (1)‏ (3)‏

Binary Search Tree Which one(s) is/are BST(s)? (1)‏(2)‏ (3)‏

Binary Search Tree Summary LeftNodeRight BST: Left < Node < Right  Heap: Only requiring Parent > Children WHOLEsub-tree WHOLE left (or right) sub-tree X)‏  All items in the sub-tree has to be X)‏ NOT necessarily a Complete tree! Worse case can be a linked list! Same collection of elements can have different BSTs

Binary search tree Find key To look for a node with a certain Key Left < Node < Right Recursively do: 1. Compare(key,node)‏ 2. Key<node: Go left 3. Key>node: Go right

Binary search tree Find the key 8 node 6, Key > 6 Go right, node 8, Key == 8 >

Binary search tree Find the key 3 node 6, Key < 6 Go left, node 2, Key > 2 Go right, node 4, Key < 4 Go left, node 3, Key == 3 O(1) at each level Find a key  O(depth) if found  O(height) if not found Time complexity=O(height)‏ < > <

Binary search tree FindMin and FindMax Left < Node < Right Min = Left most node  Recursively go left Until no more left child Max = Right most node  Recursively go right Until no more right child

Binary search tree FindMin and FindMax

Binary search tree Insert To insert a key into BST, similar to Find Key Proceed as if you want to find the key If found, duplicate key do nothing; or update a counter; or insert to a list; or … Otherwise insert X at the last spot.

Binary search tree Insert the key 5 node 6, Key < 6 Go left, node 2, Key > 2 Go right, node 4, Key > 4 Go right, NULL, insert 5 O(1) at each level Again, O(height)‏ < > 5

Binary search tree Delete To delete a key from a BST Proceed as if you want to find the key If found the node n, delete it! that simple Is it really that simple?  I wish it was  Step two is a bit harder: three cases

Binary search tree Delete the key 1 node 6, Key < 6 Go left, node 2, Key < 2 Go left, node 1, Key == 1 Case one: no child Delete it! < <

Binary search tree Delete the key 8 node 6, Key > 6 Go right, node 8, Key == 8 Case two: one child My parent bypass me, point to my child instead Really no violation?  Left < X < Right >

Binary search tree Delete the key 2 node 6, Key < 6 Go left, node 2, Key == 2 Case two: two child Replace node 2 with Min node (m) of right sub-tree Recursively delete m Why choose this way? <

Binary search tree Delete the key 2, before and after

Binary search tree Delete Summary Case 1: node n is a leaf (no child): delete it! Case 2: node n has only one child:  Parent bypass n, point to n.child and delete n Case 3: node n has two child:  Replace n with smallest node (m) of the right sub-tree  Recursively delete m O(height)? Smallest node=left-most node  Either has right child; OR No child  So, delete m can only be case 1 or 2, the simple cases