Traverse a Given Tree in Inorder Traversal (Recursive) in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:14:16

378 Views

ExampleSuppose we have the following tree.Inorder Tree Traversal Output − 4 2 5 1 6 3 7Approach to solve this problemStep 1 − If the root node of the given tree is nil, then return; else, follow the steps given below.Step 2 − Traverse the Left sub-tree.Step 3 − Print the root node data.Step 4 − Traverse the Right sub-tree.Example Live Demopackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)InOrderTraversal(){    if root !=nil{       root.left.InOrderTraversal()       fmt.Printf("%d ", root.data)       root.right.InOrderTraversal()   ... Read More

Insert Node at the Ith Index in Linked List in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:12:31

125 Views

ExamplesExample Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    }    fmt.Println() } func InsertNodeAtIthIndex(head *Node, index, data int) *Node{    if head == nil{       head = NewNode(data, nil)       return head    }    if index == 0{   ... Read More

Insert Node at Mid Index Position in Linked List using Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:07:49

117 Views

ExamplesExample Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    }    fmt.Println() } func InsertNodeAtIthIndex(head *Node, index, data int) *Node{    if head == nil{       head = NewNode(data, nil)       return head    }    if index == 0{   ... Read More

ReactJS useCallback Hook

Rahul Bansal
Updated on 18-Mar-2021 12:04:38

1K+ Views

In this article, we are going to see how to optimize a React application by passing a memoized function.This hook is used to optimize a React application by returning a memoized function which helps to prevent unnecessary re-rendering of a function. This hook stores the cached value of the function and only updates the function if the passed dependencies changes.Syntaxconst memoizedCallback = useCallback(() => {doSomething(a, b); }, [a, b], );Here, doSomething() function will only be called again on the next re-render if the values of a or b changes; otherwise only the cached version of the function is passed.Note: useCallback(fn, ... Read More

ReactJS shouldComponentUpdate Method

Rahul Bansal
Updated on 18-Mar-2021 12:01:54

3K+ Views

In this article, we are going to see how to increase the performance of React application by rerendering the component only when the props passed to it changes or on when certain conditions are met.This method is majorly used to take an exit from the complex React lifecycle, but using it extensively may lead to bugs in the application.SyntaxshouldComponentUpdate(nextProps, nextState)By default, the return value of this method is true; but if it returns false, then the render(), componentWillUpdate() and componentDidUpdate() methods are not called.Example 1In this example, we will build a React application with components only getting re-rendered if the ... Read More

Insert Node at the 0th Position in Linked List using Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:59:36

406 Views

ExamplesApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, create a new node and make it head and return it as the new head.Step 3 − When index == 0, then update the head.Step 4 − Iterate the given linked list from its head. Also, initialize preNode that will keep the store address of the previous node.Step 5 − If index i matches with the given index, then then delete that node.next, break the loop.Step 6 − Return, at the end of loop.Example Live Demopackage main ... Read More

ReactJS getSnapshotBeforeUpdate Method

Rahul Bansal
Updated on 18-Mar-2021 11:58:57

1K+ Views

In this article, we are going to see how to execute a function after the component is updated and before rendering it to the DOM.This method is called before the rendering of the component and after it is updated. This method is majorly used to compare the previous state or the previous props of the component with the new state or the new received props. The value returned by this method is passed as an argument to the componentDidUpdate method.SyntaxgetSnapshotBeforeUpdate(prevProps, prevState)ParametersprevProps − Previous props passed to componentprevState − Previous state of componentExampleIn this example, we will build a React application which ... Read More

ReactJS getDerivedStateFromProps Method

Rahul Bansal
Updated on 18-Mar-2021 11:58:34

4K+ Views

In this article, we are going to see how to execute a function before the component is rendered.This method is called before the rendering or before any updation of the component. This method is majorly used to update the state, before the rendering of the component, which depends upon the props received by the component. This method is called on every rerendering of the component.Syntaxstatic getDerivedStateFromProps(props, state)Parametersprops − Props passed to componentstate − Previous state of componentExampleIn this example, we will build a React application which will fetch the list of users and on clicking the 'fetch user' button, the ... Read More

Delete I-th Index Node in Linked List when Index is Out of Range in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:57:21

204 Views

ExampleApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − When index == 0, then return head.nextStep 4 − Else, iterate the given linked list from its head.Step 5 − If index i matches with the given index (to be deleted), then delete that node.next, break the loop.Step 6 − Return, at the end of the loop.Example Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n ... Read More

Delete the Last Node in a Linked List in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 11:55:37

115 Views

ExamplesApproach to solve this problemStep 1 − Define a method that accepts the head of a linked list.Step 2 − If head == nil, return the head.Step 3 − When index == 0, then return head.nextStep 4 − Else, iterate the given linked list from its head.Step 5 − If index i matches with the given index (to be deleted), then delete that node.next, break the loop.Step 6 − Return at the end of the loop.Example Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n ... Read More

Advertisements