Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
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.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
© 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.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Data Structures Using Java1 Chapter 10 Binary Trees.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
AA Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Binary Tree Applications
Data Structures Using C++ 2E
Binary Trees.
Binary Trees.
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Data Structures – Binary Tree
Presentation transcript:

Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from which all other nodes can be accessed. Other nodes are grouped into subtrees which in turn form other subtrees and so on. The tree shown in figure 1 has a variable number of branches from each node and may not be a practical structure to implement. A special tree with more than two branches can be implemented as a B Tree. A tree with a maximum of two branches is known as a Binary Tree. A Strict Binary Tree has either none or two branches.

Tree diagram figure 1

Knuth Binary Trees A Knuth Binary Tree has none, one or two branches. Figure 2 shows a Knuth Binary Tree. Each node contains two pointers, one pointing to the left subtree and one pointing to the right subtree. A Knuth Tree is easier to implement than a strict binary tree as recursive algorithms can be used. In a Binary Search Tree the values contained in the nodes in the left subtree, child nodes, will all be less than that in the root node. Similarly the values contained in the nodes in the right subtree will all be greater than that in the root node. Trees can be reversed if required by making the left subtrees contain larger values than the root.

Knuth binary tree figure 2

Binary search tree Figure 3 shows the order of the nodes in a example of a binary search tree. The letters symbolise the order of the nodes. In a balanced tree the root node would be approximately midway in an ordered list of the contents of the nodes. Nodes with lower values than the root are found in the left subtree and higher values in the right subtree. Each subtree follows the same rule.

Binary search tree figure 3

Traversing a Tree A traverse is an ordered walk through a tree such that each node is visited only once. There are various ways of traversing a tree. Using figure 3 as an example:

Algorithm for constructing a tree (adding a unique node)‏ If the tree is empty then make this item the tree else compare the item to the root if the item comes before the root add item to the left branch end if if the item comes after the root (if item is not unique need to use an else to allow for equality)‏ add item to the right branch end if

Algorithm for searching a tree if the tree is not empty and not found if the key sought equals the root's key then found is true else if the key sought is less than the root's key then search down the left tree else search down the right tree end if

Deleting an item from a tree There are 4 cases: 1. The node is a leaf 2. The node has only one branch 3. The node has 2 branches and the Rightmost Node of the Left Subtree (RNLS) is a leaf 4. The node has 2 branches and the RNLS has a left subtree

Case 1 - the node is a leaf Free the space occupied by the leaf node. Make pointer to the node from the parent node ==NULL.

Case 2: The node has 1 branch Graft the child branch of the node onto the parent node. Free the space occupied by the deleted node.

Case 3: The node has 2 branches and the Rightmost Node of the Left Subtree (RNLS) is a leaf Copy data part of the RNLS to data part of node to be deleted Free the space occupied by the RNLS Make pointer to the RNLS from it's parent == NULL

Case 4: The node has 2 branches and the RNLS has a left subtree Copy data part of RNLS to data part of node to be deleted Graft the child branch of the RNLS (can only be a left branch) onto the RNLS's parent node. Free the space occupied by the RNLS

tree.c source 1 Global declara tions

tree.c source 2 main()‏

tree.c source 3 chop() pause() menu()‏

tree.c source 4 display, getentry function s

tree.c source 5 insert, find_del functions

tree.c source 6delet() function

tree.c source 7 whichc ase() functio n