tree data structurekanpuruniversity.org/pdf/anil-yadav-ppts_020420.pdf · • traversing a tree...

51
Tree Data Structure Dr. Anil Yadav C.S.J.M University, Kanpur These notes are designed on Tree Data Structure specially for BTech (CSE),MCA and BCA students 4/2/2020 1

Upload: others

Post on 17-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

TreeData Structure

Dr. Anil YadavC.S.J.M University, Kanpur

These notes are designed on Tree Data Structurespecially for BTech (CSE),MCA and BCA students

4/2/2020 1

Page 2: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

TreeSo far we have seen linear data structure• Linear: before and after relationship• Sack queue, List, vector.• Not linear : Trees• Probable the most fundamental

structure in computing• A tree represents Hierarchy structure• Top element is the root node• Except the root each element has

parent• Each elements has 0 or more children

4/2/2020 2

Page 3: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Tree cont’d

4/2/2020 3

Page 4: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 4

Trees • a tree represents a hierarchy

organization structure of a corporation Electronics R ~us

R&D Purchas ing

Domestic International

- table of contents of a book

homewo.-ks

Page 5: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 5

Anot her Example • Unix or D OS/ Windows file s y s tem

/ user/ rUcourses/

grades homew orks/ programs/ p r o j ects/

h w 1 h w 2 h w 3 pr1 pr2 p r3 paper s /

buylo w sellh igh

g rades

demos/

mar ket

Page 6: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 6

Definition

• A tree T ,is a set of nodes storing elements such that the nodes have a parent-ch ild

relat ionship that sat isfies the fol lowing

• if T is not empty, T has a special free called t he rQ"ot that has no parent

• each node v of T different than t he root has a unique po.rent node w; edch node with parent w is ci child of w

Recursive definit ion

• T is either empty

• or consi:sts of a node r (the root) and a possibly empty set of trees whose roots are t he children of r

Terminology

• siblings: two nodes that have the same parent are called siblings

• internal nodes

• nodes thdf ndve children

• external nodes or leaves

• nodes that don't have ch ildren

• ancestors

• descendants

Page 7: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Tree Definition(s)/Properties

• A tree is a connected graph without any circuits.• Null tree, a tree with out any vertices.• A tree with n vertices has n-1 edges.• A graph is a tree if and only if it is minimally

connected.• A graph G n vertices, n-1 edges, and no circuits is

connected.• There is exactly one path between every pair of

vertices in G.

4/2/2020 7

Page 8: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 8

Types of Nodes

• A leaf node is a node without children.

• An interior node is a node with one or more children.

Page 9: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Terminology

4/2/2020 9

Page 10: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 10

A Tree is a Recursive Data Structure

... -.....

• Each node in the tree is the root of a smaller tree! • refer to such trees as subtrees to distinguish them from

the tree as a whole • example: node 2 is the root of the subtree circled above • example: node 6 is the root of a subtree with only one node

• We'll see that tree algorithms often lend themselves to recursive implementations.

Page 11: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Tree…

• Root: A node without any parents.• External node: A node without children (leaves)• Internal node: A node with at least one child• Sub Tree: Tree consisting of a node and its

descendants.• A node that has no sons is called a leaf .• Two nodes are brothers if they are left and right

sons of the same father.

4/2/2020 11

Page 12: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 12

Path, Depth, Level, and Height

--- level 0

- - level 1

depth =2-- -- level 2

• There is exactly one path (one sequence of edges) connecting each node to the root.

• depth of a node = # of edges on the path from it to the root

• Nodes w ith the same depth form a level of the tree.

• The height of a tree is the maximum depth of its nodes. • example: the tree above has a height of 2

Page 13: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Ancestors and Descendants of uNode n1 is an ancestor of node n2 (and n2 is a descendant of n1) if n1 is either

the father of n2 or the father of some ancestor of n2

4/2/2020 13

Page 14: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Binary tree

• A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets.

• The first subset is a single elements called the root of the tree.

• The other two subsets are themselves binary trees, called the left and right subtree of the original tree. A left or right subtree can be empty.

• Each elements of binary tree is called a node of the tree.

4/2/2020 14

Page 15: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 15

Binary Trees

• In a binary tree, nodes have at most two children.

• distinguish between them using the direction left or right

• Example: ,

26's left child - ,~-~1 I

I I , I I I

26's left subtree ___.-,\ ' '

l<c,-- 26's right child

• Recursive definition: a binary tree is either: 1) empty, or 2) a node (the root of the tree) that has

• one or more pieces of data (the key, and possibly others) • a left subtree, which is itself a binary tree • a right subtree, which is itself a binary tree

Page 16: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Binary tree

Definition: A binary tree is a tree such that

• Every node has at most 2 children• Each node is being labled either a

left child or a right child.• Recursive Def.• A binary tree is empty or it

consist of• A node ( the root) that stores an

element• A binary tree called the left child

of T• A binary tree called the right child

of T

4/2/2020 16

Page 17: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Binary Tree

Definition: A binary tree is a tree such that• Every node has at most 2 children• Each node is being labled either a left child oc a

right child.Recursive Definition• A binary tree is empty or it consist of• A node ( the root) that stores an element• A binary tree called the left child of T• A binary tree called the right child of T

4/2/2020 17

Page 18: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

What are strict binary tree?• If every non leaf node in a binary tree

has nonempty left and right subtrees,

the tree is called a strictly binary tree.

• A strictly binary tree with n leaves

always contains 2n -1 nodes.

• If every non-leaf node in a binary

tree has nonempty left and right

subtrees, the tree is termed a strictly

binary tree. Or, to put it another way,

all of the nodes in a strictly binary

tree are of degree zero or two, never

degree one.4/2/2020 18

Page 19: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Complete binary tree

• A complete binary tree is a special kind of binarytree which will be useful to us.

• When a complete binary tree is built, its nodes aregenerally added one at a time. As with any tree,the first node must be the root.

• The second node of a complete binary tree isalways the left child of the root...

• ... and the third node is always the right child ofthe root.

• The next nodes must always fill the next levelfrom left to right.

4/2/2020 19

Page 20: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

• 1) FULL BINARY TREE: A binary tree of height h that contains exactly (2^h)-1 elements is called a full binary tree. (Ref: Pg 427, Data Structures, Algorithms and Applications in C++ [University Press], Second Edition by Sartaj Sahni).

• or in other words• In a FULL BINARY TREE each node has exactly 0 or

2 children and all leaf nodes are on the same level.

4/2/2020 20

Page 21: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Example:FULL BINARY TREE:

18 / \

15 30/ \ / \

40 50 100 40Full Binary Tree

4/2/2020 21

Page 22: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

• STRICT BINARY TREE: Each node has exactly 0 or 2 children.

Example:18 / \

15 30 / \

40 50

• COMPLETE BINARY TREE: A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible. Example:

18 / \

15 30 / \ / \

40 50 100 40/ \ / 8 7 9

4/2/2020 22

Page 23: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

The following is also a Complete Binary Tree:

18/ \

15 30 / \ / \

40 50 100 40

4/2/2020 23

Page 24: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 24

Traversing a Binary Tree

• Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its data in some way

• example: print the key

• We will look at four types of traversals. Each of them visits the nodes in a different order.

• To understand traversals, it helps to remember the recursive definition of a binary tree, in which every node is the root of a subtree.

12 is the root of ___,......,. , 26's left subtree

4 istheroot of - -t'.>f 12's left subtree

26

,,....._ 32 is the root of 26's right subtree

Page 25: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 25

Preorder Traversal

• preorder traversal of the tree w hose root is N: 1) visit the root, N 2) recursively perform a preorder traversal of N's left subtree 3) recursively perform a preorder traversal of N's right subtree

9

6

• Preorder traversal of the tree above: 7 9 8 4 6 5 2

5

• Which state-space search strategy visits nodes in this order?

Page 26: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Binary Tree Traversal in C

void pretrav (NODEPTR tree){If (tree!=NULL) {printf(“%d\n”, tree→info);pretrav(tree→le );pretrav(tree→right);

}}

4/2/2020 26

Page 27: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 27

Postorder Traversal

• postorder traversal of the tree whose root is N: 1) recursively perform a postorder traversal of N's left subtree 2) recursively perform a postorder traversal of N's right subtree 3) visit the root, N

9

• Postorder traversal of the tree above:

4 8 6 9 2 5 7

5

Page 28: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Post Order Traversalvoid posttrav (NODEPTR tree){If (tree!=NULL) {posttrav(tree→le );posttrav(tree→right);printf(“%d\n”, tree→info);} }

4/2/2020 28

Page 29: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 29

lnorder Traversal

• inorder traversal of the tree whose root is N: 1) recursively perform an inorder traversal of N's left subtree 2) visit the root, N 3) recursively perform an inorder traversal of N's right subtree

9

• lnorder traversal of the tree above: 8 4 9 6 7 2 5

Page 30: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

In Order Traversalvoid intrav (NODEPTR tree){If (tree!=NULL) {intrav(tree→le );printf(“%d\n”, tree→info);intrav(tree→right);} }

4/2/2020 30

Page 31: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 31

Level-Order Traversal

• Visit the nodes one level at a time, from top to bottom and left to right.

• Level-order traversal of the tree above: 7 9 5 8 6 2 4

• Which state-space search strategy visits nodes in this order?

• How could we implement this type of traversal?

Page 32: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 32

Tree-Traversal Summary

preorder: root, left subtree, right subtree

postorder: left subtree, right subtree, root

inorder: left subtree, root, right subtree

level-order: top to bottom, left to right

• Perform each type of traversal on the tree below:

15 7

5

Page 33: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Construct Tree from given Inorder and Preorder traversals

Let us consider the below traversals:Inorder sequence: D B E A F CPreorder sequence: A B D E C FIn a Preorder sequence, leftmost element is the root of the tree. So we know ‘A’ is root for given

sequences. By searching ‘A’ in Inorder sequence, we can find out all elements on left side of‘A’ are in left subtree and elements on right are in right subtree.

A / \D B E F C

A / \

B C / \ / D E F

4/2/2020 33

Page 34: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Example

Given Inorder and preorder traversal. Construct atree and print the Postorder traversal.Inorder: 3 1 4 0 5 2 6Preorder: 0 1 3 4 2 5 6

Output: 3 4 1 5 6 2 0 (Post order)

4/2/2020 34

Page 35: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Construct Full Binary Tree from given preorder and postorder traversals

• Given two arrays that represent preorder and postordertraversals of a full binary tree, construct the binary tree.

• A Full Binary Tree is a binary tree where every node has either 0 or 2 children

• Let us consider the two given arrays as pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7} and post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};

• In pre[], the leftmost element is root of tree. Since the tree is full and array size is more than 1. The value next to 1 in pre[], must be left child of root. So we know 1 is root and 2 is left child. How to find the all nodes in left subtree? We know 2 is root of all nodes in left subtree. All nodes before 2 in post[] must be in left subtree. Now we know 1 is root, elements {8, 9, 4, 5, 2} are in left subtree, and the elements {6, 7, 3} are in right subtree.

4/2/2020 35

Page 36: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Example1

/ \{8, 9, 4, 5, 2} {6, 7, 3}

1 / \

2 3 / \ / \

4 5 6 7/ \8 9

Inorder traversal of the constructed tree: 8 4 9 2 5 1 6 3 7

4/2/2020 36

Page 37: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 37

Using a Binary Tree for an Algebraic Expression

• We'll restrict ourselves to fully parenthesized expressions and to the following binary operators: +, - , 1

', /

• Example expression: ((a + (b 1' c)) - (d / e))

• Tree representation:

• Leaf nodes are variables or constants; interior nodes are operators.

• Because the operators are binary, either a node has two children or it has none.

Page 38: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 38

Traversing an Algebraic-Expression Tree

• lnorder gives conventional algebraic notation. • print '(' before the recursive

call on the left subtree

• print ')' after the recursive call on the right subtree

• for tree at right: ( (a + Cb * c)) - (d / e) )

• Preorder gives functional notation.

• print '('sand ')'s as for inorder, and commas after the recursive call on the left subtree

• for tree above: subtr ( add(a, mult(b, c)) , di v ide(d , e) )

• Postorder gives the order in which the computation must be carried out on a stack/RPN calculator.

• for tree above: push a, push b, push c, multi pl y, add, ...

• see Exp rTree. j av a

Page 39: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Binary Tree RepresentationsNode Representation of Binary Tree

#define NUMNODES 500Struct nodetype{Int info:Int left:Int right;Int father;};Struct nodetype

node[NUMNODES];

Under this representation the operations

Info(p) = node[p].infoLeft(p) = node[p].leftRight(p)= node[p].rightFather(p) = node[p].father

4/2/2020 39

Page 40: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

Dynamic node representation ogBinary tree

Struct nodetype{Int info:Struct nodetype *left;Struct nodetype *right;Struct nodetype *father;};Typedef struct nodetype

*NODEPTR;

Under this representation the operations

Info(p) = p→infoLeft(p) = p→leRight(p)= p → rightFather(p) = p →father

4/2/2020 40

Page 41: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

NODEPTR maketree (intx)

{NODEPTR p;P= getnode();p→info = x;p→left = NULL;p→right = NULL;return(p);}

The routine setleft (int x) sets a node with contents x as the left son of node(p):

void setleft (NODEPTR p,int x){If( p== NULL)printf(“ void insertion\n);else if(p→left!=NULL)printf(“invalid insertion\n”);elsep→left = maketree(x);}

4/2/2020 41

Page 42: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 42

Binary Search Trees

• Search-tree property: for each node k: • all nodes in k's left subtree are < k • all nodes in k's right subtree are >= k

• Our earlier binary-tree example is a search tree:

26

<26

k

• With a search tree, an inorder traversal visits the nodes in order! • in order of increasing key values

Page 43: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 43

Searching for an Item in a Binary Search Tree

• Algorithm for searching for an item with a key k: if k == the root node's key, you're done else if k < the root node's key, search the left subtree else search the right subtree

• Example: search for 7

Page 44: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 44

Inserting an Item in a Binary Search Tree

• We want to insert an item whose key is k.

• We traverse the tree as if we were searching fork.

• If we find a node with key k, we add the data item to the list of items for that node.

• If we don't find it, the last node we encounter will be the parent P of the new node. • if k < P's key, make the new node

P's left child

• else make the node P's right child

• Special case: if the tree is empty, make the new node the root of the tree.

• The resulting tree is still a search tree.

example: inserl 35

26

p

Page 45: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 45

Deleting Items from a Binary Search Tree

• Three cases for deleting a node x • Case 1: x has no children.

Remove x from the tree by setting its parent's reference to null. 26 26

ex: delete 4 12 ' >

• Case 2: x has one child. Take the parent's reference to x and make it refer to x's child.

26

ex: delete 12

Page 46: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 46

Deleting Items from a Binary Search Tree (cont.)

• Case 3: x has two children • we can't just delete x. why?

• instead, we replace x with a node from elsewhere in the tree

• to maintain the search-tree property, we must choose the replacement carefully

• example: what nodes could replace 26 below?

26

12

Page 47: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 47

Deleting Items from a Binary Search Tree (cont.)

• Case 3: x has two children (continued): • replace x with the smallest node in x's right subtree­

call it y - y will either be a leaf node or will have one right child. why?

• After copying y's item into x, we delete y using case 1 or 2.

ex: delete 26

26 X

Page 48: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 48

Efficiency of a Binary Search Tree

• The three key operations (search, insert, and delete) all have the same time complexity.

• insert and delete both involve a search followed by a constant number of additional operations

• Time complexity of searching a binary search tree:

• best case: 0 (1)

• worst case: O(h), where his the height of the tree

• average case: O(h)

• What is the height of a tree containing n items?

• it depends! why?

Page 49: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 49

Balanced Trees

• A tree is balanced if, for each node, the node's subtrees have the same height or have heights that differ by 1.

• For a balanced tree with n nodes:

• height = O(log2n).

• gives a worst-case time complexity that is logarithmic (O(log2n))

- the best worst-case time complexity for a binary tree

26

Page 50: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

4/2/2020 50

What If the Tree Isn't Balanced?

• Extreme case: the tree is equivalent to a linked list

• height = n - 1

• worst-case time complexity = O(n)

• We'll look next at search-tree variants that take special measures to ensure balance.

4

26

Page 51: Tree Data Structurekanpuruniversity.org/pdf/Anil-Yadav-ppts_020420.pdf · • Traversing a tree involves visiting all of the nodes in the tree. • visiting a node = processing its

If any query please write to

[email protected]

4/2/2020 51