Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.
Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node.
CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element,
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Lab 4 Due date: March 29. Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required.
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.
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.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Planning & System installation
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Trees Chapter 15.
Data Structure and Algorithms
Fundamentals of Programming II Introduction to Trees
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Tree.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science

For each number of nodes, n, there is a certain number of possible binary tree configurations. These numbers form a sequence of integers with respect to n. A useful way to describe an integer sequence is to construct a generating function: whose coefficients b i are the sequence. B(x) is power series over a purely formal variable x.

Apparently, b 1 is 1, and b 2 is 2. The b 0 coefficient is somewhat artificial, its the no-nodes tree which is, I guess, the only one. Further analysis gives the following idea: if a binary tree has n nodes, then there must be one node as the root and two subtrees. The total number of nodes in the subtrees must be n-1, and either of them may be empty. Assuming k nodes in the left subtree, the right subtree then has n-k-1 nodes, k going from 0 to n-1. The root node is always there, and therefore the tree configurations differ only by the configuration of subtrees.

The total number of possible trees on n nodes can then be expressed as:

Postorder Template void Postorder ( BinaryTreeNode *t) { // Postorder traversal of *t. If (t){ Postorder ( t->LeftChild);//do left subtree Postorder(t->RightChild);//do right subtree visit(t); // visit tree root } Each root is visited after its left and right subtrees have been traversed.

Example Preorder +*ab/cd Inorder a*b+c/d Postorder ab*cd/+ Elements of a binary tree listed in pre-,in-,and postorder. + b * / d c a

About level order While loop only if the tree is not empty Following the addition of the children of t to the queue,we attempt to delete an element from the queue. If the queue is empty,Delete throws an OutOfBounds exception. If the queue is not empty,then Delete returns the deleted element in t.

Sample tree to illustrate tree traversal

Tree after four nodes visited in preorder traversal

Tree after left subtree of root visited using preorder traversal

Tree after completed preorder traversal

Tree visited using inorder traversal

Tree visited using postorder traversal

Here we will work through an example showing how a preorder traversal can be obtained by explicit use of a stack. The logic follows (see the text for the code): 1.Stack the root node. 2.Exit with completion if the stack is empty. 3.Pop the stack and visit the node obtained. 4.Push the right child, if it exists, on the stack. 5.Push the left child, if it exists, on the stack. 6.Go to 2.

Using the runtime stack via recursion, we can write simple definitions of the three depth-first traversals without use of an explicit stack (in these definitions I leave out the templates for brevity): void Preorder(TreeNode *p) { if (p) { Visit(p); Preorder(p->left); Preorder(p->right); }

void Inorder(TreeNode *p) { if (p) { Inorder(p->left); Visit(p); Inorder(p->right); } void Postorder(TreeNode *p) { if (p) { Postorder(p->left); Postorder(p->right); Visit(p); }

TREE SORT Combines tree initialization, insertion, and inorder traversal to generate and print each node in a "sorted" binary tree. 1. Initialize: read first item, create root node. 2. For each item after the first: read item and insert in tree. 3. Traverse in order, printing each node. More formally, in pseudocode: def treesort () read item root = makebt(null, item, null) while not end of data read item insert(root, item) end while inorder(root) end

Using tree sort, sort the letters M, A, R, J, K, S, V. Insertion produces M / \ A R \ \ J S \ \ K V Traverse in order => A J K M R S V