0% found this document useful (0 votes)
8 views24 pages

Binary Trees

A binary tree is a non-linear data structure with nodes that have at most two children, with key concepts including depth, height, and types of binary trees. Traversals can be depth-first (PreOrder, InOrder, PostOrder) or breadth-first (Level Order), each visiting nodes in a specific order. Binary Search Trees (BST) are a type of binary tree where nodes are ordered, allowing for efficient insertion, searching, and deletion operations based on specific rules regarding node keys.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

Binary Trees

A binary tree is a non-linear data structure with nodes that have at most two children, with key concepts including depth, height, and types of binary trees. Traversals can be depth-first (PreOrder, InOrder, PostOrder) or breadth-first (Level Order), each visiting nodes in a specific order. Binary Search Trees (BST) are a type of binary tree where nodes are ordered, allowing for efficient insertion, searching, and deletion operations based on specific rules regarding node keys.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Binary Trees

A binary tree is a tree-type non-linear


data structure with a maximum of two
children for each parent. Every node in a
binary tree has a left and right reference along
with the data element. The node at the top of
the hierarchy of a tree is called the root node
More tree terminology:
 The depth of a node is the number of edges from the
root to the node.
 The height of a node is the number of edges from the
node to the deepest leaf.
 The height of a tree is a height of the root.
 A full binary tree is a binary tree in which each node has
exactly zero or two children.
 A complete binary tree is a binary tree, which is
completely filled, with the possible exception of the
bottom level, which is filled from left to right.
Continuation…
Every node (excluding a root) in a tree is connected by a
directed edge from exactly one other node. This node is
called a parent.
On the other hand, each node can be connected to
arbitrary number of nodes, called children.
Nodes with no children are called leaves, or external
nodes. Nodes which are not leaves are called internal
nodes.
Nodes with the same parent are called siblings.
Important Terms
 Path − Path refers to the sequence of nodes along the
edges of a tree.
 Root − the node at the top of the tree is called root.
There is only one root per tree and one path from the
root node to any node.
 Parent − any node except the root node has one edge
upward to a node called parent.
 Child − the node below a given node connected by its
edge downward is called its child node.
 Leaf − the node which does not have any child node is
called the leaf node.
Important Terms
 Subtree − Subtree represents the descendants of a
node.
 Visiting − Visiting refers to checking the value of a node
when control is on the node.
 Traversing − Traversing means passing through nodes
in a specific order.
 Levels − Level of a node represents the generation of a
node. If the root node is at level 0, then its next child
node is at level 1, its grandchild is at level 2, and so on.
 Keys − Key represents a value of a node based on
which a search operation is to be carried out for a node.
Traversals
Traversals
• A traversal is a process that visits
all the nodes in the tree.

Two kinds:
 depth-first traversal
 breadth-first traversal
Traversals
There are three different types of depth-first
traversals, :

 PreOrder traversal - visit the parent first and


then left and right children;
 InOrder traversal - visit the left child, then the
parent and the right child;
 PostOrder traversal - visit left child, then the
right child and then the parent;

There is only one kind of breadth-first traversal--the


level order traversal. This traversal visits nodes
by levels from top to bottom and from left to right.
As an example consider the following tree
and its four traversals:
PreOrder - 8, 5, 9, 7, 1, 12,
2, 4, 11, 3
InOrder - 9, 5, 1, 7, 2, 12, 8,
4, 3, 11
PostOrder - 9, 1, 2, 12, 7, 5,
3, 11, 4, 8
LevelOrder - 8, 5, 4, 9, 7,
11, 1, 12, 3, 2
In the next picture we demonstrate the order of node
visitation. Number 1 denote the first node in a particular
traversal and 7 denote the last node.
Binary Search Trees
Binary Search Trees
• A BST is a binary tree where nodes are
ordered in the following way:

 Each node contains one key (also known as


data)
 The keys in the left subtree are less then the
key in its parent node, in short L < P;
 The keys in the right subtree are greater the
key in its parent node, in short P < R;
 Duplicate keys are not allowed.
Implementation
We implement a binary search tree
using a private inner class BSTNode.
In order to support the binary search
tree property, we require that data
stored in each node is Comparable.
Insertion
The insertion procedure is quite similar to searching.
We start at the root and recursively go down the
tree searching for a location in a BST to insert a new
node. If the element to be inserted is already in the
tree, we are done (we do not insert duplicates). The
new node will always replace a NULL reference.
Searching
Searching in a BST always starts at the root. We compare a data
stored at the root with the key we are searching for (let us call it
as toSearch). If the node does not contain the key we proceed
either to the left or right child depending upon comparison. If
the result of comparison is negative we go to the left child,
otherwise - to the right child. The recursive structure of a BST
yields a recursive algorithm.
Deletion
Deletion is somewhat more tricky than insertion. There are several
cases to consider. A node to be deleted (let us call it as toDelete)

 is not in a tree;
 is a leaf;
 has only one child;
 has two children.
If toDelete is not in the tree, there is nothing to delete. If toDelete node has
only one child the procedure of deletion is identical to deleting a node from a
linked list - we just bypass that node being deleted
Deletion of an internal node with two children is less straightforward. If we
delete such a node, we split a tree into two subtrees and therefore, some
children of the internal node won't be accessible after deletion. In the picture
below we delete 8:
Deletion
• Deletion strategy is the following: replace
the node being deleted with the largest
node in the left subtree and then delete
that largest node. By symmetry, the node
being deleted can be swapped with the
smallest node is the right subtree.
• For in depth information, you may watch this video from
Youtube.com:
• https://www.youtube.com/watch?v=9oTV7fDEaCY

You might also like