0% found this document useful (0 votes)
2 views18 pages

CC4 Lecture 5

This document provides an overview of binary trees, including their structure, types, and applications in data organization and algorithms. It explains key concepts such as nodes, edges, and various tree types like full, perfect, and binary search trees. Additionally, it outlines operations for inserting and deleting nodes in a binary search tree, along with practical exercises for illustrating different tree structures.

Uploaded by

chakrasgt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

CC4 Lecture 5

This document provides an overview of binary trees, including their structure, types, and applications in data organization and algorithms. It explains key concepts such as nodes, edges, and various tree types like full, perfect, and binary search trees. Additionally, it outlines operations for inserting and deleting nodes in a binary search tree, along with practical exercises for illustrating different tree structures.

Uploaded by

chakrasgt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit 5: Binary Trees

CC4 – Data structures and algorithms


INSTRUCTOR: DON HARL C. MALABANAN
Introduction to Trees and Binary Trees

What is a tree?
• A hierarchical data structure that is similar to a structure of a graph – nodes
connected to edges
• Used for data that has no key or reference value
• Has two (2) main parts:
• Node – entity that contains a key or value
• Edge – link between any two nodes
• The two (2) types of nodes
• Parent – node containing other nodes
• Child – node that is under another node
Introduction to Trees and Binary Trees

Why Trees?
• Storing data sequentially:
• In most data structures, it requires the data to be stored sequentially
• The more data there is, the time complexity increases
• Higher time complexity = not recommended
• Would also require to have the smaller and largest data stored specifically
• Organizing data in a sequential manner would be faster and retain its
complexity
Application of Trees

• Binary search tree


• Quickly check whether an element is present in a set or not
• Searching algorithms
• Tries
• Variation of trees that store routing information
• B-trees and T-trees
• Used for storing data in database usage
• Syntax tree
• Validation of a syntax in code, particularly an arrangement
Concepts on Trees

• Node
• Entity that contains a key or value
• Root node
• Topmost node of a tree; must not be
a child node; independent
• Edge
• Link / the line between nodes
Concepts on Trees

• Parent
• A node that is on top with an edge below
connecting to another node
• Child
• General term used for a node that has a
parent node
• Subtree
• Child node that is also a parent node
• External / Leaf
• Child node that does not have any child
nodes
Concepts on Trees

• Node height
• Number of edges from the node to the
deepest leaf
• Node depth
• Number of edges from the root to the
node
• Tree height
• Height of the root node
Concepts on Trees
• Degree of a node
• Total number of branches of a node
• Forest
• Collection of disjoin trees cut from the root
Types of Trees
• Binary
• Each parent node can have at most two children
• Binary search
• Applications of binary trees that can quickly maintain a sorted list of numbers
• AVL (Adelson-Velsky and Landis)
• Self-balancing binary search tree which contains extra information for
balancing
• B-Tree
• Self-balancing tree where each node can contain more than one key and can
have more than two children nodes
Binary Tree
What is a binary tree?
• Data structure where each parent node can have at most two children
• There are three nodes:
• Data item / parent node
• Left child
• Right child
Types of Binary Tree
• Full binary tree
• Every parent node has either two or no children
• Perfect binary tree
• Every internal node has exactly two child nodes
• All of the leaf nodes are at the same level
• Complete binary tree
• Similar to full binary tree but every level must be completely filled
• All leaf elements must lean towards the left
• The last leaf might not have a right sibling
Types of Binary Tree
• Skewed binary tree
• Tree is either dominated by left or right nodes
• Balanced binary tree
• Difference between the height of the left and the right subtree for each nod is
0 or 1
• Degenerate / pathological tree
• Tree having a single child either left or right
• Every parent must only have one child
Binary Search Tree

What is BST?
• Data structure that quickly allows us to maintain a sorted list of numbers
• Based off the binary tree, but:
• All nodes of left subtree are less than the root node
• All nodes of right subtree are more than the root node
• Both subtrees of each node are also BSTs
Binary Search Tree
Insert in BST

Insert
• Inserting a node or element inside a binary search tree
• BST elements must be comparable so that we can order them inside the tree
• Compare the current value to the existing nodes
• Recurse down left subtree (< case)
• Recurse down right subtree (> case)
• Handle finding a duplicate value (= case)
• Create a new node (null leaf)
Delete/Remove in BST

Remove
• Removing a node in BST is a decision system regarding four (4) cases
1. Node to remove is a leaf nodes
• Simple remove the node
2. Node to remove has a right subtree but no left subtree
• The successor of the node shall be the new root node
3. Node to remove has a left subtree but no right subtree
• The successor of the node shall be the new root node
4. Node to remove has both left and right subtrees
• Choose whether the new root node will be the largest node on the left
subtree or the smallest value in the right subtree
• Use case 2 or 3 to remove the chosen node from the original address
Lecture Act 5: 10pts
Draw each of the following considering the depth = 3:
1. Full tree
2. Perfect tree
3. Complete tree
4. Skewed tree
5. Balanced tree
6. Degenerate tree

Root node = 1

1pt each tree.


Lecture Act 5: 10pts cont.

1. Illustrate the tree with the following statements:


• insert(5), insert(3), insert(2), insert(1), insert(5), insert(6)
• delete(3), delete(2)
• insert(7), insert(2), insert(1), insert(10)
• delete(2), delete(5)

2. Illustrate the tree with the following statements:


• insert(24), insert(10), insert(30), insert(32), insert(10)
• delete(10), delete(30)
• insert(5), insert(26), insert(31), insert(30)

2 pts per graph

You might also like