binary tree (part 1)-chapter 6
TRANSCRIPT
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
1/30
Chapter 6
Binary Trees
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
2/30
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 arelimited to only one dimension.
For these reasons, create a new data type tree that
consists ofnodes and arcs/edges and has the root
at the top and the leaves (terminal nodes) at thebottom.
Tree Representation
Tree generally implemented in the computerusin ointers
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
3/30
Tree Terminology / concepts
Path : a unique sequence of arcs or the resultingsequence 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.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
4/30
Parent: Any node (except the root) has exactly oneedge 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
parentAncestor: Any node in the path from the root to the
node
Descendent :Any node in the path below the parent nodeAll nodes in the aths from a iven node to a leaf node
Tree Terminology cont
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
5/30
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 ofcarrying 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
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
6/30
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
7/30
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
8/30
8
A
B E F
C D G IH
Subtree B Root of Subtree I
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
9/30
Binary 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 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 vstored in n, and
all values stored in the right subtree are greater
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
10/30
xamples of binary trees
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
11/30
Why tree / binary tree ?An ordered array is quick in search using
binary search , but it is slow in insertion
and deleting nodesLinked list is quick in insertion and
deletion but slow in searching
So, you may use a tree because itcombines the advantages of the other
two structures: an ordered array and a
linked list.
6.2 Implementing Binary Tree
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
12/30
Binary tree can be implemented as :
1. A linked list.
2. An Array.
Declare a node as a structure with an informationfield and two pointers fields.
However, it may has problems when deleting and
inserting nodes
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
13/30
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
14/30
. earc ng a nary earcTree
For every node, compare the key to be locatedwith the value stored in the node currently pointed
at.
1. If key is less than the value, go to the left
subtree and try again.2. If it is greater than that value, try the right
subtree.
3. If it is the same, the search stops.4. The search is aborted if there is no way to go
the key is not in the tree.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
15/30
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
16/30
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
1. The shape of the tree.2. The position of the node in the tree.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
17/30
Unbalanced Trees
Some of the trees are unbalanced, that is, theyhave most of their nodes on one side of the root
or the other. Individual subtrees may also be
unbalanced.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
18/30
1. Finding a Node
Finding a node with a specific key is the simplestof 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.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
19/30
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 anynode using a maximum of only 5
comparisons.
This is O(log N) time.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
20/30
2. Inserting a Node
1. Find the place to insert it. This is much the sameprocess as trying to find a node which turns out
not to exist.
2. Follow the path from the root to the appropriate
node, which will be the parent of the new node.
3. 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 greaterthan that of the parent.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
21/30
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
22/30
3. Deleting a Node
Deleting a node is the most complicatedcommon 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.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
23/30
Case1 : The node to be deleted has no
children
To delete a leaf node, change the appropriatechild field in the node's parent to point to null,
instead of to the node.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
24/30
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.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
25/30
Case 3 : The node to be deleted has two
children
If the deleted node has two children, you can'tjust replace it with one of these children, at least
if the child has its own children.
To delete a node with two children, replace thenode with its in order successor.
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
26/30
4 Finding Maximum and Minimum
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
27/30
4. 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:
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
28/30
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 rightchild. This node is the maximum.
S
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
29/30
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 As 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.
S
-
7/29/2019 Binary Tree (Part 1)-Chapter 6
30/30
An unbalanced tree is one whose root has many
more left descendents than right descendants, orvice versa.
Searching for a node involves comparing the value
to be found with the key value of a node, and goingto that node's left child if the key search value is
less, or to the nodes right child if the search value
is greater.
Insertion involves finding the place to insert thenew node, and then changing a child data member
in its new parent to refer to it.
Summary (cont)