1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.

Slides:



Advertisements
Similar presentations
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
Class 8: Trees. cis 335 Fall 2001 Barry Cohen Definitions n A tree is a nonlinear hierarchical structure (more than one successor) n Consists of nodes.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
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.
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.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Starting at Binary Trees
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
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.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
The Tree ADT.
Data Structure By Amee Trivedi.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
Data Structures Binary Trees 1.
Week 6 - Wednesday CS221.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Podcast Ch17a Title: Expression Trees
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Introduction to Trees IT12112 Lecture 05.
Binary Trees.
CS212: Data Structures and Algorithms
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Data Structures and Algorithms for Information Processing
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

1 Introduction to Binary Trees

2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has a clear predecessor and a clear successor. Predecessors and successors may be defined by arrival time or by relative size. Trees are used to represent hierarchies of data. Any element in a tree may have more than one successor - called it's children.

3 Terminology Node or vertex - the labeled squares Edge -the connecting lines In the General tree to the right: B is the child of A - A is parent of B B and C are siblings A is the root of the tree B and its children (D, E, F) are a subtree of A The parent-child relationship is generalized as ancestor -descendant. B is a descendant of A - A is ancestor to B A C B DEF

4 General Tree Definition A General Tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node R - the root Sets that are general trees -the subtrees of R.

5 Binary Tree Definition A Binary Tree is a set of nodes T such that either: T is empty, or T is partitioned into three disjoint subsets: A single node R -the root Two possibly empty sets that are binary trees, called the left and right subtrees of R.

6 Binary Tree Definition T is a binary tree if either: T has no nodes, or T is of the form: R TLTL TRTR where R is a node and T L and T R are both binary trees. if R is the root of T then : T L is the left subtree of R - if it is not null then its root is the left child of R T R is the right subtree of R - if it is not null then its root is the right child of R

7 Full Binary Trees In a full binary tree: All nodes have two children except leaf nodes. All leaf nodes are located in the lowest level of the tree. A B G C D EF

8 Complete Binary Trees In a complete binary tree: All nodes have two children except those in the bottom two levels. The bottom level is filled from left to right. A B C GE F D HIG

9 Binary Search Trees A binary search tree represents a hierarchy of elements that are arranged by size: For any node n: n's value is greater than any value in its left subtree n's value is less than any value in its right subtree F D I KE H B AC G J

10 Binary Expression Trees A binary expression tree represents an arithmetic expression: For any node n: if n is a leaf node: it must contain an operand. if n is not a leaf node: it must contain an operator. it must have two subtrees. * + - d b c a This tree represents the expression: (a + b) * (c -d) or a b + c d -*

11 Binary Tree Traversals A traversal of a binary tree "visits" each node and for each node: Prints (or operates on) the data contained in that node. Visits the left subtree of the node. Visits the right subtree of the node. Recursive termination occurs when a visited node (or tree) is null. There are three possible traversals of binary trees that differ only in the order that they perform the 3 basic operations.

12 Inorder Traversal inorder (binTree tree) { //performs an inorder traversal of tree if(tree != null){ inorder(left subtree of tree) print tree's data inorder(right subtree of tree) }

13 Inorder Traversal inorder(binTree tree){ // inorder traversal of tree if(tree != null){ inorder(left subtree of tree) print tree's data inorder(right subtree of tree) } * + - d b c a For this tree produces: a + b * c - d

14 Postorder Traversal postorder(binTree tree){ //performs an postorder traversal of tree if(tree != null){ postorder(left subtree of tree) postorder(right subtree of tree) print tree's data }

15 Postorder Traversal postorder(binTree tree){ //postorder traversal of tree if(tree != null){ postorder(left subtree of tree) postorder(right subtree of tree) print tree's data } * + - d b c a For this tree produces: a b + c d - *

16 Preorder Traversal preorder(binTree tree){ //performs an preorder traversal of tree if(tree != null){ print tree's data preorder(left subtree of tree) preorder(right subtree of tree) }

17 Preorder Traversal preorder(binTree tree){ // preorder traversal of tree if(tree != null){ print tree's data preorder(left subtree of tree) preorder(right subtree of tree) } * + - d b c a For this tree produces: * + a b - c d

18 class ExpressionTree - Inner class Tree public class ExpressionTree { /** This inner class provides the structure for a tree node.*/ private class Tree { private char token; private Tree right; private Tree left; public Tree() { right = null; left = null; } public Tree(char tok, Tree lft, Tree rght) { token = tok; left = lft; right = rght; } private Tree theTree; // constants for precedence order private static final int LPAREN = 0; private static final int ADDITIVE = 1; private static final int MULTIPLICATIVE = 2; /** accessor for the Tree reference theTree */ public Tree getTree() { return theTree; } /** mutator for the Tree reference theTree */ public void setTree(Tree treeRef) { theTree = treeRef; }

19 class ExpressionTree - Inner class TreeStack /** This inner class provides a stack of trees (tree nodes) for use in building * a binary expression tree from a String containing a postfix or infix expression. */ private class TreeStack{ /** This inner class provides the structure for a single item on the Tree Stack */ private class TreeStackNode { private Tree aNode; private TreeStackNode next; private TreeStackNode(Tree theNode, TreeStackNode nxt) { aNode = theNode; next = nxt; } private TreeStackNode(Tree theNode) { aNode = theNode; next = null; } } // end class TreeStackNode private TreeStackNode theStack;

20 class ExpressionTree - Inner class TreeStack private TreeStack( ) { theStack = null; } private void push(Tree aNode) { if(theStack == null) theStack = new TreeStackNode(aNode); else theStack = new TreeStackNode(aNode, theStack); } private Tree pop( ) { if(theStack != null) { Tree temp = theStack.aNode; theStack = theStack.next; return temp; } else return null; } private Tree top(){ if(theStack != null){ Tree temp = theStack.aNode; return temp; } else return null; } } // end class TreeStack

21 Building an Expression Tree from a Postfix Expression Similar to postfix evaluation algorithm Requires a stack of operands Here an operand is a tree in which: nodes containing variable names are leaves. nodes containing operators have at least two children which can be other operators or leave nodes. Also requires a String containing the postfix expression.

22 Building an Expression Tree from a Postfix Expression For each token in postfix expression If token is an operand Form a tree node and push on the tree stack Else if token is an operator (+, -, *, /) Form a tree node with the operator Pop the stack and attach the operand popped as the right child of the operator node. Pop the stack and attach the operand popped as the left child of the operator node. Push the operator node. When input string is empty the final tree can be popped off the tree stack.

23 Postfix Expression Tree -Trace A B C D * - + E / B B Stack A A A B C A D C B A C D * B A C D *

24 Postfix Expression Tree -Trace A B C D * - + E / Stack E - B C D - * + A B C D - * + E A B C D - * + / / E A B C D - * +

25 Building an Expression Tree from an Infix Expression Similar to infix to postfix conversion Must account for operator precedence and associativity. Precedence: /, * = 3 +, -= 2 ( = 0 Requires two stacks (of trees): one for operators. one for operands. Also requires a String containing the postfix expression.

26 Building an Expression Tree from an Infix Expression For each token in postfix expression If token is an operand (letter) Form a tree node and push on the operand stack. If token is a left paren ‘(‘ Form a tree node and push the left paren onto the operator stack

27 Building an Expression Tree from an Infix Expression Else if token is an arithmetic operator (+, -, *, /) Form a tree node with the operator while(operator stack is not empty AND precedence(token operator) <= precedence(top of operator stack)  pop tree node off operator stack  pop tree off operand stack - attach to right of operator node  pop tree off operand stack - attach to left of operator node  push operator node onto operand stack push node with the new token onto the operator stack

28 Building an Expression Tree from an Infix Expression Else if token is ) while(token on top of operator stack is not '(' )  pop tree node off operator stack  pop tree off operand stack - attach to right of operator node  pop tree off operand stack - attach to left of operator node  push operator node onto operand stack pop the operator stack - removes (

29 Building an Expression Tree from an Infix Expression After all tokens in postfix expression have been used while(operator stack is not empty)  pop tree node off operator stack  pop tree off operand stack - attach to right of operator node  pop tree off operand stack - attach to left of operator node  push operator node onto operand stack When the operator stack is empty the final tree can be popped off the operand stack.

30 Infix Expression Tree -Trace A + B * C - D A A Operand Stack + A + Operator Stack Operand Stack Operator Stack B B + Operand Stack Operator Stack A

31 Infix Expression Tree -Trace A + B * C - D * B + Operand Stack Operator Stack A * C B + Operand Stack Operator Stack A * C -(1) B Operand Stack Operator Stack A C * +

32 Infix Expression Tree -Trace A + B * C - D - (2) B - Operand Stack Operator Stack A C * + Finish Operand Stack Operator Stack D - Operand Stack Operator Stack D B A C * + B A C * + - D