Binary Trees. Node structure Data A data field and two pointers, left and right.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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 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.
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.
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.
Tree Binary Tree. ARRAYS 1. Good Data structure for Searching algorithms. 2. Disadvantage : Insertion and Deletion of Elements require Data movements(Time.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
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.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
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.
Tree Data Structures.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Discrete Structures Trees (Ch. 11)
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
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.
Data Structure By Amee Trivedi.
Trees Chapter 15.
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Algorithms and Data Structures Recursion and Binary Tree
Section 8.1 Trees.
Data Structures & Algorithm Design
Podcast Ch17a Title: Expression Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
Section 9.3 by Andrew Watkins
CSC 143 Java Applications of Trees.
CSC 143 Java Trees.
BINARY TREE CSC248 – Data Structure.
Chapter 20: Binary Trees.
Binary Tree Traversal.
Presentation transcript:

Binary Trees

Node structure Data A data field and two pointers, left and right

Binary tree structure Binary tree: A tree in which each node has at most two children. Tree: A non-linear data structure (representing a strictly hierarchical system of data) where each data item is thought of as a node. Strictly binary tree: A tree in which each node has precisely two children.

Recursive! All the nodes together form a binary tree. The nodes within the red triangle also form a binary tree. The nodes within the green triangle also form a binary tree. How may binary trees are there in total?

Binary search tree Binary search tree: A binary tree that has the following properties: The left subtree of a node contains only nodes with data less than the node's data. The right subtree of a node contains only nodes with data greater than the node's data. Both the left and right subtrees are also binary search trees. Binary search tree: A binary tree that has the following properties: The left subtree of a node contains only nodes with data less than the node's data. The right subtree of a node contains only nodes with data greater than the node's data. Both the left and right subtrees are also binary search trees.

Terminology root node Parent of the node that contains 12 Left child of the node that contains 25 Leaf nodes Right subtree of the tree of which the 76 node is the root

Binary trees don’t have to be symmetrical

Insertion operation We want to insert a new node into the tree. Where should we put it? 63

Insertion operation It is bigger than 50, so go down the right subtree… 63

Insertion operation It is smaller than 76, so go down the left subtree… 63

Insertion operation It is smaller than 65, so go down the left subtree… 63

Insertion operation It is bigger than 59, and 59 has no right children, so that’s where it goes. 63

Task Think about how you would code the insert operation… 63

The iterative way Don’t think about this too long because there is a beautifully elegant alternative… private Node root; public void insert(int data) { if (root == null) { root = new Node(data); return; } Node tmp = root; while (true) { if (data == root.getData()) { // Data is already in the tree return; } else if (data < root.getData()) { // insert left if (root.getLeft() == null) { root.setLeft(new Node(data)); return; } else { tmp = root.getLeft(); } } else { // insert right if (root.getRight() == null) { root.setRight(new Node(data)); return; } else { tmp = root.getRight(); }

The recursive way Recursion is sometimes tough to get your head round. But can be very simple and very elegant void insertNode(Node root, int data) { if (root == NULL) root = new Node(data); else if (data < root.getData()) insertNode(root.getLeft(), data); else insertNode(root.getRight(), data); } You will be expected to be able to code this type of method from scratch for your exam

Binary Tree Traversals "Traversing" a binary tree means visiting every node in turn There are three types of traversal: Preorder Inorder Postorder There are three types of traversal: Preorder Inorder Postorder

Binary Tree Traversals Let's say that you want to print out your binary tree. Here are the three different traversals. Preorder: When you get to a node: Print the node's data. Then traverse its left subtree. Then traverse its right subtree. Preorder: When you get to a node: Print the node's data. Then traverse its left subtree. Then traverse its right subtree. Inorder: When you get to a node: Traverse its left subtree. Then print the node's data. Then traverse its right subtree. Inorder: When you get to a node: Traverse its left subtree. Then print the node's data. Then traverse its right subtree. Postorder: When you get to a node: Traverse its left subtree. Then traverse its right subtree. Then print the node's data. Postorder: When you get to a node: Traverse its left subtree. Then traverse its right subtree. Then print the node's data. void preOrder(Node n){ if(n == null) return; print(n); preOrder(n.left); preOrder(n.right); } void inOrder(Node n){ if(n == null) return; inOrder(n.left); print(n); inOrder(n.right); } void inOrder(Node n){ if(n == null) return; inOrder(n.left); print(n); inOrder(n.right); } void postOrder(Node n){ if(n == null) return; postOrder(n.left); postOrder(n.right); print(n); } void postOrder(Node n){ if(n == null) return; postOrder(n.left); postOrder(n.right); print(n); } Which of these would you want to use to print your binary search tree in ascending order?

Task Write down the order of the numbers as printed by each of the traversals void preOrder(Node n){ if(n == null) return; print(n); preOrder(n.left); preOrder(n.right); } void inOrder(Node n){ if(n == null) return; inOrder(n.left); print(n); inOrder(n.right); } void postOrder(Node n){ if(n == null) return; postOrder(n.left); postOrder(n.right); print(n); }

Infix, prefix and postfix notation Consider the following mathematical expression: 4  (3 + 8) This is written in what is called infix notation, in which the operators (+, -, x, /) are written between their operands. It is the usual notation that you are familiar with from mathematics. Consider the following mathematical expression: 4  (3 + 8) This is written in what is called infix notation, in which the operators (+, -, x, /) are written between their operands. It is the usual notation that you are familiar with from mathematics. However, there are two other ways of writing expressions like these, and both of them are used in computer science. In prefix notation, also known as Polish notation, the operators come before their operands. In postfix notation, also known as Reverse Polish notation, the operators come after their operands. (All operators are assumed to be binary.) However, there are two other ways of writing expressions like these, and both of them are used in computer science. In prefix notation, also known as Polish notation, the operators come before their operands. In postfix notation, also known as Reverse Polish notation, the operators come after their operands. (All operators are assumed to be binary.)

Examples Infix: 4  (3 + 8) Convert the following expressions to prefix and postfix: / (5 + 2)  (3 + 1) 4.4 / (3 + 6) + 5  9 Convert the following expressions to prefix and postfix: / (5 + 2)  (3 + 1) 4.4 / (3 + 6) + 5  9 Prefix:  Postfix: 

Why use different notations? Computers normally convert all infix expressions to postfix. Reasons: Brackets are not necessary in postfix expressions There are no rules about precedence in postfix expressions, as there are in infix expressions Postfix expressions are easy to evaluate using stacks Stacks are fast and easy to program Computers normally convert all infix expressions to postfix. Reasons: Brackets are not necessary in postfix expressions There are no rules about precedence in postfix expressions, as there are in infix expressions Postfix expressions are easy to evaluate using stacks Stacks are fast and easy to program

What has this got to do with binary trees? What happens when the following tree is printed: preorder inorder postorder What happens when the following tree is printed: preorder inorder postorder

Using stacks to evaluate postfix expressions When we see an operand, push it onto the stack When we see an operator, pop two operands from the stack, do the operation, and push the answer onto the stack Loop If you follow this algorithm, you will be left with one value on the stack, which is the answer to the expression. When we see an operand, push it onto the stack When we see an operator, pop two operands from the stack, do the operation, and push the answer onto the stack Loop If you follow this algorithm, you will be left with one value on the stack, which is the answer to the expression.

Task Create annotated notes in Word, using drawing objects. Show how 5 x (4 + 3) is converted to postfix notation. Show how the postfix expression is evaluated using a stack. Create annotated notes in Word, using drawing objects. Show how 5 x (4 + 3) is converted to postfix notation. Show how the postfix expression is evaluated using a stack.

Task answer Parse expression 5  (4 + 3) 3 Convert to postfix: 543+  Push 5 Push 4 Push 3 (diagram 1) 4 5 Pop 3 Pop 4 Push = 7 (diagram 2) Pop 7 Pop Push 7  5 = 35(diagram 3)

Past exam questions

Past exam questions (cont.)