Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
“There is never enough time to do everything, but there is always enough time to do the most important thing.” – Brian Tracy Thought for the Day.
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.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. 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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
Discrete Mathematics Chapter 5 Trees.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Trees.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Binary Search Trees Chapter 7 Objectives
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Trees.
Binary Trees.
Non-Linear Structures
Introduction to Algorithms and Data Structures
CSC 143 Java Trees.
Trees.
Chapter 20: Binary Trees.
Trees.
Data Structures Using C++ 2E
Presentation transcript:

Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree

What is a Binary Tree A binary tree is a a collection of nodes that consists of the root and other subsets to the root points, which are called the left and right subtrees. Every parent node on a binary tree can have up to two child nodes (roots of the two subtrees); any more children and it becomes a general tree. A node that has no children is called a leaf node.

A Few Terms Regarding Binary Trees A EF C B G D A is the root B and C are A’s children A is B’s parent B & C are the root of two subtrees D, E, and G are leaves

This is NOT A Binary Tree A EF C B GDH This is a general tree because C has three child nodes

This is NOT A Binary Tree A EF C B GD This is a graph because A, B, E, H, F and C form a circuit H

Tree Traversal There are three common ways to traverse a tree: –Preorder: Visit the root, traverse the left subtree (preorder) and then traverse the right subtree (preorder) –Postorder: Traverse the left subtree (postorder), traverse the right subtree (postorder) and then visit the root. –Inorder: Traverse the left subtree (in order), visit the root and the traverse the right subtree (in order).

Tree Traversals: An Example A EF C B G D Preorder: ABDECFG Postorder: DEBGFCA In Order: DBEAFGC

Tree Traversals: An Example A EF C B G D Preorder: A (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Preorder: AB (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Preorder: ABD (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Preorder: ABDE (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Preorder: ABDEC (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Preorder: ABDECF (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Preorder: ABDECFG (visit each node as your reach it)

Tree Traversals: An Example A EF C B G D Postorder:

Tree Traversals: An Example A EF C B G D Postorder:

Tree Traversals: An Example A EF C B G D Postorder: D

Tree Traversals: An Example A EF C B G D Postorder: DE

Tree Traversals: An Example A EF C B G D Postorder: DEB

Tree Traversals: An Example A EF C B G D Postorder: DEB

Tree Traversals: An Example A EF C B G D Postorder: DEB

Tree Traversals: An Example A EF C B G D Postorder: DEBG

Tree Traversals: An Example A EF C B G D Postorder: DEBGF

Tree Traversals: An Example A EF C B G D Postorder: DEBGFC

Tree Traversals: An Example A EF C B G D Postorder: DEBGFCA

Tree Traversals: An Example A EF C B G D In Order:

Tree Traversals: An Example A EF C B G D In Order:

Tree Traversals: An Example A EF C B G D In Order: D

Tree Traversals: An Example A EF C B G D In Order: DB

Tree Traversals: An Example A EF C B G D In Order: DBE

Tree Traversals: An Example A EF C B G D In Order: DBEA

Tree Traversals: An Example A EF C B G D In Order: DBEA

Tree Traversals: An Example A EF C B G D In Order: DBEAF

Tree Traversals: An Example A EF C B G D In Order: DBEAFG

Tree Traversals: An Example A EF C B G D In Order: DBEAFGC

Tree Traversals: An Example A EF C B G D Preorder: ABDECFG Postorder: DEBGFCA In Order: DBEAFGC

Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIECG Postorder: HIFDEBGCA In Order: DHFIBEACG G H

Tree Traversals: Another Example A E F C B I D Preorder: A G H

Tree Traversals: Another Example A E F C B I D Preorder: AB G H

Tree Traversals: Another Example A E F C B I D Preorder: ABD G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDF G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDFH G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDFHI G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIE G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIEC G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIECG G H

Tree Traversals: Another Example A E F C B I D Postorder: G H

Tree Traversals: Another Example A E F C B I D Postorder: G H

Tree Traversals: Another Example A E F C B I D Postorder: G H

Tree Traversals: Another Example A E F C B I D Postorder: G H

Tree Traversals: Another Example A E F C B I D Postorder: H G H

Tree Traversals: Another Example A E F C B I D Postorder: HI G H

Tree Traversals: Another Example A E F C B I D Postorder: HIF G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFD G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFDE G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFDEB G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFDEB G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFDEBG G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFDEBGC G H

Tree Traversals: Another Example A E F C B I D Postorder: HIFDEBGCA G H

Tree Traversals: Another Example A E F C B I D In Order: G H

Tree Traversals: Another Example A E F C B I D In Order: G H

Tree Traversals: Another Example A E F C B I D In Order: D G H

Tree Traversals: Another Example A E F C B I D In Order: D G H

Tree Traversals: Another Example A E F C B I D In Order: DH G H

Tree Traversals: Another Example A E F C B I D In Order: DHF G H

Tree Traversals: Another Example A E F C B I D In Order: DHFI G H

Tree Traversals: Another Example A E F C B I D In Order: DHFIB G H

Tree Traversals: Another Example A E F C B I D In Order: DHFIBE G H

Tree Traversals: Another Example A E F C B I D In Order: DHFIBEA G H

Tree Traversals: Another Example A E F C B I D In Order: DHFIBEAC G H

Tree Traversals: Another Example A E F C B I D In Order: DHFIBEACG G H

Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIECG Postorder: HIFDEBGCA In Order: DHFIBEACG G H

Basic Implementation of a Binary Tree We can implement a binary in essentially the same way as a linked list, except that there are two nodes that comes next: public class Node { private int data; private Node left; private Node right;

public int getData() { return data; } public Node getLeft() { return left; } public Node getRight() { return right; } public void setData(int x) { data = x; }

public void setLeft(Node p) { left = p; } public void setRight(Node p) { right = p; }

The tree Class public class Tree { private Node root; // tree() - The default constructor – Starts // the tree as empty public Tree() { root = null; } // Tree() - An initializing constructor that // creates a node and places in it // the initial value

public Tree(int x) { root = new Node(); root.setData(x); root.setLeft(null); root.setRight(null); } public Node getRoot() { return root; }

// newNode() - Creates a new node with a // zero as data by default public Node newNode() { Node p = new Node(); p.setData(0); p.setLeft(null); p.setRight(null); return(p); }

// newNode() - Creates a new node with the // parameter x as its value public Node newNode(int x) { Node p = new Node(); p.setData(x); p.setLeft(null); p.setRight(null); return(p); }

//travTree() – initializes recursive //traversal of tree public void travTree() { if (root != null) travSubtree(root); System.out.println(); } //travSubtree() – recursive method used to //traverse a binary tree (inorder) public void travSubtree(Node p) { if (p != null) { travSubtree(p.getLeft()); System.out.print(p.getData() + "\t"); travSubtree(p.getRight()); }

// addLeft() - Inserts a new node containing // x as the left child of p public void addLeft(Node p, int x) { Node q = newNode(x); p.setLeft(q); } // addRight() - Inserts a new node containing // x as the right child of p public void addRight(Node p, int x) { Node q = newNode(x); p.setRight(q); }

A Basic Search Tree We can construct a simple search tree if we add new nodes with value x on the tree using this strategy: –Every time x is less than the value in the node we move down to the left. –Every time x is greater than the value in the node we move down to the right.

A Sample Search Tree

// insert() -Insert value x in a new node to //be insertedafter p public voidinsert(int x) { Node p, q; booleanfound = false; p = root; q = null; while (p != null && !found){ q = p; if (p.getData() == x) found = true; else if (p.getData() > x) p = p.getLeft(); else p = p.getRight(); }

if (found) error("Duplicate entry"); if (q.getData() > x) addLeft(q, x); else addRight(q, x); //q = newNode(x); }

// isXThere() -Is there a node in the // tree containing x? public booleanisXThere(int x) { Node p; booleanfound = false; p = root; while (p != null && !found){ if (p.getData() == x) found = true; else if (p.getData() > x) p = p.getLeft(); else p = p.getRight(); } return(found); }

public voiderror(String message) { System.out.println(message); System.exit(0); }

// getNode() -Get the pointer for the // node containing x public NodegetNode(int x) { Node p, q; booleanfound = false; p = root; q = null; while (p != null && !found){ q = p; if (p.getData() == x) found = true; else if (p.getData() > x) p = p.getLeft(); else p = p.getRight(); }

if (found) return(q); else return(null); }

public class TestTree { public static void main(String[] args) { Treemytree = new Tree(8); mytree.addLeft(mytree.getRoot(), 6); mytree.addRight(mytree.getRoot(), 9); mytree.insert(4); mytree.insert(1); mytree.insert(12); if (mytree.isXThere(13)) System.out.println("great"); else System.out.println("not great, Bob"); mytree.travTree(); }

Tracing TestTree 8

8 6

8 9 6