Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
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, Binary Trees, and Binary Search Trees COMP171.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
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.
Tree.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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 SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 25 Binary Search Trees
Binary Tree.
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved

The Definition of a Tree A tree is a nonlinear data structure that is used to store data in a hierarchical manner (ex. Family tree) A tree is a set of nodes connected by edges (lines connecting the nodes) The nodes represent the entities (ie. People, data) that make up the organization © John Urrutia 2013, All Rights Reserved

Parts of a Tree The top of the tree is called the root node If the node is connected to other nodes ( children) it is a parent node A node without a child node is called a leaf Visiting all the nodes of the tree in a distinct order is called a traversal Moving from one node to the other is called a path or branch © John Urrutia 2013, All Rights Reserved

Parts of a Tree The root node is Level 0 (note: some trees don’t have a root) The parent’s child nodes are at the parent’s Level +1 A Level represents all nodes at the same level in the hierarchy A subtree is any parent node with all of the associated child nodes. © John Urrutia 2013, All Rights Reserved

Unordered Trees An unordered tree has no significance by the value of data stored or the order in which it is stored © John Urrutia 2013, All Rights Reserved

Binary Trees Each parent node is limited to only two children This allows for efficiency in programming ( inserting, deleting, and searching ) The child nodes in a Binary tree are called the left and right nodes Data with lesser values are always stored on the left and greater values are always stored on the right This guarantees a specific order to the tree’s levels Left node Right node © John Urrutia 2013, All Rights Reserved

Building a Binary Search Tree First we construct a generic Node Class that is similar to the Node Class of the linked list code Each node consists of these data elements public class Node { public int Data; //Nodes Data Element public Node Left; //Ref to left Node (< Data) public Node Right; //Ref to left Node (> Data) public void DisplayNode() { Console.Write(Data + " "); } © John Urrutia 2013, All Rights Reserved

…Building a Binary Search Tree Next we build a Binary Search tree class(BST) public class BinarySearchTree { public Node root; public BinarySearchTree() //Default Constructor { root = null; } - The first Node object represents the root node of the tree - The default constructor sets the root node to null, creating a null Node on which to build © John Urrutia 2013, All Rights Reserved

…Building a Binary Search Tree Next, we include an Insert method to add new nodes to our tree public void Insert(int i) { Node newNode = new Node(); newNode.Data = i; if (root == null) //First node in tree is assigned root = newNode; //as the ROOT of the tree else { //… code to add the node into the right or left subtree } This method creates an object of the Node class called newNode Assigns newNode.Data to the data passed by the caller then checks to see if the (BST) has a non-null root node © John Urrutia 2013, All Rights Reserved

…Building a Binary Search Tree This determines the correct position for the new node public void Insert(int i) //CONTINUED … Node current = root; //Set current node to root of tree Node parent; //Create parent node while (true) //find where the current node belongs { parent = current; //set the prospective parent if (i < current.Data) //if the new node data is < the parent { //traverse to the left subtree and current = current.Left; //replace the prospective parent if (current == null) //if prospective parent is leaf node { parent.Left = newNode; //add this new node as the left node and break; //stop } else //traverse to the right subtree { … } © John Urrutia 2013, All Rights Reserved

Set the current node to the right child node of the current node. // else //traverse to the right subtree // { current = current.Right; //replace the prospective parent if (current == null) //if prospective parent is leaf node { parent.Right = newNode; //add this new node as the right node and break; //stop } } // End of while (true) } //End of Insert(int i) } //End of BinarySearchTree class …Building a Binary Search Tree © John Urrutia 2013, All Rights Reserved

…Building a Binary Search Tree New nodes © John Urrutia 2013, All Rights Reserved

Traversing a Binary Search Tree There a three traversal methods used in the BST: Inorder, Preorder, and Postorder. Inorder – traversal visits all the nodes in ascending order Preorder – traversal visits the root node first, followed by the subtree nodes moving from left to right Postorder – traversals visits the subtree nodes left to right first, followed by the root node we will discussed in more detail later (order of operations) © John Urrutia 2013, All Rights Reserved

Inorder Traversal © John Urrutia 2013, All Rights Reserved

Inorder Traversal Code public void InOrder(Node theRoot) { if (!(theRoot == null)) //End of tree? { //No, then InOrder(theRoot.Left); //Traverse left theRoot.DisplayNode(); //Print node InOrder(theRoot.Right); //Traverse right } //Yes, go home } This is written as a recursive procedure © John Urrutia 2013, All Rights Reserved

Inorder Traversal Code static void Main(string[] args) { BinarySearchTree nums = new BinarySearchTree() nums.Insert(23); nums.Insert(45); nums.Insert(16); nums.Insert(37); nums.Insert(3); nums.Insert(99); nums.Insert(22); Console.Write("Inorder traversal: "); nums.InOrder(nums.root); Output: 3, 16, 22, 23, 37, 45, 99 © John Urrutia 2013, All Rights Reserved

Preorder Traversal : © John Urrutia 2013, All Rights Reserved

Preorder Traversal : public void PreOrder(Node theRoot) { if (!(theRoot == null)) //End of tree? { //No, then theRoot.DisplayNode(); //Print node PreOrder(theRoot.Left); //Traverse left PreOrder(theRoot.Right); //Traverse right } //Yes, go home } This is written as a recursive procedure Output: 23, 16, 3, 22, 45, 37, 99 © John Urrutia 2013, All Rights Reserved

Postorder Traversal : © John Urrutia 2013, All Rights Reserved

Postorder Traversal public void PostOrder(Node theRoot) { if (!(theRoot == null)) //End of tree? { //No, then PostOrder(theRoot.Left); //Traverse left PostOrder(theRoot.Right); //Traverse right theRoot.DisplayNode(); //Print node } //Yes, go home } This is written as a recursive procedure Output: 3, 22, 16, 37, 99, 45, 23 © John Urrutia 2013, All Rights Reserved

Finding a Node Three Methods: Minimum, Maximum and Find Minimum Value: The BST always organizes the smallest or Minimum value to be the leftmost Child Node or This Node © John Urrutia 2013, All Rights Reserved

Finding a Node Minimum Values: The leftmost Node public int FindMin() { Node current = root; while (!(current.Left == null)) current = current.Left; return current.Data; } © John Urrutia 2013, All Rights Reserved

Finding a Node Maximum Value: The BST always organizes the largest or Maximum value will always be the rightmost child Node © John Urrutia 2013, All Rights Reserved

Finding a Node Maximum Values: The rightmost Node public int FindMax() { Node current = root; while (!(current.Right == null)) current = current.Right; return current.Data; } © John Urrutia 2011, All Rights Reserved © John Urrutia 2013, All Rights Reserved

Finding a Node Find method: used to determine if a specified value is stored in the BST The method creates a Node object and proceeds to compare it to the other Nodes in the tree by moving left or right until it is found or there are no more Nodes to search: © John Urrutia 2013, All Rights Reserved

Finding a Node © John Urrutia 2013, All Rights Reserved

Finding a Node public Node Find(int key) { Node current = root; //Start at root while (current.Data != key) //key found? { //No, then if (key < current.Data) //key < data? current = current.Left; //Yes, reset to left subtree else current = current.Right; //No, reset to right subtree if (current == null) //End of Tree? return null; //Yes, key not found } return current; //key was found, return node } © John Urrutia 2013, All Rights Reserved

Deleting a Leaf Node Think of it as pulling a real life leaf off of a tree. There is nothing else attached to it. The Node to be removed simply found and set to null Before After © John Urrutia 2013, All Rights Reserved

Deleting Node with One Child The node is cut from the tree links single child to the parent of the removed node © John Urrutia 2013, All Rights Reserved

© John Urrutia 2013, All Rights Reserved Deleting with Two Children This is more complex. We can’t just connect the child Node to the deleted Node’s parent. Delete Node 23 CAN’T HAVE TWO LEFT NODES

© John Urrutia 2013, All Rights Reserved Deleting with Two Children Replacing the parent with the leftmost leaf Node of the rightmost subtree (successor) Loss of balance

Delete Method Method used to delete a value from the BST First checks to see if the value is a leaf If not, find the value that needs to be placed in deleted node to ensure correct tree structure For the left subtree this is will be rightmost successor node For the right subtree this will be the leftmost successor node This will always work but introduces another problem. Loss of balance. © John Urrutia 2013, All Rights Reserved

Deleting Nightmare The successor to any Node is the in order Node that follows it. © John Urrutia 2013, All Rights Reserved – 10 – 15 – 50 – 60 –

Deleting Nightmare Worst Case the successor has 1 child in the right subtree. © John Urrutia 2013, All Rights Reserved Step 1:SuccessorParent leftChild = Successor.rightChild Step 2:Successor rightChild = DeletedNode.rightChild Step 3:ParentNode rightChild = Successor Step 4:Successor leftChild = DeletedNode.leftChild SP S DN

BST Imbalance… oops The previous examples have shown “perfect scenario” BST’s. The trees were fairly balanced… but what can easily happen is shown here.

Self-Balancing BST’s Tree structure Levels Complete Level Level 1 Level 2 Level 3

Self-Balancing BST’s A balanced tree maintains a predefined ratio between its height and width. This offers a guaranteed BST search performance of close to log 2 (n) Several Types: AVL trees Red-Black trees 2-3 and trees Splay trees B-trees, and more…

The Huffman Code We have seen BST’s used to: Efficiently search for elements in an ordered list Represent a way of processing algebraic expressions using precedence BST’s can also be used to compress messages using Huffman codes. © John Urrutia 2013, All Rights Reserved

The Huffman Code Ѣ Ѣ Ѣ Ѣ SUSIE Ѣ SAYS Ѣ IT Ѣ IS Ѣ EASY © John Urrutia 2013, All Rights Reserved

The Huffman Code Assigns a unique set of bits for each character in the message which will save bytes. I.E. now Ѣ is Ѣ the Ѣ time Ѣ for Ѣ all Ѣ good Ѣ men Ѣ to Ѣ come Ѣ to Ѣ the Ѣ aid Ѣ of Ѣ their Ѣ country. ( Ѣ are the blanks/spaces between words) 1 st – Create a priority queue by character frequency in the message 2 nd – Assign a bit string to represent each character 3 rd – translate the characters to the bit string © John Urrutia 2013, All Rights Reserved

The Huffman Code now Ѣ is Ѣ the Ѣ time Ѣ for Ѣ all Ѣ good Ѣ men Ѣ to Ѣ come Ѣ to Ѣ the Ѣ aid Ѣ of Ѣ their Ѣ country. ( Ѣ are the blanks/spaces between words) 1 st – Create a priority queue by character frequency from the message © John Urrutia 2013, All Rights Reserved Ѣ oteihmnracdflgsuwy

The Huffman Code now Ѣ is Ѣ the Ѣ time Ѣ for Ѣ all Ѣ good Ѣ men Ѣ to Ѣ come Ѣ to Ѣ the Ѣ aid Ѣ of Ѣ their Ѣ country. ( Ѣ are the blanks/spaces between words) 2 nd – Assign a bit string to represent each character © John Urrutia 2013, All Rights Reserved Ѣ oteihmnracdfl gsuwy

The Huffman Code now Ѣ is Ѣ the Ѣ time Ѣ for Ѣ all Ѣ good Ѣ men Ѣ to Ѣ come Ѣ to Ѣ the Ѣ aid Ѣ of Ѣ their Ѣ country. ( Ѣ are the blanks/spaces between words) 3 rd – translate the characters to the bit string © John Urrutia 2013, All Rights Reserved

The Huffman Code now Ѣ is Ѣ the Ѣ time Ѣ for Ѣ all Ѣ good Ѣ men Ѣ to Ѣ come Ѣ to Ѣ the Ѣ aid Ѣ of Ѣ their Ѣ country. ( Ѣ are the blanks/spaces between words) 3 rd – translate the characters to the bit string © John Urrutia 2013, All Rights Reserved F5 FF 86 FF 85 D8 4C EC 3F 4F 1F 7F 7F 0F F5 7E 1D 9E 14 3E 9D 85 0B B3 F3 7E D3 FE F2 79 FF 80

The Huffman Code now Ѣ is Ѣ the Ѣ time Ѣ for Ѣ all Ѣ good Ѣ men Ѣ to Ѣ come Ѣ to Ѣ the Ѣ aid Ѣ of Ѣ their Ѣ country. ( Ѣ are the blanks/spaces between words) Efficiency 53 down to 35 ~30% savings in space © John Urrutia 2013, All Rights Reserved