SlideShare a Scribd company logo
Binary Trees
Parts of a binary tree
   A binary tree is composed of zero or more nodes
   Each node contains:
       A value (some sort of data item)
       A reference or pointer to a left child (may be null), and
       A reference or pointer to a right child (may be null)
   A binary tree may be empty (contain no nodes)
   If not empty, a binary tree has a root node
       Every node in the binary tree is reachable from the root
        node by a unique path
   A node with neither a left child nor a right child is
    called a leaf
       In some binary trees, only the leaves contain a value
                                                                    2
Picture of a binary tree

                       a


          b                    c



      d        e                       f


g          h       i               j       k


               l
                                               3
Size and depth
                                       The size of a binary tree is the
            a                           number of nodes in it
                                           This tree has size 12
        b           c                  The depth of a node is its
                                        distance from the root
    d       e               f
                                        
                                            a is at depth zero
                                        
                                            e is at depth 2
g       h       i       j       k
                                       The depth of a binary tree is
            l                           the depth of its deepest node
                                           This tree has depth 4


                                                                           4
Balance
                 a                                 a
         b               c                     b
                                           c       e
     d       e       f       g
                                       d       f
  h i              j
                                           g       h
A balanced binary tree
                                        i j
                                   An unbalanced binary tree
   A binary tree is balanced if every level above the lowest is “full”
    (contains 2n nodes)
   In most applications, a reasonably balanced binary tree is
    desirable
                                                                          5
Binary search in an array
   Look at array location (lo + hi)/2

           Searching for 5:
           (0+6)/2 = 3
                                          Using a binary
    hi = 2;
    (0 + 2)/2 = 1         lo = 2;         search tree
                          (2+2)/2=2
                                                      7

                                              3            13
       0     1   2    3       4   5   6
      2     3    5    7 11 13 17          2       5       11 17

                                                                  6
Tree traversals
   A binary tree is defined recursively: it consists of a root, a
    left subtree, and a right subtree
   To traverse (or walk) the binary tree is to visit each node in
    the binary tree exactly once
   Tree traversals are naturally recursive
   Since a binary tree has three “parts,” there are six possible
    ways to traverse the binary tree:
                                root, right, left
      root, left, right
                                right, root, left
      left, root, right
                                right, left, root
      left, right, root




                                                                     7
Preorder traversal
   In preorder, the root is visited first
   Here’s a preorder traversal to print out all the
    elements in the binary tree:

    public void preorderPrint(BinaryTree bt) {
       if (bt == null) return;
       System.out.println(bt.value);
       preorderPrint(bt.leftChild);
       preorderPrint(bt.rightChild);
    }



                                                       8
Inorder traversal
   In inorder, the root is visited in the middle
   Here’s an inorder traversal to print out all the
    elements in the binary tree:

    public void inorderPrint(BinaryTree bt) {
       if (bt == null) return;
       inorderPrint(bt.leftChild);
       System.out.println(bt.value);
       inorderPrint(bt.rightChild);
    }



                                                       9
Postorder traversal
   In postorder, the root is visited last
   Here’s a postorder traversal to print out all the
    elements in the binary tree:

    public void postorderPrint(BinaryTree bt) {
       if (bt == null) return;
       postorderPrint(bt.leftChild);
       postorderPrint(bt.rightChild);
       System.out.println(bt.value);
    }



                                                        10
Tree traversals using “flags”
   The order in which the nodes are visited during a tree
    traversal can be easily determined by imagining there is a
    “flag” attached to each node, as follows:



             preorder                    inorder                     postorder

   To traverse the tree, collect the flags:
                 A                           A                           A

         B               C           B               C           B               C

     D       E       F       G   D       E       F       G   D       E       F       G

         ABDECFG                     DBEAFCG                     DEBFGCA
                                                                                         11
Copying a binary tree
   In postorder, the root is visited last
   Here’s a postorder traversal to make a complete
    copy of a given binary tree:

    public BinaryTree copyTree(BinaryTree bt) {
       if (bt == null) return null;
       BinaryTree left = copyTree(bt.leftChild);
       BinaryTree right = copyTree(bt.rightChild);
       return new BinaryTree(bt.value, left, right);
    }



                                                       12
Other traversals
   The other traversals are the reverse of these three
    standard ones
       That is, the right subtree is traversed before the left subtree
        is traversed
   Reverse preorder: root, right subtree, left subtree
   Reverse inorder: right subtree, root, left subtree
   Reverse postorder: right subtree, left subtree, root




                                                                          13
The End




          14

More Related Content

What's hot

Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
Siddhi Shrivas
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
United International University
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
Raj Sarode
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
MD. MARUFUZZAMAN .
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Zafar Ayub
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Saurabh Mishra
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
binary search tree
binary search treebinary search tree
binary search tree
Shankar Bishnoi
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
Radhika Puttewar
 

What's hot (20)

Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Trees
TreesTrees
Trees
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
binary search tree
binary search treebinary search tree
binary search tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 

Viewers also liked

14.jun.2012
14.jun.201214.jun.2012
14.jun.2012Tech_MX
 
Constants
ConstantsConstants
ConstantsTech_MX
 
Trends and technologies in system softwares
Trends and technologies in system softwaresTrends and technologies in system softwares
Trends and technologies in system softwaresTech_MX
 
Mutable and immutable classes
Mutable and  immutable classesMutable and  immutable classes
Mutable and immutable classesTech_MX
 
Graph theory
Graph theoryGraph theory
Graph theoryTech_MX
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
What are interpersonal skills
What are interpersonal skillsWhat are interpersonal skills
What are interpersonal skillsTech_MX
 
Linear programming problem
Linear programming problemLinear programming problem
Linear programming problemTech_MX
 
Investment problem
Investment problemInvestment problem
Investment problemTech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Buddy system final
Buddy system finalBuddy system final
Buddy system finalTech_MX
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Graph data structure
Graph data structureGraph data structure
Graph data structureTech_MX
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
E post office system
E post office systemE post office system
E post office systemTech_MX
 
Combined paging and segmentation
Combined paging and segmentationCombined paging and segmentation
Combined paging and segmentationTech_MX
 

Viewers also liked (19)

14.jun.2012
14.jun.201214.jun.2012
14.jun.2012
 
Constants
ConstantsConstants
Constants
 
Trends and technologies in system softwares
Trends and technologies in system softwaresTrends and technologies in system softwares
Trends and technologies in system softwares
 
Mutable and immutable classes
Mutable and  immutable classesMutable and  immutable classes
Mutable and immutable classes
 
Graph theory
Graph theoryGraph theory
Graph theory
 
More on Lex
More on LexMore on Lex
More on Lex
 
What are interpersonal skills
What are interpersonal skillsWhat are interpersonal skills
What are interpersonal skills
 
Linear programming problem
Linear programming problemLinear programming problem
Linear programming problem
 
Investment problem
Investment problemInvestment problem
Investment problem
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Buddy system final
Buddy system finalBuddy system final
Buddy system final
 
Set data structure
Set data structure Set data structure
Set data structure
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Inline function
Inline functionInline function
Inline function
 
E post office system
E post office systemE post office system
E post office system
 
Combined paging and segmentation
Combined paging and segmentationCombined paging and segmentation
Combined paging and segmentation
 
Linkers
LinkersLinkers
Linkers
 
Uid
UidUid
Uid
 
Spss
SpssSpss
Spss
 

Similar to 09 binary-trees

Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 
Data structures
Data structuresData structures
Data structures
IIUI
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
Tribhuvan University
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Binary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptBinary Search Tree Traversal.ppt
Binary Search Tree Traversal.ppt
Riannel Tecson
 
Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
Riannel Tecson
 
Tree
TreeTree
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
HamzaUsman48
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Implementation of trees
Implementation of trees Implementation of trees
Implementation of trees
Mubashar Mehmood
 

Similar to 09 binary-trees (20)

Binary trees
Binary treesBinary trees
Binary trees
 
Binary tree
Binary treeBinary tree
Binary tree
 
Trees
TreesTrees
Trees
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Data structures
Data structuresData structures
Data structures
 
Unit 8
Unit 8Unit 8
Unit 8
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Lec6
Lec6Lec6
Lec6
 
Binary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptBinary Search Tree Traversal.ppt
Binary Search Tree Traversal.ppt
 
Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
 
Tree
TreeTree
Tree
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Implementation of trees
Implementation of trees Implementation of trees
Implementation of trees
 

More from Tech_MX

Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimationTech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating SystemTech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pcTech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbmsTech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbmsTech_MX
 
Linear regression
Linear regressionLinear regression
Linear regressionTech_MX
 
Keyboard interrupt
Keyboard interruptKeyboard interrupt
Keyboard interruptTech_MX
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loadersTech_MX
 
Interpersonal communication
Interpersonal communicationInterpersonal communication
Interpersonal communicationTech_MX
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theoryTech_MX
 

More from Tech_MX (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
String & its application
String & its applicationString & its application
String & its application
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Parsing
ParsingParsing
Parsing
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 
Linear regression
Linear regressionLinear regression
Linear regression
 
Keyboard interrupt
Keyboard interruptKeyboard interrupt
Keyboard interrupt
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 
Interpersonal communication
Interpersonal communicationInterpersonal communication
Interpersonal communication
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theory
 

Recently uploaded

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 

09 binary-trees

  • 2. Parts of a binary tree  A binary tree is composed of zero or more nodes  Each node contains:  A value (some sort of data item)  A reference or pointer to a left child (may be null), and  A reference or pointer to a right child (may be null)  A binary tree may be empty (contain no nodes)  If not empty, a binary tree has a root node  Every node in the binary tree is reachable from the root node by a unique path  A node with neither a left child nor a right child is called a leaf  In some binary trees, only the leaves contain a value 2
  • 3. Picture of a binary tree a b c d e f g h i j k l 3
  • 4. Size and depth  The size of a binary tree is the a number of nodes in it  This tree has size 12 b c  The depth of a node is its distance from the root d e f  a is at depth zero  e is at depth 2 g h i j k  The depth of a binary tree is l the depth of its deepest node  This tree has depth 4 4
  • 5. Balance a a b c b c e d e f g d f h i j g h A balanced binary tree i j An unbalanced binary tree  A binary tree is balanced if every level above the lowest is “full” (contains 2n nodes)  In most applications, a reasonably balanced binary tree is desirable 5
  • 6. Binary search in an array  Look at array location (lo + hi)/2 Searching for 5: (0+6)/2 = 3 Using a binary hi = 2; (0 + 2)/2 = 1 lo = 2; search tree (2+2)/2=2 7 3 13 0 1 2 3 4 5 6 2 3 5 7 11 13 17 2 5 11 17 6
  • 7. Tree traversals  A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree  To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once  Tree traversals are naturally recursive  Since a binary tree has three “parts,” there are six possible ways to traverse the binary tree:  root, right, left  root, left, right  right, root, left  left, root, right  right, left, root  left, right, root 7
  • 8. Preorder traversal  In preorder, the root is visited first  Here’s a preorder traversal to print out all the elements in the binary tree: public void preorderPrint(BinaryTree bt) { if (bt == null) return; System.out.println(bt.value); preorderPrint(bt.leftChild); preorderPrint(bt.rightChild); } 8
  • 9. Inorder traversal  In inorder, the root is visited in the middle  Here’s an inorder traversal to print out all the elements in the binary tree: public void inorderPrint(BinaryTree bt) { if (bt == null) return; inorderPrint(bt.leftChild); System.out.println(bt.value); inorderPrint(bt.rightChild); } 9
  • 10. Postorder traversal  In postorder, the root is visited last  Here’s a postorder traversal to print out all the elements in the binary tree: public void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); System.out.println(bt.value); } 10
  • 11. Tree traversals using “flags”  The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows: preorder inorder postorder  To traverse the tree, collect the flags: A A A B C B C B C D E F G D E F G D E F G ABDECFG DBEAFCG DEBFGCA 11
  • 12. Copying a binary tree  In postorder, the root is visited last  Here’s a postorder traversal to make a complete copy of a given binary tree: public BinaryTree copyTree(BinaryTree bt) { if (bt == null) return null; BinaryTree left = copyTree(bt.leftChild); BinaryTree right = copyTree(bt.rightChild); return new BinaryTree(bt.value, left, right); } 12
  • 13. Other traversals  The other traversals are the reverse of these three standard ones  That is, the right subtree is traversed before the left subtree is traversed  Reverse preorder: root, right subtree, left subtree  Reverse inorder: right subtree, root, left subtree  Reverse postorder: right subtree, left subtree, root 13
  • 14. The End 14