Recently do Leetcode on a number of topics on the linked list and two-tree-related operations, and then reviewed the data structure, to achieve the linked list and two fork tree corresponding operation.
#ifndef node_h
#define NODE_H
class node
{public
:
node ();
Node *searchnode (int nodeindex);
void Deletenode ();
void Preordertraversal ();
void Inordertraversal ();
void Postordertracersal ();
int index;
int data;
Node *plchild;
Node *prchild;
Node *pparent;
};
#endif
#include "Node.h" #include <iostream> using namespace std;
Node::node () {index = 0;
data = 0;
Plchild = NULL;
Prchild = NULL;
pparent = NULL;
} node* node::searchnode (int nodeindex) {if (This->index = = Nodeindex) return this;
Node *temp = NULL;
if (this->plchild!= NULL) {if (This->plchild->index = = Nodeindex) return this->plchild;
else{temp = This->plchild->searchnode (Nodeindex);
if (temp!= NULL) return temp;
} if (This->prchild!= NULL) {if (This->prchild->index = = Nodeindex) return this->prchild;
else{temp = This->prchild->searchnode (Nodeindex);
if (temp!= NULL) return temp;
} return NULL;
void Node::D eletenode () {if (this->plchild!= NULL) This->plchild->deletenode ();
if (this->prchild!= NULL) This->prchild->deletenode ();
if (this->pparent!= null) {if (This->pparent->plchild = = this) This->pparent->plchild = null; if (this->pparent->Prchild = = this) This->pparent->prchild = NULL;
} Delete this;
void Node::P reordertraversal () {cout<<this->index<< "" <<this->data<<endl;
if (this->plchild!= NULL) this->plchild->preordertraversal ();
if (this->prchild!= NULL) this->prchild->preordertraversal ();
} void Node::inordertraversal () {if (this->plchild!= NULL) this->plchild->inordertraversal ();
cout<<this->index<< "" <<this->data<<endl;
if (this->prchild!= NULL) this->prchild->inordertraversal ();
void Node::P ostordertracersal () {if (this->plchild!= NULL) this->plchild->postordertracersal ();
if (this->prchild!= NULL) this->prchild->postordertracersal ();
cout<<this->index<< "" <<this->data<<endl; }
#ifndef tree1_h
#define TREE1_H
#include "Node.h"
#include <stdlib.h>
class Tree1
{
Public:
Tree1 ();
~tree1 ();
Node *searchnode (int nodeindex);
BOOL AddNode (int nodeindex, int direction, Node *pnode);
BOOL Deletenode (int nodeindex, Node *pnode);
void Preordertraversal ();
void Inordertraversal ();
void Postordertracersal ();
Private:
Node *m_proot;
#endif
#include "Tree1.h" #include "Node.h" #include <iostream> tree1::tree1 () {m_proot = new Node ();}
Tree1::~tree1 () {deletenode (0,null);
M_proot->deletenode ();
Node *tree1::searchnode (int nodeindex) {return m_proot->searchnode (Nodeindex);
BOOL Tree1::addnode (int nodeindex, int direction, node *pnode) {Node *temp = Searchnode (Nodeindex);
if (temp = NULL) return false;
Node *newnode = new node ();
if (NewNode = NULL) return false;
Newnode->index = pnode->index;
Newnode->data = pnode->data;
Newnode->pparent = temp;
if (direction = = 0) {temp->plchild = NewNode;
} if (direction = = 1) {temp->prchild = NewNode;
return true;
BOOL Tree1::D eletenode (int nodeindex, node *pnode) {Node *temp = Searchnode (Nodeindex);
if (temp = NULL) return false;
if (Pnode!= NULL) {pnode->data = temp->data;
} temp->deletenode ();
return true;
} void Tree1::P reordertraversal () {m_proot->preordertraversal (); } void Tree1::inoRdertraversal () {m_proot->inordertraversal ();
} void Tree1::P ostordertracersal () {m_proot->postordertracersal ();
}
#include <iostream> #include "Tree1.h" #include <stdlib.h> using namespace std;
int main () {node *node1 = new node ();
Node1->index = 1;
Node1->data = 5;
Node *node2 = new node ();
Node2->index = 2;
Node2->data = 8;
Node *node3 = new node ();
Node3->index = 3;
Node3->data = 2;
Node *node4 = new node ();
Node4->index = 4;
Node4->data = 6;
Node *NODE5 = new node ();
Node5->index = 5;
Node5->data = 9;
Node *node6 = new node ();
Node6->index = 6;
Node6->data = 7;
Tree1 *tree1 = new Tree1 ();
Tree1->addnode (0,0,NODE1);
Tree1->addnode (0,1,NODE2);
Tree1->addnode (1,0,NODE3);
Tree1->addnode (1,1,NODE4);
Tree1->addnode (2,0,NODE5);
Tree1->addnode (2,1,NODE6);
Tree1->preordertraversal ();
Tree1->inordertraversal ();
Tree1->postordertracersal ();
Tree1->deletenode (2,null);
Tree1->preordertraversal ();
return 0; }