73 views

Uploaded by annieSummer

- Experts System System ch-12
- Assignment- IC Binary Trees Solutions
- DSA Lab Manual -RMDEC
- DATA STRUCTURE ,C,C++
- Meljun Cortes Data Structures Trees
- Tree Doc
- Data Structures 2-1 2nd Mid (1)
- 5. Trees-1
- daa7
- Multi-Relational secured in Data Mining
- 15925_K3R15
- Information Security, Coding Theory and Related Combinatorics 2011 by Cool Release
- AVL tree
- DS _Abc
- Nissan Interview Experience
- AVL tree in details
- Coding Questions
- Árboles Binarios (UQ)
- Ds Program Record
- appmon

You are on page 1of 64

Zulaile Mabni

1

CHAPTER OBJECTIVES

To learn how to use a tree to represent a hierarchical organization of information. To learn how to use recursion to process trees. To understand the different ways of traversing a tree. To understand the difference between binary trees, and binary search trees. To learn how to implement binary trees, and binary search trees using linked data structures and arrays.

Malik D.S.

TREE TERMINOLOGY

A tree consists of a collection of elements or nodes, with each node linked to its successors. The node at the top of a tree is called its root. The links from a node to its successors are called branches. The successors of a node are called its children. The predecessor of a node is called its parent.

Malik D.S.

Each node in a tree has exactly one parent except for the root node, which has no parent. Nodes that have the same parent are siblings. A node that has no children is called a leaf node. A generalization of the parent-child relationship is the ancestor-descendent relationship.

Malik D.S.

BINARY TREES

A binary tree is a hierarchical structure. Definition: A binary tree, T, is either empty or such that:

Malik D.S.

T has a special node called the root node; T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively; LT and RT are binary trees

BINARY TREE

Malik D.S.

Malik D.S.

LA = empty RA = empty

7

Malik D.S.

Malik D.S.

10

Malik D.S.

BINARY TREES

Following class defines the node of a binary tree:

protected class BinaryTreeNode { Object info; BinaryTreeNode llink; BinaryTreeNode rlink; }

Malik D.S.

11

NODES

Data is stored in info The reference to the left child is stored in llink The reference to the right child is stored in rlink

Malik D.S.

12

13

Malik D.S.

Leaf:

node that has no left and right children. Parent: node with at least one child node. Level of a node: number of branches on the path from root to node.

The level of the root node is 0 The level of the children of the root node is 1

Malik D.S.

Height

of a binary tree: The length of the path from the root to the deepest node in the tree. A (rooted) tree with only a node (the root) has a height of zero.

14

Recursive algorithm to find height of binary tree: (height(p) denotes height of binary tree with root p):

Malik D.S.

Find the height of left subtree & right subtree Find the maximum of these two heights and add 1

if(p is NULL)

height(p) = 0

else height(p) = 1 + max(height(p.llink),height(p.rlink))

15

Method to implement above algorithm:

Malik D.S.

private int height(BinaryTreeNode p) { if(p == NULL) return 0; else return 1 + max(height(p.llink), height(p.rlink)); }

16

Malik D.S.

Height = 3

17

Expression tree

Malik D.S.

Huffman tree

Represents Huffman codes for characters that might appear in a text file Huffman code uses different numbers of bits to encode letters as opposed to ASCII or Unicode

All elements in the left subtree precede those in the right subtree

18

19

Malik D.S.

A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node has zero or two children except for the leaves. A perfect binary tree (sometimes complete binary tree) is a full binary tree in which all leaves are at the same depth.

20

Malik D.S.

Malik D.S.

or

Inorder Preorder Postorder

21

TRAVERSALS

Inorder

Traverse the left subtree Visit the node Traverse the right subtree

Malik D.S.

Preorder

Visit the node Traverse the left subtree Traverse the right subtree

22

TRAVERSALS

Postorder

Traverse the left subtree Traverse the right subtree Visit the node

Malik D.S.

23

Malik D.S.

Inorder

:BDAC

24

Preorder : A B D C Postorder: D B C A

private void inorder(BinaryTreeNode p) { if(p != NULL) { inorder(p.llink); System.out.println(p.info + ); inorder(p.rlink); } }

25

Malik D.S.

private void preorder(BinaryTreeNode p) { if(p != NULL) { System.out.println(p.info + ); preorder(p.llink); preorder(p.rlink); } }

26

Malik D.S.

private void postorder(BinaryTreeNode p) { if(p != NULL) { postorder(p.llink); postorder(p.rlink); System.out.println(p.info + ); } }

27

Malik D.S.

28

Malik D.S.

Larger than the data in its left child Smaller than the data in its right child

Malik D.S.

T has a special node called the root node T has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectively Key in root node larger than every key in left subtree and smaller than every key in right subtree LT and RT are binary search trees

29

30

Malik D.S.

Determine whether the binary search tree is empty Search the binary search tree for a particular item Insert an item in the binary search tree Delete an item from the binary search tree

31

Malik D.S.

Find the height of the binary search tree Find the number of nodes in the binary search tree Find the number of leaves in the binary search tree Traverse the binary search tree

32

Malik D.S.

Exercises:

50

30

90

Malik D.S.

12

40

86

100

Determine the order in which the elements would be accessed during an in-order traversal.

33

1. inOrder(t) { if (t is not empty) { inOrder(leftTree(t)); access the root element of t; inOrder(rightTree(t)); } // if } // inOrder traversal Left Root Right

34

Malik D.S.

50

30 12 90

Malik D.S.

40 86

100

35

2.

postOrder (t) { if (t is not empty) { postOrder(leftTree(t)); postOrder(rightTree(t)); access the root element of t; } // if } // postOrder traversal Left Right Root

36

Malik D.S.

Malik D.S.

37

3.

preOrder (t) { if (t is not empty) { access the root element of t; preOrder (leftTree (t)); preOrder (rightTree (t)); } // if } // preOrder traversal

Malik D.S.

38

Malik D.S.

39

An inorder traversal of an expression tree inserts parenthesis where they belong (infix form) A postorder traversal of an expression tree results in postfix form A preorder traversal of an expression tree results in prefix form

40

Malik D.S.

An inorder traversal of an expression tree inserts parenthesis where they belong. An infix expression is obtained.

41

Malik D.S.

Determine the order in which the elements would be accessed during a post-order traversal. Hint: An operator immediately follows its operands.

+

Malik D.S.

*

42

Answer: X, Y, -, Z, A, B, *, / +

Malik D.S.

Postfix!

43

Determine the order in which the elements would be accessed during a pre-order traversal. Hint: An operator immediately precedes its operands.

+

Malik D.S.

*

44

Malik D.S.

Answer: +, -, X, Y, /, Z, *, A, B

Prefix!

45

46

Malik D.S.

47

Malik D.S.

add (73); 80

40

90

Malik D.S.

60

48

50

75

80 40 90

Malik D.S.

60

50

75

73

49

Given the following data: HP, Compaq, Apple, Sony, Samsung, Compact, Toshiba, Acer, Dell Draw the Binary Search Tree

Malik D.S.

50

After deleting the desired item, the resulting tree must be a binary search tree. Four cases (Refer to binary search tree on the next slide):

1) The node to be deleted has no left and right subtrees. Example: Node with data 46

Malik D.S.

2) The node to deleted has no left subtree but has a nonempty right subtree. Example: Node with data 30

3) The node to be deleted has no right subtree but has a nonempty left subtree Example: Node with data 80 4) The node to be deleted has nonempty left and right subtrees. Example: Node with data 50

51

52

Malik D.S.

53

Malik D.S.

Exercises:

remove (50);

80 40 90

Malik D.S.

60

50

75

54

73

remove (40); 80 40 90

Malik D.S.

60

75

55

73

Malik D.S.

80 60 90

75

73

56

remove (80);

80 60 110

Malik D.S.

75

100

150

73

85

105

57

95

The element 80 has two children, so we cannot simply unlink 80 from the tree: that would create a hole.

Of the elements already in the tree, two could replace 80 (and then have the original deleted) without destroying the binary search tree properties. Which two?

58

Malik D.S.

We can replace 80 with either its predecessor, 75, or its successor, 85. The successor of an element is the leftmost element in the right subtree. The predecessor of an element is the rightmost element in the left subtree. Well choose its successor. Replace 80 with 85, and then remove 85.

59

Malik D.S.

85 60 110

Malik D.S.

75

100

150

73

95

105

60

61

Malik D.S.

BINARYSEARCHTREE CLASS

62

Malik D.S.

CHAPTER REVIEW

A tree is a recursive, nonlinear data structure that is used to represent data that is organized as a hierarchy. A binary tree is a collection of nodes with three components: a reference to a data object, a reference to a left subtree, and a reference to a right subtree. In a binary tree used for arithmetic expressions, the root node should store the operator that is evaluated last. A binary search tree is a tree in which the data stored in the left subtree of every node is less than the data stored in the root node, and the data stored in the right subtree is greater than the data stored in the root node.

63

Malik D.S.

REFERENCES

Collin, Williams, Data Structures and The Java Collections Framework, 2nd Edition, McGraw Hill , 2005. Koffman E., Wolfgang P., Objects, Abstraction, Data Structures And Design Using Java, John Wiley & Sons, 2005. Malik D.S, Nair P.S., Data Structures Using Java, Course Technology, 2003.

Malik D.S.

64

- Experts System System ch-12Uploaded bySagar
- Assignment- IC Binary Trees SolutionsUploaded byDrKrishna Priya Chakireddy
- DSA Lab Manual -RMDECUploaded bySai Prashanth
- DATA STRUCTURE ,C,C++Uploaded byBhanu K Prakash
- Meljun Cortes Data Structures TreesUploaded byMELJUN CORTES, MBA,MPA
- Tree DocUploaded byawadhesh.kumar
- Data Structures 2-1 2nd Mid (1)Uploaded byBabu
- 5. Trees-1Uploaded bySarthak Patel
- daa7Uploaded byFauzia Tri Mumpuni
- Multi-Relational secured in Data MiningUploaded byIJIRAE- International Journal of Innovative Research in Advanced Engineering
- 15925_K3R15Uploaded byAlok Bishnoi
- Information Security, Coding Theory and Related Combinatorics 2011 by Cool ReleaseUploaded byTad-electronics Tadelectronics
- AVL treeUploaded byChippyVijayan
- DS _AbcUploaded bysaleem010
- Nissan Interview ExperienceUploaded byYogesh Khurana
- AVL tree in detailsUploaded byAbhisek Sarkar
- Coding QuestionsUploaded byMukesh Panch
- Árboles Binarios (UQ)Uploaded byagmedia95
- Ds Program RecordUploaded byramurama12
- appmonUploaded bySkyezine Via Kit Fox
- Unit 2Uploaded byyaswanthmalaikani
- 4.Shamod 4-4 BST Search -DeletionUploaded byrobinpt
- cs330-10-notes.pdfUploaded byasifmechengr
- Phast7.0 ReleaseLetter&NoteUploaded byssselva
- uml01.psUploaded byYaser Nadvi
- Catalan Trees and Lattice PathsUploaded bymanuel
- 8puzzle.pdfUploaded byganeshkurapati_15512
- ECE QB(2)Uploaded bykholdude
- FINDING DIFFERENCE BETWEEN WEST AND EAST YELLOW FEVER BY APRIORI AND DECISION TREEUploaded byAnonymous UXBSV13c
- L-11Uploaded byKrishna Vijayan

- DATA CENTER DESIGN.pdfUploaded bygilmarorozco
- 2024_V4_Rev_CUploaded byxjaf01
- NewGatewayAPIDoc.pdf TallyUploaded byChandan Shah
- Configuring the HPE Proliant DL380 Gen9 24-SFF CTO Server as a Vertica NodeUploaded byCiscohere-HKYEJIAN
- B-64414EN 01 0i-D DataServerUploaded byLuciano Alberto Lopez Castro
- Django a Pi BookUploaded byNeag Ilie
- Store Sales 2012Uploaded byAmr Shaheen
- Pass your Cisco 400-351 Exam With DumpsUploaded byLogan
- OMC 303 - Section A.docxUploaded byKaro
- F5 201 - Study Guide - TMOS Administration r2Uploaded byAnonymous 9yIt5yn
- Extractor Technical Design SpecificationUploaded byricardo0486
- DxDiag.txtUploaded bykay rodriguez
- SAP HANA Commvault Best PracticesUploaded byivfour47918
- Bluestacks Beta Root InstructionsUploaded bySetio Priyono
- les01Uploaded byMiron Stefan
- Mass Update of Tax Classification Material in Material Master Using Direct Input MethodUploaded byrasheed_am
- Comp01 - Lect 1 - Data ProcessingUploaded byJeiel Baculod
- Blu-ray™ Disc Player - BD-F5100 user manualUploaded bysgross2130
- MoviDrive Drive Inverters DFD11A DeviceNet Fbus Interface ManualUploaded bytom corvin
- ATTINY13A-20PUUploaded byanonymx
- Vcs Admin 601 AixUploaded byCharles Moon
- Caviar Green Specifications (English)Uploaded bydibalada
- ReadMe.rtfUploaded byda
- Wide Area NetworkUploaded bys.reegan
- Quartus on UbuntuUploaded byTien_Do_6932
- 6114-6_SM_demoUploaded byBudi Adnyana DGs
- CDNUploaded byVyankteshKshirsagar
- Oracle 9i SQL Course on oracle.comUploaded byromil
- Client-Server Perseus Software Reference - En02Uploaded bybmartlew
- HPE 3PAR CLI CommandsUploaded byRahul