0% found this document useful (0 votes)
27 views

Trees

This document discusses binary search trees. It begins by listing topics related to binary search trees including introduction, searching, traversing, inserting elements, and deleting elements. It then provides motivations for using a binary search tree such as efficiency for insertion, deletion, and searching operations. Key aspects of binary search trees are described including that each node has a maximum of two child nodes and elements in the left subtree are less than the parent node while elements in the right subtree are greater. The document defines the node class and tree class needed to implement a binary search tree and describes methods for searching, traversing (inorder, preorder, postorder), and inserting/deleting elements from the tree.

Uploaded by

Dilmi Ranaweera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Trees

This document discusses binary search trees. It begins by listing topics related to binary search trees including introduction, searching, traversing, inserting elements, and deleting elements. It then provides motivations for using a binary search tree such as efficiency for insertion, deletion, and searching operations. Key aspects of binary search trees are described including that each node has a maximum of two child nodes and elements in the left subtree are less than the parent node while elements in the right subtree are greater. The document defines the node class and tree class needed to implement a binary search tree and describes methods for searching, traversing (inorder, preorder, postorder), and inserting/deleting elements from the tree.

Uploaded by

Dilmi Ranaweera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Data Structures and Algorithms

Trees
1
Topics

◼ Introduction to Binary search trees


◼ Searching
◼ Traversing
◼ Inserting Elements
◼ Deleting Elements

2
Motivations for a Binary Search
Tree

◼ Ordered Array takes O(log N) for search


but it takes O(N) for insert, delete
◼ Linked List takes O(1) for insert, delete
but takes O(N) for search
◼ We would like to have a data structure
that is efficient in both insertion, deletion
and searching.

3
Motivations for a Binary Search
Tree
◼ A Binary Search Tree can be used to
insert, delete elements at O(log N) and
search for elements at O(log N)
◼ There are lots of other interesting
applications of trees because of its
hierarchical nature.

4
Trees
◼ A tree is really special variation of a
data structure called a graph.
◼ Trees have studied extensively in
mathematics.
◼ We are interested in a type of Tree
called Binary (Search) Trees.
◼ There are other types of trees such as
Red Black Trees, 2-3-4 trees, B-Trees
5
What is a tree?
A tree is a data structure representing
hierarchical relationships.
◼ Examples in everyday life:
– genealogical tree
– organisational chart
– parliamentary government

◼ Examples in computer science:


– expression evaluation
– games
– dictionaries

6
Visualising a tree
root leaves

Trees in computing
grow upside down

branches
branches

root leaves 7
Genealogical tree
Siridoris and Baby Nona

Amarapala and Padma Rohana and Siriyawathi

Pradeep Saman Nilusha Udaya Malkanthi

◼ One node is the root node


(grandparents Siridoris and Baby Nona)
◼ Nodes other than root have exactly one
parent node.
◼ Each node can have zero to many children. 8
Trees in computing...
root node duriyan

intermediate
node
banana strawberry
branches

apple cherry plumb tomato


leaf nodes

One root at top with many leaves at the bottom.


9
A Tree Has One Root Node

ROOT NODE
Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

10
Leaf Nodes Have No Children

Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

LEAF NODES
11
A Subtree

Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

LEFT SUBTREE OF
ROOT NODE
12
A Subtree

Owner
Jake

Manager Chef
Brad Carol

Waitress Waiter Cook Helper


Joyce Chris Max Len

RIGHT SUBTREE OF
ROOT NODE
13
Binary search trees
E
Each node has two next
links instead if just one!
These are called left
and right links. B G

A D F H
◼ Left branch holds lower key values than
the current node.
◼ Right branch holds higher key values
than the current node.
14
Binary Search Tree

15
Defining class Node
◼ iData used to determine if this is the right
node for insert, delete, find in class Tree
methods.
dData to be manipulated, stored, etc.
left and right child references other instance of
class “Node”

Node
int iData
double dData
Node *leftChild
Node *rightChild

void displayNode()
16
Node Class

class Node {
public:
int iData; // data item (key)
double dData; // data item
Node* leftChild; // this node's left child
Node* rightChild; // this node's right child

Node(int piData, double pdData);


void displayNode(); // display itself
};

17
Node Class
Node::Node(int piData, double pdData) {
iData = piData;
dData = pdData;
leftChild = NULL;
rightChild = NULL;
}

Node::displayNode() {
cout << “[“ << iData << “, “;
cout << dData << “]” << endl;
}
18
Defining class Tree

Tree

Node* root;

Tree();
Node* find(int key)
void insert(int id, double dd)
void delete(int id)

void inOrder(Node *localRoot)


void preOrder(Node *localRoot)
void postOrder(Node *localRoot)
19
Tree Class
class Tree {
private:
Node *root;
public:
Tree();
Node* find(int key);
void insert(int id, double dd);
void delete(int id);

void inOrder(Node localRoot);


void preOrder(Node localRoot);
void postOrder(Node localRoot);
};
20
Find F
Find method...
E
Start at root. F>E
At each step, compare follow right
current node to target
node.
Stop if the current node B G
F<G
equals the target.
follow left

F=F
A D F H found!
◼ Follow the left link if the current node is
less than the target.
◼ Follow the right link if it’s greater.
◼ If you get to a leaf node and haven’t
found it, then it’s not in the tree! 21
find() method
iterative solution
Node* Tree::find(int key) {
Node* current = root; // start at root
while (current->iData != key) {// while no match,
if (key < current->iData) // go left?
current = current->leftChild;
else // or go right?
current = current->rightChild;
if (current == NULL) // if no child,
return NULL; // didn't find it
}
return current; // found it
} // end find() 22
find() method
recursive solution
Node* Tree::find(int key) {
return (findRecursive(key, root));
}
Node* Tree::findRecursive(int key, Node* cur) {
// Not in the tree so return null
if (cur == NULL)
return cur;
else if (key == cur->iData)
return cur;
else if (key < cur->iData)
return findRecursive(key, cur->leftChild);
else
return findRecursive(key, cur->rightChild);23
}
Traversing
◼ Going through the elements in a data
structure.

◼ In Order – Sorted order


◼ pre Order
◼ post Order

24
inOrder
◼ Call itself to traverse the node’s left sub tree
◼ Visit the node
◼ Call itself to traverse the node’s right sub tree

2
◼ Left
◼ Current 1 3
◼ Right

25
inOrder
void Tree::inOrder(Node* localRoot)
{
if (localRoot != NULL)
{
inOrder(localRoot->leftChild);
localRoot->displayNode();
inOrder(localRoot->rightChild);
}
}
26
preOrder
◼ Visit the node
◼ Call itself to traverse the node’s left sub tree
◼ Call itself to traverse the node’s right sub tree

1
◼ Current
◼ Left 2 3
◼ Right

27
Binary Search Tree – Pre Order

41 9 0 3 2 4 37 32 21 73 54 46 72 65 89 74 77
28
preOrder
void Tree::preOrder(Node* localRoot)
{
if(localRoot != NULL)
{
localRoot->displayNode();
preOrder(localRoot->leftChild);
preOrder(localRoot->rightChild);
}
} 29
postOrder
◼ Call itself to traverse the node’s left sub tree
◼ Call itself to traverse the node’s right sub tree
◼ Visit the node

3
◼ Left
1 2
◼ Right
◼ Current

30
Binary Search Tree – Post Order

2 4 3 0 21 32 37 9 46 65 72 54 77 74 89 73 41
31
postOrder
void Tree::postOrder(Node* localRoot)
{
if (localRoot != NULL)
{
postOrder(localRoot->leftChild);
postOrder(localRoot->rightChild);
localRoot->displayNode();
}
}
32
Insert C
E
Insert method C<E
◼ Traverse the
hierarchy to a leaf
node that is a valid B G
parent C>B
◼ The leaf node
becomes the parent A D F H
of the new leaf node
that is inserted. C<D
D is leaf node, so C is
C inserted as D’s child
Must update the left of right link of the inserted
node’s parent.
Trickier if you’re not using recursion!
33
Insert C
Recursive insert E

◼ Recursively find leaf


insertion point.
B G
Final recursive call following
correct null branch
Anchor stops recursion and
creates a new node “C” A D F H
Left child of D
return As recursion
is insertionunwinds
point
update links
C Recurse. Anchor
creates new node “C”
Left link of the insertion point is corrected as the recursion
unwinds.

In all other cases, old child is returned. 34


Binary Search Recursive

void Tree::insert(int id, double dd) {


root = insertRec(id, dd, root);
}
Node* Tree::insertRec(int id, double dd,
Node *cur) {
……
……

} 35
Insertion Recursive
Node* Tree::insertRec(int id, double dd, Node* cur) {
// create the new node and return memory address
if (cur == null) {
Node* theNode = new Node(id, dd);
return theNode;
}
else if (id < cur->iData)
cur->leftChild=insertRec(id, dd, cur->leftChild);
else if (id > cur->iData)
cur->rightChild=insertRec(id, dd, cur->rightChild)

return cur;
}
36
Delete method- Three cases...

◼ Node to be deleted is a leaf node.


◼ Node to be deleted has one child.
◼ Node to be deleted has two children.

37
Node to be deleted is a leaf node
D
Delete A

B G

A C E I

F H J

◼ Find the node to be deleted.


◼ If the node to be deleted is a leaf node then
set the parent’s link to null.
38
Delete node has one child
D

B G

C E I

F H J

◼ find the node to be deleted.


◼ The orphan is adopted by the grandparent.
39
Delete node has two children
D

H
C G
◼ Find the node to be
deleted. E I
◼ Node to be deleted is
replaced by left most
node in right sub-tree, F H J
called the successor.
◼ Successor’s right child
adopted by successor’s
pre-switch parent
(not shown). 40
Expression trees...
◼ In addition to binary search trees, binary
trees can also be used as expression
trees.
◼ Expression trees are a means of
representing an arithmetic expression.
◼ Infix and postfix representations can be
generated from the same tree using
different traversal mechanisms.

41
An expression tree example...
*

+ -

A B X Y

UNDERSTAND TRAVERAL ORDER TO


DISPLAY THE FOLLOWING
INFIX1: A+B*X-Y
INFIX2: ((A+B) * (X-Y))
PREFIX: *+AB-XY
POSTFIX: AB+XY- * 42
More Terminology
E
◼ Each node has at
most 2 children
◼ E is the root and
has no parent B F
◼ B is parent of A & D
◼ H is a child of F
◼ E is an ancestor
of C and G A D H
◼ I is a descendant
of F
◼ G and I are siblings C G I
◼ A, C, G, and I are
leaves (childless) 43
Height and Depth
E
◼ Node height is path
length to the most
distant descendant.
◼ Height of tree is B F
height of root node.
◼ Depth is path length
from the node to the
root. A D H
◼ Height of E is 3
◼ Height of B is 2
◼ Height of D is 1
C G I
◼ Depth of C is 3
◼ Depth of F is 1 44
Levels
E
◼ Number of levels
counts the number of
nodes along any
path
B F
◼ This tree has 4
levels, since there
are 4 nodes along
the longest path.
A D H

C G I

45
Binary Tree Complexity Analysis
◼ For a binary tree of height H and
L=H+1 levels, there are 2 L - 1 = N nodes.

◼When searching for a target, must visit


at most L levels.
◼ The worst case search for completely
balanced tree is log (N+1) = L
2
◼ Time required is proportional to log 2N
◼ In contrast, time required for worst case
linked list search is proportional to N 46
4
2 - 1 = 15 Nodes
15 Nodes in total 4 Levels = (nodes in max path)
Height is 3 (links in max path) Levels is log 2(15+1)

47
Linked list N log 2 N
2 1
versus 4 2
balanced 8 3
16 4
binary tree... 32 5
64 6
128 7
256 8
512 9
assumes 1024 10
tree is 2048 11
balanced! 4096 12
8192 14
16384 13
32768 15 48
Two binary trees...
D A

B
B F
C

A C E G
D

What’s the same? E


What’s different?
F
Which is more efficient?
G49
How good? How bad?
C A

B
B F
C

A C E G
D

Worst case search for left balanced


tree is depth L=log 2 (7+1) = 3. E

Worst case search for the degenerate


tree on right is L=7. F

Insert & delete need to work towards


G
producing balanced trees! 50
Producing balanced trees
◼ Inserting generally produces roughly
balanced trees if inserted into tree in
random order.
◼ If the data is inserted in order, the tree
degenerates into a linked list!
◼ Sometimes if the tree becomes
unbalanced, it’s appropriate to “rebuild”
the tree.
◼ Insert to create balanced trees is
complex, and will be covered in a later
module. 51

You might also like