Binary Trees Chapter 6.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Introduction to Trees Chapter 6 Objectives
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
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.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
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,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
COSC2007 Data Structures II
Tree.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Slow Insertion in an Ordered Array 1. Slow Searching in a Linked List 2.
CS261 Data Structures Trees Introduction and Applications.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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
Data Structures TREES.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
CSE 373 Data Structures Lecture 7
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
Tonga Institute of Higher Education
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Introduction to Trees IT12112 Lecture 05.
Trees and Binary Trees.
Trees CSE 373 Data Structures.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Trees.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Binary Trees Chapter 6

6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use them to organize a hierarchical representation of objects. Stacks and queues reflect some hierarchy but are limited to only one dimension. For these reasons, create a new data type tree that consists of nodes and arcs/edges and has the root at the top and the leaves (terminal nodes) at the bottom. Tree Representation Tree generally implemented in the computer using pointers

Tree Terminology / concepts Path : a unique sequence of arcs or the resulting sequence of nodes. Root : the node at the top of the tree. (one node / one path) Leaf : a node that has no children. Subtree : any node can be considered to be the root of a subtree, which consists of its children, and its children's children, and so on. Level of the node : The length of the path from the root to the node. Level of a node is its distance from the root.

Tree Terminology cont’ Parent : Any node (except the root) has exactly one edge running upward to another node.. It has a successor node Child : Any node can have one or more lines running downward to other nodes. It has a predecessor node Siblings :Two or more nodes with the same parent Ancestor : Any node in the path from the root to the node Descendent : Any node in the path below the parent node All nodes in the paths from a given node to a leaf node

Tree Terminology cont’ Length : the number of arcs in a path. Height of non empty tree : the maximum level of a node in the tree. Is the level of the leaf in the longest path from the root + 1 The height of an empty tree is -1 Depth == Height Visiting : a node is visited when program control arrives at the node, usually for the purpose of carrying out some operation on the node. Traversing : to visit all the nodes in some specified order. Key : a value that is used to search for the item or perform other operations on it.

A Root of Subtree I Subtree B B E F C D G H I

Binary Tree The binary search tree : It is a tree whose nodes have two children (possibly empty), and each child is designed as either a left child or a right child. If it has 2i nodes at each level i+1, it is called complete binary tree: all nonterminal nodes have both their children and all leaves are at the same level. The binary search tree : Also called ordered binary trees For each node n, all values stored in its left subtree are less than value v stored in n, and all values stored in the right subtree are greater than v.

Examples of binary trees

6.2 Implementing Binary Tree Why tree / binary tree ? An ordered array is quick in search using binary search , but it is slow in insertion and deleting nodes Linked list is quick in insertion and deletion but slow in searching So, you may use a tree because it combines the advantages of the other two structures: an ordered array and a linked list.

Binary tree can be implemented as : A linked list. An Array. Declare a node as a structure with an information field and two “pointers” fields. However, it may has problems when deleting and inserting nodes

6.3 Searching a Binary Search Tree For every node, compare the key to be located with the value stored in the node currently pointed at. If key is less than the value, go to the left subtree and try again. If it is greater than that value, try the right subtree. If it is the same, the search stops. The search is aborted if there is no way to go – the key is not in the tree.

The complexity of searching is measured by the number of comparisons performed. This number depends on the number of nodes encountered on the unique path leading from the root to the node being searched for. So, the complexity is the length of the path leading to this node plus 1. Complexity depends on The shape of the tree. The position of the node in the tree.

Unbalanced Trees Some of the trees are unbalanced, that is, they have most of their nodes on one side of the root or the other. Individual subtrees may also be unbalanced.

Finding a Node Finding a node with a specific key is the simplest of the major tree operations. Remember that the nodes in a binary search tree correspond to objects containing information, one of them can be considered as a key.

Efficiency of the Find Operation How long it takes to find a node depends on how many levels down it is situated. For example, there can be up to 31 nodes, but no more than 5 levels. Thus you can find any node using a maximum of only 5 comparisons. This is O(log N) time.

Inserting a Node Find the place to insert it. This is much the same process as trying to find a node which turns out not to exist. Follow the path from the root to the appropriate node, which will be the parent of the new node. After this parent is found, the new node is connected as its left or right child, depending on whether the new node's key is less or greater than that of the parent.

Deleting a Node Deleting a node is the most complicated common operation required for binary search trees. However, deletion is important in many tree applications. There are three cases to consider:- 1- The node to be deleted is a leaf node (has no children). 2- The node to be deleted has one child. 3- The node to be deleted has two children.

Case1 : The node to be deleted has no children To delete a leaf node, change the appropriate child field in the node's parent to point to null, instead of to the node.

Case 2 : The node to be deleted has one child The node has only two connections: to its parent and to its only child. You want to "snip" the node out of this sequence by connecting its parent directly to its child.

Case 3 : The node to be deleted has two children If the deleted node has two children, you can't just replace it with one of these children, at least if the child has its own children. To delete a node with two children, replace the node with its in order successor.

problem correction

Finding Maximum and Minimum values For the minimum, go to the left child of the root; then go to the left child of that child, and so on, until you come to a node that has no left child. This node is the minimum:

For the maximum value in the tree, follow the same procedure, but go from right child to right child until you find a node with no right child. This node is the maximum.

Summary Trees consist of nodes (circles) connected by edges (lines). The root is the topmost node in a tree; it has no parent. In a binary tree, a node has at most two children. In a binary search tree, all the nodes that are left descendants of node A have key values less than A; all the nodes that are A’s right descendants have key values greater than (or equal to) A. Trees perform searches, insertions, and deletions in O(log N) time. Nodes represent the data-objects being stored in the tree. Edges are most commonly represented in a program by pointers to a node's children (and sometimes to its parent).

Summary (cont’) An unbalanced tree is one whose root has many more left descendents than right descendants, or vice versa. Searching for a node involves comparing the value to be found with the key value of a node, and going to that node's left child if the key search value is less, or to the node’s right child if the search value is greater. Insertion involves finding the place to insert the new node, and then changing a child data member in its new parent to refer to it.