Definition A tree is either empty or consists of one node called the root and zero or more subtrees.

Similar documents
DATA STRUCTURES USING C

Binary Search Trees (BST)

Algorithms and Data Structures

Data Structure [Question Bank]

Data Structures and Algorithms V Otávio Braga

Data Structure with C

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

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

Binary Trees and Huffman Encoding Binary Search Trees

Ordered Lists and Binary Trees

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

Binary Search Trees CMPSC 122

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

Algorithms Chapter 12 Binary Search Trees

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

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

Data Structures and Algorithms

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

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

10CS35: Data Structures Using C

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

PES Institute of Technology-BSC QUESTION BANK

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

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

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Converting a Number from Decimal to Binary

Data Structures UNIT III. Model Question Answer

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

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Full and Complete Binary Trees

Analysis of Algorithms I: Binary Search Trees

From Last Time: Remove (Delete) Operation

Data Structures. Level 6 C Module Descriptor

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

Chapter 14 The Binary Search Tree

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

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

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

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

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.

TREE BASIC TERMINOLOGIES

Parallelization: Binary Tree Traversal

GRAPH THEORY LECTURE 4: TREES

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

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:

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

Analysis of Algorithms I: Optimal Binary Search Trees

Big Data and Scripting. Part 4: Memory Hierarchies

Module 2 Stacks and Queues: Abstract Data Types

Introduction to Stacks

Why? A central concept in Computer Science. Algorithms are ubiquitous.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

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

Java Software Structures

Section IV.1: Recursive Algorithms and Recursion Trees

Binary Heap Algorithms

7.1 Our Current Model

International Journal of Software and Web Sciences (IJSWS)

Mathematics for Algorithm and System Analysis

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

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

Chapter 8: Binary Trees

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

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

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

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

AP Computer Science AB Syllabus 1

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

Hash-based Digital Signature Schemes

Object Oriented Software Design

M-way Trees and B-Trees

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

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

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

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

2. FINDING A SOLUTION

The ADT Binary Search Tree

Graduate Assessment Test (Sample)

Exam study sheet for CS2711. List of topics

Binary Search Trees 3/20/14

Data Structures and Algorithms(5)

Rotation Operation for Binary Search Trees Idea:

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Alex. Adam Agnes Allen Arthur

Structural Design Patterns Used in Data Structures Implementation

Chapter 7: Termination Detection

Teaching an Introductory Computer Science Sequence with Python

Lecture Notes on Binary Search Trees

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

4.3 TABLE 3 TABLE five

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

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations (ODE), v0.7.

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

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

Transcription:

Binary Trees Introduction We extend the concept of linked data structures to structure containing nodes with more than one self-referenced field. Definition A tree is either empty or consists of one node called the root and zero or more subtrees. Every node (exclude a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent. On the other hand, each node can be connected to arbitrary number of nodes, called children. Nodes with no children are called leaves, or external nodes. Nodes which are not leaves are called internal nodes. Nodes with the same parent are called siblings. In the following picture A is the root; B, C and D are children of A; C is the parent of E, F and G; B, D, E, F and G are leaves; B, C and D are siblings

Definition A binary tree is either empty or consists of a root, a left subtree and a right subtree. Definition The depth of a node is the number of edges from the root to the node. The height of a node is the number of edges from the node to the deepest leaf. The height of a tree is a height of the root. There exists a unique mapping from any (ordered) tree to a binary tree. A general tree can be implemented using only two references for each node: one to the leftmost child, and one to the sibling. The following picture illustrates that mapping (nodes of the general tree are in black, and edges of a binary tree are in red)

Definition A binary tree in which each node has exactly zero or two children is called a full binary tree. Definition A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. Exercise. What is the maximum height of a binary tree with N nodes? Exercise. What is the minimum height of a binary tree with N nodes? The minimum height is provided by a tree that is completely filled with nodes. Let us assume that a tree height is h. Then the maximum number of nodes is given by (we count nodes by levels) n = 1 + 2 + 4 +... + 2h-1 + 2h = 2h+1-1 Exercise. What is the maximum number of external nodes (or leaves) in a binary tree of height h?

Tree Traversals There are four standard methods of traversing trees: PreOrder traversal -visit the parent first and then left and right children; InOrder traversal -visit the left child, then the parent and the right child; PostOrder traversal -visit left child, then the right child and then the parent; LevelOrder traversal -visit nodes by levels from top to bottom and from left to right. Consider the following tree: PreOrder Traversal -8, 5, 9, 7, 1, 12, 2, 4, 11, 3 InOrder Traversal -9, 5, 1, 7, 2, 12, 8, 4, 3, 11 PostOrder Traversal -9, 1, 2, 12, 7, 5, 3, 11, 4, 8 LevelOrder Traversal -8, 5, 4, 9, 7, 11, 1, 12, 3, 2

These common traversals can be represented as a single algorithm by assuming that we visit each node three times. An Euler tour is a walk around the binary tree where each edge is treated as a wall, which you cannot cross. In this walk each node will be visited either on the left, or under the below, or on the right. The Euler tour in which we visit nodes on the left produces a preorder traversal. When we visit nodes from the below, we get an inorder traversal. And when we visit nodes on the right, we get a postorder traversal. Traversals can be easily implemented recursively. Exercise. Draw a binary tree T such that all the following conditions are satisfied: each node stores a single number and a preorder traversal of T yields 6,4,2,1,3,5,10,8,7,9 and an inorder traversal of T yields 1,2,3,4,5,6,7,8,9,10

Complexity of traversal Assume that T(n) is the function that describes complexity of traversing a binary tree with n nodes. T(n) accumulates complexity for visiting the root -it takes constant time -and for visiting the left subtree T(m) and the right subtree T(k), where m+k = n-1. Therefore, T(n) = c + T(m) + T(k) We solve this recurrence equation asymptotically, by guessing that T(n) < a*n. It follows that T(n) = c + T(m) + T(k) < a1*m + a2*k = max(a1,a2) * (m + k) = O(n) Tree iterator We implement a non-recursive preorder traversal by adding a new method iterator. This method returns an iterator over the nodes of a binary tree in pre-order: public Iterator iterator() { return new PreOrderIterator(); The PreOrderIterator class is implemented as an inner private class of the BTree class private class PreOrderIterator implements Iterator {... The algorithm uses the Stack as an intermediate storage. We start with the root and push it on a stack. When a user calls for next(), we check if the top element has a left child. If it has a left child, we push the child on a stack and return a parent node. If there is no left child, we check for a right child. If it has a right child, we push the right child on a stack and return a parent node. If there is no right child, we move back up the tree (in a while-loop by popping elements from a stack) until we find a node with a right child.

public Object next() { BNode cur = stk.peek(); if(cur.left!= null){ stk.push(cur.left); else { BNode tmp = stk.pop(); while(tmp.right == null){ if (stk.isempty()) return cur; tmp = stk.pop(); stk.push(tmp.right); return cur; Let us demonstrate the above code on the following example. Here is a table showing the output and the stack during each call to next() Output 1 2 4 6 5 7 8 3 6 4 4 7 2 2 2 5 5 8 Stack 1 1 1 1 1 1 1 3

Expression Trees Typically we deal with mathematical expressions in infix notation where the operators (e.g. +, *) are written between the operands: 2 + 5 Here, 2 and 5 are called operands, and the '+' is operator. The above arithmetic expression is called infix, since the operator is in between operands. Writing the operators after the operands gives a postfix form. In a prefix form, operands follow operator: 2 5 + + 2 5 The infix form has a disadvantage that parentheses must be used to indicate the order of evaluation. For example, the following expression is ambiguous 7 + 10 * 15 Evaluation becomes much easier if we evaluate the operators from left to right. This leads to a postfix form expression. Postfix form has a nice property that parentheses are unnecessary. 7 10 15 * +