Lecture: #9-10. Binary Trees

Similar documents
Binary Search Trees (BST)

Ordered Lists and Binary Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Converting a Number from Decimal to Binary

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Data Structures and Algorithms

DATA STRUCTURES USING C

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees CMPSC 122

Full and Complete Binary Trees

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Algorithms and Data Structures Written Exam Proposed SOLUTION

TREE BASIC TERMINOLOGIES

Data Structures. Level 6 C Module Descriptor

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Data Structure with C

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Binary Trees and Huffman Encoding Binary Search Trees

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Algorithms Chapter 12 Binary Search Trees

Chapter 14 The Binary Search Tree

Data Structure [Question Bank]

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Big Data and Scripting. Part 4: Memory Hierarchies

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Search Trees 3/20/14

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

The ADT Binary Search Tree

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structures and Algorithms(5)

From Last Time: Remove (Delete) Operation

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Lecture Notes on Binary Search Trees

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Search Trees. Ric Glassey

Lecture Notes on Binary Search Trees

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

OPTIMAL BINARY SEARCH TREES

Alex. Adam Agnes Allen Arthur

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Heap Algorithms

Binary Heaps. CSE 373 Data Structures

CSE 326: Data Structures B-Trees and B+ Trees

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Algorithms and Data Structures

GRAPH THEORY LECTURE 4: TREES

Any two nodes which are connected by an edge in a graph are called adjacent node.

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Data Structures Using C++

Data Structures Using C++ 2E. Chapter 5 Linked Lists

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Unordered Linked Lists

Why Use Binary Trees?

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Parallelization: Binary Tree Traversal

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Analysis of Algorithms I: Optimal Binary Search Trees

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Rotation Operation for Binary Search Trees Idea:

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Chapter 8: Binary Trees

Firewall Implementation

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Object Oriented Software Design

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

A DESIGN PATTERNS PERSPECTIVE ON DATA STRUCTURES. Virginia Niculescu

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

Introduction to Data Structures and Algorithms

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

University of Pune Revised Structure for the B. Sc. (Computer Science) Course (Second Year to be implemented from Academic Year )

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

10CS35: Data Structures Using C

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Linked List as an ADT (cont d.)

Data Structures UNIT III. Model Question Answer

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

Hierarchical Model APPENDIXE. E.1 Basic Concepts

Lecture 2 February 12, 2003

Java OOP Binary Search Tree

Lecture 12 Doubly Linked Lists (with Recursion)

Transcription:

Lecture: #9-10 Binary Trees

Topics 1. Definition and Application of Binary Trees 2. Binary Search Tree Operations 3. Template Considerations for Binary Search Trees 19-2

1. Definition and Application of Binary Trees Binary tree: a nonlinear data structure in which each node may point to 0, 1, or two other nodes The nodes that a node N points to are the (left or right) children of N 19-3

Terminology If a node N is a child of another node P, then P is called the parent of N A node that has no children is called a leaf In a binary tree there is a unique node with no parent. This is the root of the tree 19-4

Binary Tree Terminology Root pointer: like a head pointer for a linked list, it points to the root node of the binary tree Root node: the node with no parent 19-5

Binary Tree Terminology Leaf nodes: nodes that have no children The nodes containing 7 and 43 are leaf nodes 7 19 31 43 59 19-6

Binary Tree Terminology Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59 7 19 31 43 59 19-7

Binary Tree Terminology The parent of the node containing 43 is the node containing 59 31 19 59 7 43 19-8

Binary Tree Terminology A subtree of a binary tree is a part of the tree from a node N down to the leaf nodes Such a subtree is said to be rooted at N, and N is called the root of the subtree 19-9

Subtrees of Binary Trees A subtree of a binary tree is itself a binary tree A nonempty binary tree consists of a root node, with the rest of its nodes forming two subtrees, called the left and right subtree 19-10

Binary Tree Terminology The node containing 31 is the root The nodes containing 19 and 7 form the left subtree The nodes containing 59 and 43 form the right subtree 7 19 31 43 59 19-11

Uses of Binary Trees Binary search tree: a binary tree whose data is organized to simplify searches Left subtree at each node contains data values less than the data in the node 19 31 59 Right subtree at each node contains values greater than the data in the node 7 43 19-12

2. Binary Search Tree Operations Create a binary search tree Insert a node into a binary tree put node into tree in its correct position to maintain order Find a node in a binary tree locate a node with particular data value Delete a node from a binary tree remove a node and adjust links to preserve the binary tree and the order 19-13

Binary Search Tree Node A node in a binary tree is like a node in a linked list, except it has two node pointer fields: struct TreeNode { int value; TreeNode *left; TreeNode *right; }; A constructor can aid in the creation of nodes 19-14

TreeNode Constructor TreeNode::TreeNode(int val, TreeNode *l1=, TreeNode *r1=) { value = val; left = l1; right = r1; } 19-15

Creating a New Node TreeNode *p; int num = 23; p = new TreeNode(num); p 23 19-16

Inserting an item into a Binary Search Tree 1) If tree is empty, replace the empty tree with a new binary tree consisting of the new node as root, with empty left and right subtrees 2) Otherwise, if the item is less than root, recursively insert the item in the left subtree. If the item is greater than the root, recursively insert the item into the right subtree 19-17

Inserting an item into a Binary Search Tree Step 2: 23 is greater than 19. Recursively insert 23 into the right subtree 31 root Step 1: 23 is less than 31. Recursively insert 23 into the left subtree 19 59 value to insert: 23 7 43 Step 3: Since the right subtree is, insert 23 here 19-18

Traversing a Binary Tree Three traversal methods: 1) Inorder: a) Traverse left subtree of node b) Process data in node c) Traverse right subtree of node 2) Preorder: a) Process data in node b) Traverse left subtree of node c) Traverse right subtree of node 3) Postorder: a) Traverse left subtree of node b) Traverse right subtree of node c) Process data in node 19-19

Traversing a Binary Tree 19 31 59 TRAVERSAL METHOD NODES VISITED IN ORDER Inorder 7, 19, 31, 43, 59 7 43 Preorder 31, 19, 7, 59, 43 Postorder 7, 19, 43, 59, 31 19-20

Searching in a Binary Tree 1) Start at root node 2) Examine node data: a) Is it desired value? Done b) Else, is desired data < node data? Repeat step 2 with left subtree c) Else, is desired data > node data? Repeat step 2 with right subtree 3) Continue until desired value found or pointer reached 7 19 31 43 59 19-21

Searching in a Binary Tree To locate the node containing 43, 1. Examine the root node (31) 2. Since 43 > 31, examine the right child of the node containing 31, (59) 3. Since 43 < 59, examine the left child of the node containing 59, (43) 4. The node containing 43 has been found 7 31 19 59 43 19-22

Deleting a Node from a Binary Tree Leaf Node If node to be deleted is a leaf node, replace parent node s pointer to it with a pointer, then delete the node 19 19 7 Deleting node with 7 before deletion Deleting node with 7 after deletion 19-23

Deleting a Node from a Binary Tree One Child If node to be deleted has one child node, adjust pointers so that parent of node to be deleted points to child of node to be deleted, then delete the node 19-24

Deleting a Node from a Binary Tree One Child 31 31 19 59 7 59 7 43 43 Deleting node containing 19 before deletion Deleting node containing 19 after deletion 19-25

Deleting a Node from a Binary Tree Two Children If node to be deleted has left and right children, Promote one child to take the place of the deleted node Locate correct position for other child in subtree of promoted child Convention in text: "promote" the right child, position left subtree underneath 19-26

Deleting a Node from a Binary Tree Two Children 31 59 19 59 43 7 43 19 Deleting node with 31 before deletion 7 Deleting node with 31 after deletion 19-27

3. Template Considerations for Binary Search Trees Binary tree can be implemented as a template, allowing flexibility in determining type of data stored Implementation must support relational operators >, <, and == to allow comparison of nodes 19-28