TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Binary Tree Preorder Traversal

Binary Tree Preorder Traversal with Introduction, Asymptotic Analysis, Array, Pointer, Structure, Singly Linked List, Doubly Linked List, Circular Linked List, Binary Search, Linear Search, Sorting, Bucket Sort, Comb Sort, Shell Sort, Heap Sort, Merge Sort, Selection Sort, Counting Sort, Stack, Qene, Circular Quene, Graph, Tree, B Tree, B+ Tree, Avl Tree etc.

<< Back to BINARY

Pre-order traversal

Steps

  • Visit the root node
  • traverse the left sub-tree in pre-order
  • traverse the right sub-tree in pre-order

Algorithm

  • Step 1: Repeat Steps 2 to 4 while TREE != NULL
  • Step 2: Write TREE -> DATA
  • Step 3: PREORDER(TREE -> LEFT)
  • Step 4: PREORDER(TREE -> RIGHT)
    [END OF LOOP]
  • Step 5: END

C Function

void pre-order(struct treenode *tree)
	{
		if(tree != NULL)
		{
			printf("%d",tree→ root);
			pre-order(tree→ left);
			pre-order(tree→ right);
		}
	}

Example

Traverse the following binary tree by using pre-order traversal


binary tree Pre-order traversal
  • Since, the traversal scheme, we are using is pre-order traversal, therefore, the first element to be printed is 18.
  • traverse the left sub-tree recursively. The root node of the left sub-tree is 211, print it and move to left.
  • Left is empty therefore print the right children and move to the right sub-tree of the root.
  • 20 is the root of sub-tree therefore, print it and move to its left. Since left sub-tree is empty therefore move to the right and print the only element present there i.e. 190.
  • Therefore, the printing sequence will be 18, 211, 90, 20, 190.

Next TopicDoubly Linked List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf